Skip to content
ShadyLight

Internationalization (i18n)

Starlight provides built-in support for multilingual sites, including routing, fallback content, and full right-to-left (RTL) language support.

Configure i18n

  1. Tell Starlight about the languages you support by passing locales and defaultLocale to the Starlight integration:

    astro.config.mjs
    1
    import { defineConfig } from 'astro/config';
    2
    import starlight from '@astrojs/starlight';
    3
    4
    export default defineConfig({
    5
    integrations: [
    6
    starlight({
    7
    title: 'My Docs',
    8
    // Set English as the default language for this site.
    9
    defaultLocale: 'en',
    10
    locales: {
    11
    // English docs in `src/content/docs/en/`
    12
    en: {
    13
    label: 'English',
    14
    },
    15
    // Simplified Chinese docs in `src/content/docs/zh-cn/`
    16
    'zh-cn': {
    17
    label: '简体中文',
    18
    lang: 'zh-CN',
    19
    },
    20
    // Arabic docs in `src/content/docs/ar/`
    21
    ar: {
    22
    label: 'العربية',
    23
    dir: 'rtl',
    24
    },
    25
    },
    26
    }),
    27
    ],
    28
    });

    Your defaultLocale will be used for fallback content and UI labels, so choose the language you are most likely to start writing content in, or already have content for.

  2. Create a directory for each language in src/content/docs/. For example, for the configuration shown above, create the following folders:

    • Directorysrc/
      • Directorycontent/
        • Directorydocs/
          • Directoryar/
          • Directoryen/
          • Directoryzh-cn/
  3. You can now add content files in your language directories. Use the same file name to associate pages across languages and take advantage of Starlight’s full set of i18n features, including fallback content, translation notices, and more.

    For example, create ar/index.md and en/index.md to represent the homepage for Arabic and English respectively.

Use a root locale

You can use a “root” locale to serve a language without any i18n prefix in its path. For example, if English is your root locale, an English page path would look like /about instead of /en/about.

To set a root locale, use the root key in your locales config. If the root locale is also the default locale for your content, remove defaultLocale or set it to 'root'.

astro.config.mjs
1
import { defineConfig } from 'astro/config';
2
import starlight from '@astrojs/starlight';
3
4
export default defineConfig({
5
integrations: [
6
starlight({
7
title: 'My Docs',
8
defaultLocale: 'root', // optional
9
locales: {
10
root: {
11
label: 'English',
12
lang: 'en', // lang is required for root locales
13
},
14
'zh-cn': {
15
label: '简体中文',
16
lang: 'zh-CN',
17
},
18
},
19
}),
20
],
21
});

When using a root locale, keep pages for that language directly in src/content/docs/ instead of in a dedicated language folder. For example, here are the home page files for English and Chinese when using the config above:

  • Directorysrc/
    • Directorycontent/
      • Directorydocs/
        • index.md
        • Directoryzh-cn/
          • index.md

Monolingual sites

By default, Starlight is a monolingual (English) site. To create a single language site in another language, set it as the root in your locales config:

astro.config.mjs
1
import { defineConfig } from 'astro/config';
2
import starlight from '@astrojs/starlight';
3
4
export default defineConfig({
5
integrations: [
6
starlight({
7
title: 'My Docs',
8
locales: {
9
root: {
10
label: '简体中文',
11
lang: 'zh-CN',
12
},
13
},
14
}),
15
],
16
});

This allows you to override Starlight’s default language without enabling other internationalization features for multi-language sites, such as the language picker.

Fallback content

Starlight expects you to create equivalent pages in all your languages. For example, if you have an en/about.md file, create an about.md for each other language you support. This allows Starlight to provide automatic fallback content for pages that have not yet been translated.

If a translation is not yet available for a language, Starlight will show readers the content for that page in the default language (set via defaultLocale). For example, if you have not yet created a French version of your About page and your default language is English, visitors to /fr/about will see the English content from /en/about with a notice that this page has not yet been translated. This helps you add content in your default language and then progressively translate it when your translators have time.

Translate Starlight’s UI

In addition to hosting translated content files, Starlight allows you to translate the default UI strings (e.g. the “On this page” heading in the table of contents) so that your readers can experience your site entirely in the selected language.

and we welcome contributions to add more default languages.

You can provide translations for additional languages you support — or override our default labels — via the i18n data collection.

  1. Configure the i18n data collection in src/content/config.ts if it isn’t configured already:

    src/content/config.ts
    1
    import { defineCollection } from 'astro:content';
    2
    import { docsSchema, i18nSchema } from '@astrojs/starlight/schema';
    3
    4
    export const collections = {
    5
    docs: defineCollection({ schema: docsSchema() }),
    6
    i18n: defineCollection({ type: 'data', schema: i18nSchema() }),
    7
    };
  2. Create a JSON file in src/content/i18n/ for each additional locale you want to provide UI translation strings for. For example, this would add translation files for Arabic and Simplified Chinese:

    • Directorysrc/
      • Directorycontent/
        • Directoryi18n/
          • ar.json
          • zh-CN.json
  3. Add translations for the keys you want to translate to the JSON files. Translate only the values, leaving the keys in English (e.g. "search.label": "Buscar").

    These are the English defaults of the existing strings Starlight ships with:

    Starlight’s code blocks are powered by the Expressive Code library. You can set translations for its UI strings in the same JSON file using expressiveCode keys:

    1
    {
    2
    "expressiveCode.copyButtonCopied": "Copied!",
    3
    "expressiveCode.copyButtonTooltip": "Copy to clipboard",
    4
    "expressiveCode.terminalWindowFallbackTitle": "Terminal window"
    5
    }

    Starlight’s search modal is powered by the Pagefind library. You can set translations for Pagefind’s UI in the same JSON file using pagefind keys:

    1
    {
    2
    "pagefind.clear_search": "Clear",
    3
    "pagefind.load_more": "Load more results",
    4
    "pagefind.search_label": "Search this site",
    5
    "pagefind.filters_label": "Filters",
    6
    "pagefind.zero_results": "No results for [SEARCH_TERM]",
    7
    "pagefind.many_results": "[COUNT] results for [SEARCH_TERM]",
    8
    "pagefind.one_result": "[COUNT] result for [SEARCH_TERM]",
    9
    "pagefind.alt_search": "No results for [SEARCH_TERM]. Showing results for [DIFFERENT_TERM] instead",
    10
    "pagefind.search_suggestion": "No results for [SEARCH_TERM]. Try one of the following searches:",
    11
    "pagefind.searching": "Searching for [SEARCH_TERM]..."
    12
    }

Extend translation schema

Add custom keys to your site’s translation dictionaries by setting extend in the i18nSchema() options. In the following example, a new, optional custom.label key is added to the default keys:

src/content/config.ts
1
import { defineCollection, z } from 'astro:content';
2
import { docsSchema, i18nSchema } from '@astrojs/starlight/schema';
3
4
export const collections = {
5
docs: defineCollection({ schema: docsSchema() }),
6
i18n: defineCollection({
7
type: 'data',
8
schema: i18nSchema({
9
extend: z.object({
10
'custom.label': z.string().optional(),
11
}),
12
}),
13
}),
14
};

Learn more about content collection schemas in “Defining a collection schema” in the Astro docs.