On this page
A language switcher should take visitors to the translation of the page they are reading—not send everyone to the translated homepage or guess that the same URL path exists in every language. This guide shows how to map pages explicitly, choose a useful fallback when a translation is missing, and preserve only query parameters and fragments that make sense on the destination page.
Key Takeaways
- Map pages by a stable content identifier, not by assuming translated URLs have matching paths.
- When a translation is missing, link to a relevant page in the selected language and make the fallback clear.
- Carry query parameters or a fragment only when the destination supports their meaning.
- Use real links with clear language names, keyboard access, and appropriate language metadata.
Map translations explicitly
Localized routes often differ: /en/guides/account/ might correspond to /fr/aide/compte/. Some pages may not have a translation at all. Treat the relationship as content data rather than trying to derive a translated URL by replacing a locale prefix or copying a slug.
A content management system can provide this mapping. For a small site, a route table can do the same:
const localizedPaths = {
"account-guide": {
en: "/en/guides/account/",
fr: "/fr/aide/compte/",
},
"billing-guide": {
en: "/en/guides/billing/",
fr: null,
},
};
const languageHome = {
en: "/en/",
fr: "/fr/",
};
function getLanguageDestination(contentId, targetLanguage) {
const translatedPath = localizedPaths[contentId]?.[targetLanguage];
if (translatedPath) {
return { href: translatedPath, isTranslation: true };
}
return {
href: languageHome[targetLanguage],
isTranslation: false,
};
}
Use the current page's content ID to look up its destination. A missing or unpublished translation should be represented as missing data, not as a guessed URL. In a larger site, keep the mapping in the CMS or localization system so that publishing or removing a translation updates the switcher automatically.
Advertisement
Choose an honest, useful fallback
If the requested translation does not exist, do not link to a 404 page or imply that the destination is the same article. A language homepage is a safe default, but it may be less helpful than a translated parent section or a relevant index page. Choose the nearest available destination that is genuinely useful in that language.
Make the fallback understandable from the link text or surrounding context. For example, a switcher may list “Français” and take visitors to the French section when the current article has no French version. Do not silently send them to an unrelated page while presenting it as a translation.
If there is no useful destination in the requested language, omit that option or explain that the page is unavailable in that language. The right choice depends on the site, but every displayed link should lead somewhere intentional.
Preserve only meaningful URL state
A destination page may support some of the current URL's state, but not all of it. A search term or filter can be worth carrying over if the translated page implements the same feature. Tracking parameters usually are not useful to visitors. A fragment such as #steps should only be retained if the destination has the corresponding section; localized pages often use different heading IDs.
For example, let each translation specify which query parameters and fragment IDs it supports:
function buildLanguageHref({
origin,
targetPath,
currentUrl,
queryKeys = [],
fragmentMap = {},
}) {
const destination = new URL(targetPath, origin);
for (const key of queryKeys) {
for (const value of currentUrl.searchParams.getAll(key)) {
destination.searchParams.append(key, value);
}
}
const currentFragment = decodeURIComponent(
currentUrl.hash.slice(1)
);
const destinationFragment = fragmentMap[currentFragment];
if (destinationFragment) {
destination.hash = destinationFragment;
}
return `${destination.pathname}${destination.search}${destination.hash}`;
}
const currentUrl = new URL(window.location.href);
const href = buildLanguageHref({
origin: window.location.origin,
targetPath: "/fr/aide/compte/",
currentUrl,
queryKeys: ["view"],
fragmentMap: {
"account-settings": "parametres-du-compte",
},
});
In this example, only view is copied, and only a fragment with an explicit destination equivalent is carried over. If query parameters have different meanings across languages, use a translation-specific mapping or drop them. Configure targetPath from trusted internal route data; do not accept an arbitrary destination URL from user input.
This fragment example assumes the current URL contains a valid percent-encoded fragment. If your application accepts malformed URL input, handle decoding errors as well. The simplest safe policy is to drop an unrecognized fragment rather than preserve it blindly.
Advertisement
Build an accessible switcher
Prefer ordinary links: they work with a keyboard, can be opened in a new tab, and have a destination users can inspect. Give the navigation a clear label in the page's language, and use familiar language names such as “English” and “Français.” Avoid flags as the only language labels; a flag does not reliably identify a language.
<nav aria-label="Language">
<ul>
<li>
<a href="/en/guides/account/" lang="en" hreflang="en">
English
</a>
</li>
<li>
<a href="/fr/aide/compte/" lang="fr" hreflang="fr" aria-current="page">
Français
</a>
</li>
</ul>
</nav>
The link's lang identifies the language of its label, which helps assistive technologies pronounce it appropriately. hreflang describes the language of the linked page; it does not replace the page's own language declaration. Set the document language on every localized page, for example <html lang="fr"> on a French page. Use the appropriate language tag for the actual content, including a regional variant when needed.
Keep the switcher in a predictable location and make sure keyboard focus is visible. If the current page is included in the list, aria-current="page" can identify it. If you use a custom dropdown instead of links, it needs correct keyboard behavior, an accessible name, and screen-reader announcements; a styled list of links is often simpler.
Common Mistakes
- Replacing the locale in the path: The translated page may have a different slug, structure, or no translation.
- Sending every visitor to the language homepage: This discards useful context when a matching page exists.
- Preserving every query parameter: Parameters may be irrelevant, unsupported, or intended only for tracking.
- Copying every fragment: The destination may not contain an element with that ID, especially when headings are translated.
- Using a flag as the only label: Flags represent places, not languages, and can be ambiguous.
- Relying on the switcher to identify page language: The localized document itself still needs a correct
langattribute. - Using a clickable
divinstead of a link: It may not behave like a link for keyboard users, assistive technology, or browser features.
Best Practices
- Store translation relationships using stable content IDs, and omit unavailable translations explicitly.
- Decide the fallback per language and section; use a relevant destination and avoid presenting it as an exact translation.
- Preserve only an allowlist of query parameters that the destination understands.
- Map fragments between localized pages, or drop them when no equivalent is known.
- Render descriptive links with language names, visible keyboard focus, and suitable language metadata.
- Test pages with and without translations, repeated query parameters, unknown fragments, and keyboard-only navigation.
Conclusion
A reliable language switcher uses explicit translation data and treats missing pages as a normal case. Link visitors to the matching page when it exists, use a clear fallback when it does not, and carry URL state only when it remains meaningful in the destination language.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.