An astro integration of i18next + some utility components to help you translate your astro websites!
Note
Status - π§ Beta
You can use it, and feedback is more than welcome! Note that some breaking changes may still be introduced during this phase as the goal for v1 is to get the best possible DX for translating your Astro pages.
Example | Status |
---|---|
SSG - Basics | |
SSR - Node | |
React | |
SSR - Netlify | (yassinedoghri#26) |
SSR - Deno | (yassinedoghri#55) |
- Examples
- π Getting started
- π» CLI commands
- π Translate Routes
- π¦ Utility components
- π¦ Utility functions
- π Going further
- β¨ Contributors
- β€οΈ Acknowledgments
- π License
npm install astro-i18next i18next
or
pnpm add astro-i18next i18next
or
yarn add astro-i18next i18next
-
Add
astro-i18next
to yourastro.config.mjs
:import { defineConfig } from "astro/config"; import astroI18next from "astro-i18next"; export default defineConfig({ integrations: [astroI18next()], });
-
Configure
astro-i18next
in yourastro-i18next.config.mjs
file:/** @type {import('astro-i18next').AstroI18nextConfig} */ export default { defaultLocale: "en", locales: ["en", "fr"], };
βΉοΈ Your
astro-i18next
config file can be a javascript (.js
|.mjs
|.cjs
) or typescript (.ts
|.mts
|.cts
) file.βΉοΈ For a more advanced configuration, see the AstroI18nextConfig props.
-
By default,
astro-i18next
expects your translations to be organized inside your astro'spublicDir
, in alocales
folder:public βββ locales # create this folder to store your translation strings βββ en | βββ translation.json βββ fr βββ translation.json
βΉοΈ
astro-i18next
loads your translation files both server-side and client-side using i18next-fs-backend and i18next-http-backend plugins.βΉοΈ You may choose to organize your translations into multiple files instead of a single file per locale using namespaces.
You may now start translating your pages by using
i18next's t
function or the
Trans component depending on your needs.
Here's a quick tutorial to get you going:
-
Use translation keys in your Astro pages
--- // src/pages/index.astro import i18next, { t } from "i18next"; import { Trans, HeadHrefLangs } from "astro-i18next/components"; --- <html lang={i18next.language}> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width" /> <title>{t("site.title")}</title> <meta name="description" content={t("site.description")} /> <HeadHrefLangs /> </head> <body> <h1>{t("home.title")}</h1> <p> <Trans i18nKey="home.subtitle"> This is a <em>more complex</em> string to translate, mixed with <strong >html elements </strong> such as <a href="https://example.com/">a cool link</a>! </Trans> </p> </body> </html>
// public/locales/en/translation.json { "site": { "title": "My awesome website!", "description": "Here is the description of my awesome website!" }, "home": { "title": "Welcome to my awesome website!", "subtitle": "This is a <0>more complex</0> string to translate, mixed with <1>html elements</1>, such as a <2>a cool link</2>!" } }
// public/locales/fr/translation.json { "site": { "title": "Mon super site web !", "description": "Voici la description de mon super site web !" }, "home": { "title": "Bienvenue sur mon super site web !", "subtitle": "Ceci est une chaine de charactères <0>plus compliquée</0> à traduire, il y a des <1>éléments html</1>, comme <2>un super lien</2> par exemple !" } }
-
Create localized pages using the generate command
npx astro-i18next generate
-
You're all set! Have fun translating and generate localized pages as you go π
Note
For a real world example, see the website or check out the examples.
npx astro-i18next generate
This command will generate localized pages depending on your config and set i18next's language change on each page.
For instance, with locales = ["en", "fr", "es"]
, and "en"
being the default
locale and having:
src
βββ pages
βββ about.astro
βββ index.astro
π Running npx astro-i18next generate
will create the following pages
src
βββ pages
βββ es
| βββ about.astro
| βββ index.astro
βββ fr
| βββ about.astro
| βββ index.astro
βββ about.astro
βββ index.astro
astro-i18next
let's you translate your pages routes for each locale!
For instance, with support for 3 locales (en
, fr
, es
), en
being the
default and the following pages:
src
βββ pages
βββ about.astro
βββ contact-us.astro
βββ index.astro
-
Set route mappings in your
astro-i18next
config:/** @type {import('astro-i18next').AstroI18nextConfig} */ export default { defaultLocale: "en", locales: ["en", "fr", "es"], routes: { fr: { "about": "a-propos", "contact-us": "contactez-nous", "products": { "index": "produits", "categories": "categories", } } es: { "about": "a-proposito", "contact-us": "contactenos", "products": { "index": "productos", "categories": "categorias", } } }, };
-
Generate your localized pages using the generate CLI command, they will be translated for you!
src
βββ pages
βββ es
| βββ productos
| | βββ categorias.astro
| | βββ index.astro
| βββ a-proposito.astro
| βββ contactenos.astro
| βββ index.astro
βββ fr
| βββ produits
| | βββ categories.astro
| | βββ index.astro
| βββ a-propos.astro
| βββ contactez-nous.astro
| βββ index.astro
βββ products
| βββ categories.astro
| βββ index.astro
βββ about.astro
βββ contact-us.astro
βββ index.astro
Note
The localizedPath and localizeUrl utility functions will retrieve the correct route based on your mappings.
A component that takes care of interpolating its children with the translation strings. Inspired by react-i18next's Trans component.
---
import { Trans } from "astro-i18next/components";
---
<Trans i18nKey="superCoolKey">
An <a href="https://astro.build" title="Astro website">astro</a> integration of
<a href="https://www.i18next.com/" title="i18next website">i18next</a> and utility
components to help you translate your astro websites!
</Trans>
// fr.json
{
"superCoolKey": "Une intΓ©gration <0>astro</0> d'<1>i18next</1> + quelques composants utilitaires pour vous aider Γ traduire vos sites astro !"
}
Prop name | Type (default) | Description |
---|---|---|
i18nKey | ?string (undefined) | Internationalization key to interpolate to. Can contain the namespace by prepending it in the form 'ns:key' (depending on i18next.options.nsSeparator). If omitted, a key is automatically generated using the content of the element. |
ns | ?string (undefined) | Namespace to use. May also be embedded in i18nKey but not recommended when used in combination with natural language keys. |
Unstyled custom select component to choose amongst supported locales.
---
import { LanguageSelector } from "astro-i18next/components";
---
<LanguageSelector showFlag={true} class="my-select-class" />
<!-- LanguageSelector with custom language naming -->
<LanguageSelector
showFlag={true}
languageMapping={{ en: "Anglais" }}
class="my-select-class"
/>
Prop name | Type (default) | Description |
---|---|---|
showFlag | ?boolean (false ) |
Choose to display the language emoji before language name |
languageMapping | ?object (undefined ) |
Rewrite language names by setting the locale as key and the wording of your choice as value |
HTML tags to include in your page's <head>
section to let search engines know
about its language and region variants. To know more, see
Google's advanced localized versions.
---
import i18next from "i18next";
import { HeadHrefLangs } from "astro-i18next/components";
---
<html lang={i18next.language}>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>...</title>
<meta name="description" content="..." />
<HeadHrefLangs />
</head>
<body>...</body>
</html>
The HeadHrefLangs component will generate all of the alternate links depending on the current url and supported locales.
For example, if you are on the /about
page and support 3 locales (en
, fr
,
es
) with en
being the default locale, this will render:
<link rel="alternate" hreflang="en" href="https://www.example.com/about/" />
<link rel="alternate" hreflang="fr" href="https://www.example.com/fr/about/" />
<link rel="alternate" hreflang="es" href="https://www.example.com/es/about/" />
interpolate(i18nKey: string, reference: string, namespace: string | null): string
astro-i18next
exposes the logic behind the Trans component, you may want to
use it directly.
import { interpolate } from "astro-i18next";
const interpolated = interpolate(
"superCoolKey",
'An <a href="https://astro.build" title="Astro website">astro</a> integration of <a href="https://www.i18next.com/" title="i18next website">i18next</a> and utility components to help you translate your astro websites!'
);
localizePath(path: string, locale: string | null = null, base: string = import.meta.env.BASE_URL): string
Sets a path within a given locale. If the locale param is not specified, the current locale will be used.
Note
This should be used instead of hard coding paths to other pages. It will take care of setting the right path depending on the locale you set.
---
import { localizePath } from "astro-i18next";
import i18next from "i18next";
i18next.changeLanguage("fr");
---
<a href={localizePath("/about")}>...</a>
<!-- renders: <a href="/fr/about">...</a> -->
localizeUrl(url: string, locale: string | null = null, base: string = import.meta.env.BASE_URL): string
Sets a url within a given locale. If the locale param is not specified, the current locale will be used.
Note
This should be used instead of hard coding urls for internal links. It will take care of setting the right url depending on the locale you set.
---
import { localizeUrl } from "astro-i18next";
import i18next from "i18next";
i18next.changeLanguage("fr");
---
<a href={localizeUrl("https://www.example.com/about")}>...</a>
<!-- renders: <a href="https://www.example.com/fr/about">...</a> -->
i18next allows you to organize your translation keys into namespaces.
You can have as many namespaces as you wish, have one per page and one for common translation strings for example:
public
β-- locales
| |-- en
| | |-- about.json # "about" namespace
| | |-- common.json # "common" namespace
| | β-- home.json # "home" namespace
| β-- fr # same files in other locale folders
src
β-- pages
|-- about.astro
β-- index.astro
-
It can easily be setup using the
namespaces
anddefaultNamespace
keys, like so:/** @type {import('astro-i18next').AstroI18nextConfig} */ export default { defaultLocale: "en", locales: ["en", "fr"], namespaces: ["about", "common", "home"], defaultNamespace: "common", };
-
Load the namespace globally using
i18next.setDefaultNamespace(ns: string)
or specify it in thet
function or theTrans
component:--- import { t, setDefaultNamespace } from "i18next"; import { Trans } from "astro-i18next/components"; setDefaultNamespace("home"); --- <h1>{t("myHomeTitle")}</h1> <p> <Trans i18nKey="myHomeDescription"> This translation is loaded from the default <strong>home</strong> namespace! </Trans> </p> <p> <Trans i18nKey="myCommonCTA" ns="common"> This translation is loaded from the <strong>common</strong> namespace! </Trans> </p> <!-- t function loads the "buttonCTA" key from the "common" namespace --> <button>{t("common:buttonCTA")}</button>
astro-i18next
's goal is to abstract most of the configuration for you so that
you don't have to think about it. Just focus on translating!
Though if you'd like to go further in customizing i18next, feel free to tweak your config!
Prop name | Type (default) | Description |
---|---|---|
defaultLocale | string (undefined) |
The default locale for your website. |
locales | string[] (undefined) |
Your website's supported locales. |
namespaces | string or string[] ('translation') |
String or array of namespaces to load. |
defaultNamespace | string (translation') |
Default namespace used if not passed to the translation function. |
load | Array<"server" or "client"> (["server"] ) |
Load i18next on server side only, client side only or both. |
i18nextServer | ?InitOptions |
The i18next server side configuration. See i18next's documentation. |
i18nextServerPlugins | ?{[key: string]: string} ({} ) |
Set i18next server side plugins. See available plugins. |
i18nextClient | ?InitOptions |
The i18next client side configuration . See i18next's documentation. |
i18nextClientPlugins | ?{[key: string]: string} ({} ) |
Set i18next client side plugins. See available plugins. |
routes | [segment: string]: string or object ({} ) |
The translations mapping for your routes. See translate routes. |
showDefaultLocale | boolean (false ) |
Whether or not the defaultLocale should show up in the url just as other locales. |
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
This wouldn't have been possible without the awesome work from the Locize and Astro teams.
Inspired by some of the greatly thought-out i18n implementations:
Code released under the MIT License.
Copyright (c) 2022-present, Yassine Doghri (@yassinedoghri)