Internationalization
Serve your site in multiple languages.
A site can serve its pages in multiple languages, with a menu in the navbar to switch between them. Each language has its own URLs, sidebar, UI texts, search index, llms.txt and RSS feed, and the sitemap pairs the pages with the same path.
Setup
npm create vuesho -- --i18n creates a site with two languages. To add languages to an existing site, follow these steps.
Define the Languages
List the languages with defineI18n() from vuesho/i18n, and pass them to createSite() as i18n:
import { createSite, recommendedPreset } from "vuesho/config";
import { defineDocs } from "vuesho/content";
import { defineI18n } from "vuesho/i18n";
const docs = defineDocs();
const i18n = defineI18n({
languages: ["en", "cn"],
defaultLanguage: "en",
hideLocale: "default-locale",
});
export default createSite(docs.toSource(), { i18n }).plugins(recommendedPreset()); Prop
Type
A language key, like cn, names the language in file names and URLs. The config accepts only these keys, so a jp key in ui.translations is a type error. With multiple sources, pass i18n after the object of sources:
createSite(
{ docs: docs.toSource(), blog: blog.toSource({ baseDir: "blog" }) },
{ i18n },
);Name the Languages
site.languages sets each language's label in the menu, the site name in that language, and its language tag when the key isn't one:
createSite(docs.toSource(), { i18n }).config({
site: {
name: "Acme Docs",
languages: {
en: { label: "English" },
cn: { label: "中文", lang: "zh-CN", name: "Acme 文档" },
},
},
});An entry of site.languages takes these fields:
Prop
Type
The language tag sets <html lang>, the hreflang links and the UI's language pack. An invalid tag, like zh_CN, fails when the site starts.
A right-to-left language, like ar or he, sets <html dir="rtl">, and the layouts mirror. In a component, read the direction from useSite()'s dir.
External Languages
A key of site.languages that isn't one of the site's languages is an external language: a language on another site, like a community translation. The language switch lists it after the site's own languages, and it has no pages. Only label and link apply; without a link, the site fails to start.
This works without i18n too. An English site can list its Russian translation hosted elsewhere, and its URLs stay as they are:
createSite(docs.toSource()).config({
site: {
languages: {
en: { label: "English" },
ru: { label: "Русский (community)", link: "https://ru.example.com" },
},
},
});Translate the Pages
Put a translation beside its page, with the language key before the extension, like install.cn.md. Meta files are translated the same way:
With hideLocale: "default-locale", guide/install.md is at /guide/install, and guide/install.cn.md at /cn/guide/install.
Page Translations
File Names
parser sets where a file's path holds its language. With parser: "dir", each language has a folder instead of a key in the file name:
With "dir" and baseDir: "docs", content/cn/guide.md is at /cn/docs/guide, since the language folder comes before baseDir. "none" reads no language from the path, for a source whose files set locale.
Fallback Language
When a page has no translation, its URL in that language shows the page in the default language. fallbackLanguage sets another language to show, and null responds with a 404 instead.
The fallback copy has noindex and a canonical link to the page it shows, and stays out of the sitemap, the RSS feeds and llms.txt.
Shared Pages
A file with $ instead of the language key, like changelog.$.md or meta.$.json, is shared by every language. With parser: "dir", the shared folder is content/$/. Search engines index a shared page in the fallback language only.
Links in Translated Pages
A relative link to a file goes to the page in the same language. A link to a URL, like /cn/guide/install, stays as written:
<!-- Links to /cn/guide/install. -->
[安装](./install.md)In a component, build a page's URL in a language with useSite()'s localizePath():
<script setup lang="ts">
import { Link, useSite } from "vuesho/client";
const { lang, localizePath } = useSite();
</script>
<template>
<Link :href="localizePath(lang, '/guide/install')">Install</Link>
</template>URLs
hideLocale sets which languages have a URL prefix, as guide.md's URLs show:
hideLocale | URL in en | URL in cn | Redirect |
|---|---|---|---|
"never" (default) | /en/guide | /cn/guide | /guide to the reader's language |
"default-locale" | /guide | /cn/guide | /en/guide to /guide, on a server |
"always" | /guide | /guide | /cn/guide to /guide, setting the cookie |
A language's root without an index page, like /cn, redirects to the first page in its sidebar.
Language Detection
With hideLocale: "never", the server redirects a URL without a prefix to the language that the Accept-Language header prefers, or else to the default language. A header language matches a site language with the same tag, a more specific one or a more general one: zh and zh-CN both match zh-CN, while zh-TW doesn't.
On a static host, a script in the browser redirects / the same way, and without JavaScript, / redirects to the default language.
Hiding Every Prefix
With hideLocale: "always", every language has the same URLs, and the server reads each request's language from, in order:
- The cookie that the language menu sets:
FD_LOCALE, or the name incookieName. - The
Accept-Languageheader. - The default language.
A URL with a prefix, like /cn/guide/install, sets the cookie to cn and redirects to /guide/install.
Server Required
A static host can't read the cookie, so the pages render on request and the site needs a server.
A page has one URL for all its languages, so the sitemap lists it once, without hreflang links.
Navbar Links
ui.layoutProps can be a function that receives the language and returns its navbar links. this.localizePath() adds the language prefix to a URL:
createSite(docs.toSource(), { i18n }).config({
ui: {
layoutProps({ lang }) {
const text = lang === "cn" ? "版本" : "Releases";
return { links: [{ text, url: this.localizePath(lang, "/releases") }] };
},
},
});UI Translations
ui.translations replaces UI texts in each language. A key is the English text followed by its place in the UI in parentheses, like Search(search dialog). Keys from the UI and the plugins are typed:
createSite(docs.toSource(), { i18n }).config({
ui: {
translations: {
cn: {
"Search(search trigger)": "搜索",
"Search(search dialog)": "搜索",
"On this page(table of contents)": "本页内容",
"Copy Markdown(page actions)": "复制 Markdown",
},
},
},
});A site without i18n sets the texts without a language key:
createSite(docs.toSource()).config({
ui: {
translations: { "On this page(table of contents)": "Contents" },
},
});Language Packs
The UI and the plugins have texts in these languages:
- Japanese (
ja) - Simplified Chinese (
zh-CN) - Traditional Chinese (
zh-TW)
A language uses the pack that matches its tag, or a shorter prefix of it, so ja-JP uses ja. ui.translations replaces single texts in a pack:
createSite(docs.toSource(), { i18n }).config({
site: { languages: { cn: { lang: "zh-CN" } } },
ui: {
translations: { cn: { "Search(search dialog)": "搜索文档" } },
},
});A site with one language uses the pack for site.lang.