Skip to main content
Framework

Localization

Root.js has built-in support for locale-aware URLs, translated strings, and localized CMS content.

Configuration

Set the site's locales and URL format with the i18n option in root.config.ts:

ts
// @/root.config.ts

import {defineConfig} from '@blinkk/root';

export default defineConfig({
  i18n: {
    locales: ['en', 'ja'],
    defaultLocale: 'en',
    urlFormat: '/[locale]/[path]',
  },
});

Each route is then served at a URL per locale, e.g. /ja/about. Use fallbacks to set which locales a missing translation falls back to.

Translations

Translated strings live in translations/, one JSON file per locale, mapping each source string to its translation:

json
// @/translations/fr.json

{
  "Hello, world!": "Bonjour le monde !",
  "Hello, {name}!": "Bonjour {name} !"
}

useTranslations()

useTranslations() returns a function that translates a string into the current locale. Values in curly braces are replaced with the params you pass.

tsx
// @/routes/index.tsx

import {useTranslations} from '@blinkk/root';

export default function Page(props) {
  const t = useTranslations();
  if (props.name) {
    return <h1>{t('Hello, {name}!', {name: props.name})}</h1>;
  }
  return <h1>{t('Hello, world!')}</h1>;
}

Translating CMS content

Schema fields marked with translate: true are sent for translation. Editors translate them in the CMS, with AI, or by exporting them to a spreadsheet or translation vendor.

Translations are stored in the CMS. createRoute() loads them for each page, and cmsClient.loadTranslationsForLocale() loads them anywhere else. Pass them to ctx.render() to use them with useTranslations(). See Data fetching.

For content that differs by locale but isn't a translation, like a localized image or link, use localized values.

Translations manager v2 (experimental)

The v2 translations manager stores translations directly on each doc by locale, so drafts and publishes stay in sync with the doc itself. It's experimental and off by default. To try it, enable the flag in cmsPlugin():

ts
cmsPlugin({
  experiments: {v2TranslationsManager: true},
});

The first time you start the dev server or run root build after opting in, the CMS copies your v1 translations into the new format while leaving the v1 data intact as a backup. You can also run the migration manually with root-cms translations.migrate (pass --status to check if it has already run).

The migration check runs before each build, so with the v2 translations manager, root build needs Firestore access. Make sure your CI has application default credentials, e.g. gcloud auth application-default login or GOOGLE_APPLICATION_CREDENTIALS.

1
2
3
4
5
6
7
8
9
10
11
12
Breakpoint: