DEV Community

Joe Lin for BeGoodTool.com

Posted on

Keeping Localized Converter Content and URLs in Sync in Vue

A translated conversion page can display the right formula while still building its URL from the wrong identity. I wanted the centimeter-to-feet page to keep three things distinct: which conversion it represents, which language is being shown, and which numerical operation runs underneath.

The Vue implementation uses one shared component for many fixed conversion pairs. That makes it a useful localization example: the arithmetic stays shared, while headings, explanations, labels, and nested paths come from explicit data rather than assumptions about how a title should translate.

Translate the Explanation, Not the Conversion Factor

The centimeter-to-feet definition identifies the source as cm, the destination as ft, and its category as length. The common unit table represents those scales as 0.01 and 0.3048 meters.

Consequently, the numerical operation is equivalent to dividing centimeters by 30.48. The result does not change just because the user changes the page language.

The localized pair data supplies the human explanation: a heading, description, formula title, formula text, frequently asked questions, and an additional introduction. For example, the English formula explains the 30.48-centimeter definition of one foot, while the component still executes the same shared length helper used by other pages.

That separation matters for maintenance. Editing translated prose should not introduce a second executable formula that can drift away from the calculation. Conversely, sharing the arithmetic does not mean every page needs identical explanatory text.

For 165 centimeters, the numerical result rounds to 5.413386 feet. That is decimal feet, not 5 feet and 41 inches: multiply the fractional part of the unrounded result by 12 to get about 4.96 inches. The localization layer does not change the numeric field into a compound feet-and-inches string.

I would review formula prose against the actual unit keys and constants. A translation can be grammatically correct while accidentally describing the reverse direction, and a working calculation would not automatically reveal that content error.

Resolve Content from Both Pair and Locale

The component looks up the current pair from route metadata. It then derives the content for the active locale:

const content = computed(
  () => pair.value?.i18n[locale.value] || pair.value?.i18n.en || fallbackContent,
);
Enter fullscreen mode Exit fullscreen mode

The fallback order is explicit: requested language, English for the selected pair, and finally a placeholder object. It selects a whole content object, not individual fields. If a translation object exists but omits h1, this expression does not borrow the English heading. I'd validate required fields when maintaining the data; this lookup alone does not enforce completeness.

Because the lookup is computed, the rendered heading and explanatory content can follow a reactive locale change. Pair identity remains the same unless the route selects another slug. Falling back to English content also does not change locale.value, so it does not automatically change the URL language.

The source and destination labels are resolved separately through the category's translation namespace. A length page uses the length-unit labels, while the pair data supplies the specific unit keys. The model stores unit identity, not a translated display string that would need to be parsed later.

This is a useful general pattern for localized numeric applications. Stable keys belong in the computation; translated names belong at the presentation boundary. Treating the displayed word for “feet” as the internal identifier would make a language switch unnecessarily risky.

There is a limit to the fallback guarantee. It covers the content lookup, not every expression in the component. Some metadata calculations assume that the router supplied a valid pair, so this is not a general unknown-route recovery mechanism.

Build Nested Paths from the Pair Definition

The route slug for this page is cm-to-feet, but the complete path also contains its category hub. The helper used to derive that full slug is:

export function getPairFullSlug(pair) {
  const hubPath = categoryHubPath[pair.category] || "";
  return `${hubPath.replace(/^\//, "")}/${pair.slug}`;
}
Enter fullscreen mode Exit fullscreen mode

For a length pair, the category map supplies the length-converter prefix. The page slug alone is therefore not the complete address.

The router generates each base pair route from that full slug and stores the pair slug in route.meta.slug. It then creates English routes by appending /en to the base paths. There is no :locale route parameter in this setup.

The component's canonical computation combines the full pair slug with the site origin and the active locale suffix. Traditional Chinese uses the unsuffixed path; English and the other configured locale codes use a suffix. The content lookup does not itself navigate between those routes.

I would not derive that suffix from a visible language name or assume every project uses standard browser language tags as its route codes. This site has its own locale identifiers. The URL convention is part of the application's data contract, not something Intl or the translation library guesses.

The same pair identity also feeds breadcrumbs and sibling-conversion links, but not every consumer calls this helper. The sibling component assembles its links from categoryHubPath, each sibling's slug, and a locale suffix. It shares the category map rather than the complete path builder. I'd test both constructions when changing URL conventions.

It is still necessary to verify the final head output during navigation. The client-side canonical is passed to useHead as a computed value, but htmlAttrs.lang is supplied as t("message.lang"), and alternate links are spread from alternate.value during setup. Those entries are snapshots at that call site, not reactive bindings like the canonical. I'd check a fresh page load and a locale change without remounting separately; reactive body text alone proves neither metadata behavior.

Shared Content Needs a Deliberate Boundary

The introduction can either be supplied as a complete localized pair introduction or assembled from the category introduction plus pair-specific material:

const combinedIntro = computed(() => {
  if (content.value.intro) return content.value.intro;
  const hubIntro = t(`${categoryConfig.value.i18nKey}.intro`);
  const extra = content.value.introExtra
    ? `<h3>${content.value.h1}</h3><p>${content.value.introExtra}</p>`
    : "";
  return `${hubIntro}${extra}`;
});
Enter fullscreen mode Exit fullscreen mode

That keeps general conversion background reusable while allowing the centimeter-to-feet page to explain its own use cases. It is not equivalent to changing only a title on otherwise identical content.

The assembled introduction is rendered as HTML. In this implementation, it comes from maintained application translation data; the code is not an invitation to interpolate arbitrary visitor-supplied HTML into the same path.

Localization also does not imply locale-aware numeric parsing or decimal formatting. The fields use type="number" and v-model.number; the length helper still calls Number(value) and rounds results to at most six decimal places. For example, Number("165,5") is NaN, which that helper turns into zero through its || 0 fallback. That is not a promise about what every browser lets someone type. A translated explanation and a localized numeric editing grammar are separate features.

For verification, I would check the heading, formula direction, unit labels, nested canonical path, and one known numerical result in several locales. That catches disagreements between content, routing, and computation that a screenshot of a translated title would miss.

I turned this shared localized component into small free conversion pages, including the CM to Feet Converter.

Top comments (0)