Adding Korean and English to a static Next.js blog (with hreflang)
On this page
Every post on this blog goes out in Korean and English. I wanted English readers to find it through search, and translated pages alone don't get you there. Search engines need to be told that two pages are the same article in different languages. Otherwise one of them can get treated as a duplicate.
The catch was that this blog is a static export, so there's no middleware. Nearly every Next.js i18n example detects the language in middleware, so I had to work around that piece by piece.
URLs: /ko/ and /en/
Multilingual sites usually pick subpaths (/ko/posts/a/), subdomains (ko.example.com) or a query string (?lang=en). Query strings are hard for search engines to tell apart, so that was out. Subdomains meant deploying each language separately, which felt like overkill. Subpaths it was.
I settled this before publishing anything, because changing the URL scheme later means whatever ranking the old URLs earned has to be rebuilt on the new ones.
Language as the first path segment
With the App Router, every page lives under app/[locale]/. The list of languages is fixed at build time, so the layout returns it from generateStaticParams.
// app/[locale]/layout.tsx
export const dynamicParams = false;
export function generateStaticParams() {
return [{ locale: "ko" }, { locale: "en" }];
}That layout renders <html lang={locale}>, so every page gets the right lang attribute. The top-level app/layout.tsx is an empty pass-through, because the <html> tag has to come from the per-language layout.
Same file name in both languages
content/
ko/nextjs-static-i18n-hreflang.md
en/nextjs-static-i18n-hreflang.mdThis turned out to be the most useful decision of the lot. If the file names (slugs) match, "does this post have an English version?" is just "does the file exist?". There's no separate table of translations to keep in sync.
export function getAvailableLocales(slug: string) {
return locales.filter((l) => getAllPosts(l).some((p) => p.slug === slug));
}A post that hasn't been translated yet simply shows up in one language only.
hreflang
The tag that ties the two pages together is <link rel="alternate" hreflang="...">. In Next.js you put the URLs in alternates.languages inside generateMetadata.
export async function generateMetadata({ params }) {
const { locale, slug } = await params;
const languages = Object.fromEntries(
getAvailableLocales(slug).map((l) => [l, `/${l}/posts/${slug}/`]),
);
return {
alternates: { canonical: `/${locale}/posts/${slug}/`, languages },
};
}Two things are easy to get wrong here.
The links have to go both ways. If the Korean page points to the English one but not the other way round, search engines ignore the pair. Generating both sides from the same function keeps them in step.
The canonical URL must be the page itself. It's tempting to point the English page's canonical at the Korean original. That tells Google the English page is a copy and gets it dropped from the index, which defeats the whole point of writing it.
The sitemap says the same thing
Adding alternates.languages to each entry in app/sitemap.ts writes xhtml:link elements into the sitemap. Now the relationship is declared in two places: the page head and the sitemap.
entries.push({
url: `${site.url}/${locale}/posts/${post.slug}/`,
alternates: {
languages: Object.fromEntries(
getAvailableLocales(post.slug).map((l) => [l, `${site.url}/${l}/posts/${post.slug}/`]),
),
},
});Picking a language without middleware
That left the question of where to send someone who just types blog.manunlab.com. With middleware you'd read Accept-Language on the server. Without it, I made the root page a tiny HTML file that redirects in the browser.
var l = (navigator.languages && navigator.languages[0]) || navigator.language || "";
location.replace(/^ko/i.test(l) ? "/ko/" : "/en/");I used location.replace rather than setting location.href so the back button doesn't land on the root page and bounce you forward again. For visitors with JavaScript off there's a meta refresh to /ko/ inside <noscript>. The root page also carries hreflang="x-default", which marks it as the entry point for people with no language preference.
The language switcher 404'd
The EN/KO button in the header started out as a plain link that swapped /ko for /en in the current URL. On a post without an English version, that went straight to a 404. Now the switcher gets the list of slugs for each language and falls back to the other language's home page when the post doesn't exist there. Tag pages had the same problem, since tag names differ between languages (배포 vs deployment), so those fall back to the tag index.
With this structure, adding a post is just creating two files with the same name.
Manun
I write down what I learn by building things and breaking them. I build and run this blog on my own, too.
About →