# 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](/search) index, `llms.txt` and [RSS feed](/seo#rss-feed), and the [sitemap](/seo#sitemap) pairs the pages with the same path.

## Setup [#setup]

`npm create vuesho -- --i18n` creates a site with two languages. To add languages to an existing site, follow these steps.

### Define the Languages [#define-the-languages-step]

List the languages with `defineI18n()` from `vuesho/i18n`, and pass them to `createSite()` as `i18n`:

```ts title="vuesho.config.ts"
import { createSite, recommendedPreset } from "vuesho/config";
import { defineDocs } from "vuesho/content";
import { defineI18n } from "vuesho/i18n"; // [!code ++]

const docs = defineDocs();

// [!code ++:5]
const i18n = defineI18n({
  languages: ["en", "cn"],
  defaultLanguage: "en",
  hideLocale: "default-locale",
});

export default createSite(docs.toSource(), { i18n }).plugins(recommendedPreset()); // [!code highlight]
```

### I18nConfig

| Prop                | Type                                                   | Description                                                                                          |
| ------------------- | ------------------------------------------------------ | ---------------------------------------------------------------------------------------------------- |
| `languages`         | `string[]`                                             | The site's languages.                                                                                |
| `defaultLanguage`   | `string`                                               | The language of a file or URL without one, and for a reader whose languages the site lacks.          |
| `hideLocale?`       | `"always" \| "default-locale" \| "never" \| undefined` | The languages without a prefix in their URLs: none, the default one, or all. Default: `"never"`      |
| `parser?`           | `"dot" \| "dir" \| "none" \| undefined`                | How a file names its language: `page.cn.md`, `cn/page.md`, or not at all. Default: `"dot"`           |
| `fallbackLanguage?` | `string \| null \| undefined`                          | The language a page without a translation shows, or `null` for none. Default: `the default language` |
| `cookieName?`       | `string \| undefined`                                  | The cookie that stores the reader's language, with `hideLocale: "always"`. Default: `"FD_LOCALE"`    |

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](/markdown/collections#multiple-collections), pass `i18n` after the object of sources:

```ts title="vuesho.config.ts"
createSite(
  { docs: docs.toSource(), blog: blog.toSource({ baseDir: "blog" }) },
  { i18n }, // [!code highlight]
);
```

### Name the Languages [#name-the-languages-step]

`site.languages` sets each language's label in the menu, the site name in that language, and its [language tag](https://www.rfc-editor.org/info/bcp47) when the key isn't one:

```ts title="vuesho.config.ts"
createSite(docs.toSource(), { i18n }).config({
  site: {
    name: "Acme Docs",
    languages: {
      en: { label: "English" },
      cn: { label: "中文", lang: "zh-CN", name: "Acme 文档" }, // [!code highlight]
    },
  },
});
```

An entry of `site.languages` takes these fields:

### LanguageConfig

| Prop           | Type                  | Description                                                                                   |
| -------------- | --------------------- | --------------------------------------------------------------------------------------------- |
| `label?`       | `string \| undefined` | The language's name in the language switch. Default: ``the `displayName` text, then the key`` |
| `lang?`        | `string \| undefined` | The language tag, when the key isn't one. Default: `the key`                                  |
| `name?`        | `string \| undefined` | The localized site name. Default: `` `site.name` ``                                           |
| `description?` | `string \| undefined` | The localized site description. Default: `` `site.description` ``                             |
| `link?`        | `string \| undefined` | Where the language switch leads: a path or another site. Default: `the page in this language` |

The language tag sets `<html lang>`, the `hreflang` links and the UI's [language pack](#language-packs). 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()`](/client-api)'s `dir`.

#### External Languages [#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:

```ts title="vuesho.config.ts"
createSite(docs.toSource()).config({
  site: {
    languages: {
      en: { label: "English" },
      ru: { label: "Русский (community)", link: "https://ru.example.com" },
    },
  },
});
```

### Translate the Pages [#translate-the-pages-step]

Put a translation beside its page, with the language key before the extension, like `install.cn.md`. Meta files are translated the same way:

```files
content
├── index.md
├── index.cn.md
└── guide
    ├── meta.json
    ├── meta.cn.json
    ├── install.md
    └── install.cn.md
```

With `hideLocale: "default-locale"`, `guide/install.md` is at `/guide/install`, and `guide/install.cn.md` at `/cn/guide/install`.

## Page Translations [#page-translations]

### File Names [#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:

```files
content
├── en
│   ├── index.md
│   └── guide
│       └── install.md
└── cn
    ├── index.md
    └── guide
        └── install.md
```

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](/content-sources) whose files set `locale`.

### Fallback Language [#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 [#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 [#links-in-translated-pages]

A [relative link](/markdown#links) to a file goes to the page in the same language. A link to a URL, like `/cn/guide/install`, stays as written:

```md title="content/guide/index.cn.md"
<!-- Links to /cn/guide/install. -->

[安装](./install.md)
```

In a component, build a page's URL in a language with `useSite()`'s `localizePath()`:

```vue title="src/components/InstallLink.vue"
<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 [#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 [#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 [#hiding-every-prefix]

With `hideLocale: "always"`, every language has the same URLs, and the server reads each request's language from, in order:

1. The cookie that the language menu sets: `FD_LOCALE`, or the name in `cookieName`.
2. The `Accept-Language` header.
3. The default language.

A URL with a prefix, like `/cn/guide/install`, sets the cookie to `cn` and redirects to `/guide/install`.

<Callout type="warn" title="Server Required">
A static host can't read the cookie, so the pages render on request and the site needs a [server](/deploying).
</Callout>

A page has one URL for all its languages, so the sitemap lists it once, without `hreflang` links.

### Navbar 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:

```ts title="vuesho.config.ts"
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]

`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:

```ts title="vuesho.config.ts"
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:

```ts title="vuesho.config.ts"
createSite(docs.toSource()).config({
  ui: {
    translations: { "On this page(table of contents)": "Contents" }, // [!code highlight]
  },
});
```

### Language Packs [#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:

```ts title="vuesho.config.ts"
createSite(docs.toSource(), { i18n }).config({
  site: { languages: { cn: { lang: "zh-CN" } } },
  ui: {
    translations: { cn: { "Search(search dialog)": "搜索文档" } }, // [!code highlight]
  },
});
```

A site with one language uses the pack for `site.lang`.
