Keeping Localised Next.js Pages Attached to the Right Content

An English page about the old station lives at /stories/old-station. Its French translation lives at /fr/stories/ancienne-gare. A language switch which simply puts /fr in front of the current address sends the reader to a page that doesn't exist.
The locale changed successfully. Unfortunately, the reader wanted the story as well.
Next.js can handle the locale prefix, but it doesn't know that old-station and ancienne-gare describe the same piece of content. That relationship belongs in our data. I want to make it explicit before building the switcher, because it also determines which pages we generate and which alternate URLs we advertise.
Give the Story an Identity of Its Own
In my example, the station has a stable ID and a separate record for each translation:
{
id: 'station',
translations: {
en: { slug: 'old-station', published: true },
fr: { slug: 'ancienne-gare', published: true },
},
}The complete records also contain a title and body. The ID joins them together; the slug provides a readable address in that language.
I wouldn't use the English slug as the ID. An editor should be able to rename an English URL without quietly breaking the relationship with every translation. Old addresses may need redirects, but that's a separate decision from deciding which French article belongs to this English one.
Some CMSs represent this with localised fields on one entry. Others use separate entries linked to a shared record. Either arrangement can work. What matters here is being able to answer two questions: which story is this, and which published translations does it have?
My article on using CMS data with getStaticProps covers fetching the content. For this example, a small local collection makes the translation relationships easy to inspect.
Let Next Handle the Locale Prefix
I'm using Next 10.2.3 with its built‑in internationalised routing:
module.exports = {
i18n: {
locales: ['en', 'fr'],
defaultLocale: 'en',
localeDetection: false,
},
};English uses the unprefixed address, and French uses /fr. I've disabled automatic locale detection so that these checks start with an explicit URL choice. Language negotiation and separate domains can be useful, but neither fixes a missing relationship between translated records.
The route file is pages/stories/[slug].js. There's no [locale] folder: Next supplies the locale to the data‑fetching function. Its internationalised routing documentation describes that part of the arrangement.
For static generation, I enumerate the published translation of each story, passing both its slug and locale to getStaticPaths. The station contributes two paths. The harbour story, whose French translation is still a draft, contributes one.
const translation = publishedTranslation(story, locale);
return translation
? [{ params: { slug: translation.slug }, locale }]
: [];That's the inner step of the enumeration, applied across the stories and configured locales. My getStaticPaths guide explains the surrounding function. Here I use fallback: false: a translation absent from the generated collection is a missing page, not an invitation to invent one.
The lookup in getStaticProps must use the locale and slug together. Searching for a slug alone would let one language's address accidentally select another language's record. I also return notFound: true when that lookup finds no published translation, rather than passing an empty object to the page.
Switch to the Translated Address
Once the page has resolved its content ID, the switcher looks up the target translation belonging to that same ID. It then gives Next the translated slug and the target locale:
<Link
href={{
pathname: '/stories/[slug]',
query: { slug: other.slug },
}}
locale={otherLocale}
>
<a hrefLang={otherLocale} lang={otherLocale}>
{names[otherLocale]}
</a>
</Link>This is the Next 10 form of Link, including its child anchor. other is already a published translation, not merely an entry in the list of supported languages.
I use language names, including Français, rather than flags. The link leads somewhere, so it remains an ordinary keyboard‑accessible link. It doesn't need a button which asks JavaScript to assemble an address after the click.
My second translated story uses marché-du-dimanche. The data retains the accented slug; URLs encode it when necessary. I want a direct request for that address to find the same record as a client‑side transition. An example containing only convenient ASCII slugs would leave that unchecked.
Decide What an Absent Translation Means
For the harbour story, the French record exists but isn't published. I show a short unavailable‑translation message instead of a French link. Requests for its unpublished French slug return 404.
That is my policy for these editorial pages. Another site might offer a clearly labelled link to the English version. What I wouldn't do is silently serve the English body under a French address and announce it as French in the document metadata.
The download includes that mistake deliberately: a fallback page returns English harbour content with a French document language and a French alternate URL. It looks more accommodating than a missing page, until we inspect what it is claiming.
Keeping a draft record out of the path list isn't enough if the page lookup later supplies a default‑language fallback. Generation and lookup need to apply the same publication rule. Adding a translation also means rebuilding this static example; changing the local collection alone doesn't change an already‑running production build.
Check the Relationships in the HTML
Each published page has a canonical URL for its own language. The English station doesn't canonicalise to the French station, or vice versa. They are separate translated pages.
Both pages also advertise the same pair of hreflang alternates, including themselves. I derive those links from the published translations of the resolved story. The harbour page therefore has an English alternate only; a configured French locale is not evidence that this particular story exists in French.
Next supplies the document's language from the selected locale. I still inspect the actual html element, title, canonical, and alternate links alongside the visible heading. A convincing heading can sit above quite misleading metadata.
My checks load every published address directly with JavaScript disabled, then exercise the language links in the browser. Switching English to French must preserve the content ID, change the slug, and update the metadata. Back and Forward must restore the corresponding language and address, and refreshing the French page must still work.
I also keep the naive locale‑only switch as a failing comparison. It reaches /fr/stories/old-station and the missing‑page screen. The corrected link instead resolves the French slug belonging to the station record.
The useful relationship is between the stories, not between matching pieces of URL text. Once that relationship is recorded properly, the switcher, generated pages, and alternate links can all describe the same set of published translations.
Postscript
November 2026: My downloadable example retains Next 10.2.3 and React 17 so that its routing matches the article. I've added production‑build and browser checks using a current Node 22, Yarn 4, and Chrome harness; those tool versions are documented in the README. The newer App Router handles locale segments differently, so its route files aren't a direct replacement for this configuration. The content relationship still needs to be explicit whichever router you use.