# π§ͺ astro-i18next
An [astro](https://astro.build/) integration of
[i18next](https://www.i18next.com/) + some utility components to help you
translate your astro websites!
> **Note**
>
> Status - π§ **Beta**
>
> [π **Road to v1.0.0**](https://github.com/yassinedoghri/astro-i18next/issues/19)
>
> 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.
## Examples
| Example | Status |
| ------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| [SSG - **Basics**](examples/basics) | [![example-up-badge]](examples/basics) |
| [SSR - **Node**](examples/node) | [![example-up-badge]](examples/node) |
| [**React**](examples/react) | [![example-up-badge]](examples/react) |
| [SSR - **Netlify**](examples/netlify) | [![example-down-badge]](examples/netlify) (https://github.com/yassinedoghri/astro-i18next/issues/26) |
| SSR - **Deno** | [![example-down-badge]](examples/basics) (https://github.com/yassinedoghri/astro-i18next/issues/55) |
- [Examples](#examples)
- [π Getting started](#-getting-started)
- [1. Install](#1-install)
- [2. Configure](#2-configure)
- [3. Start translating](#3-start-translating)
- [π» CLI commands](#-cli-commands)
- [generate](#generate)
- [π Translate Routes](#-translate-routes)
- [π¦ Utility components](#-utility-components)
- [Trans component](#trans-component)
- [LanguageSelector component](#languageselector-component)
- [HeadHrefLangs component](#headhreflangs-component)
- [π¦ Utility functions](#-utility-functions)
- [interpolate function](#interpolate-function)
- [localizePath function](#localizepath-function)
- [localizeUrl function](#localizeurl-function)
- [π Going further](#-going-further)
- [Namespaces](#namespaces)
- [AstroI18nextConfig Props](#astroi18nextconfig-props)
- [β¨ Contributors](#-contributors)
- [β€οΈ Acknowledgments](#οΈ-acknowledgments)
- [π License](#-license)
## π Getting started
### 1. Install
```bash
npm install astro-i18next
```
or
```bash
pnpm add astro-i18next
```
or
```bash
yarn add astro-i18next
```
### 2. Configure
1. Add `astro-i18next` to your `astro.config.mjs`:
```js
import { defineConfig } from "astro/config";
import astroI18next from "astro-i18next";
export default defineConfig({
integrations: [astroI18next()],
});
```
2. Configure `astro-i18next` in your `astro-i18next.config.mjs` file:
```js
/** @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](#astroi18nextconfig-props).
3. By default, `astro-i18next` expects your translations to be organized inside
your
[astro's `publicDir`](https://docs.astro.build/en/reference/configuration-reference/#publicdir),
in a `locales` folder:
```bash
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](https://github.com/i18next/i18next-fs-backend) and
[i18next-http-backend](https://github.com/i18next/i18next-http-backend)
plugins.
βΉοΈ You may choose to organize your translations into multiple files instead
of a single file per locale [using namespaces](#namespaces).
### 3. Start translating
You may now start translating your pages by using
[i18next's `t` function](https://www.i18next.com/overview/api#t) or the
[Trans component](#trans-component) depending on your needs.
Here's a quick tutorial to get you going:
1. Use translation keys in your Astro pages
```astro
---
// src/pages/index.astro
import i18next, { t } from "i18next";
import { Trans, HeadHrefLangs } from "astro-i18next/components";
---
{t("site.title")}
{t("home.title")}
This is a more complex string to translate, mixed with html elements
such as a cool link!
This translation is loaded from the default home namespace!
This translation is loaded from the common namespace!
```
### AstroI18nextConfig Props
`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. |
| resourcesBasePath | `?string` | Set base path for i18next resources. Defaults to `/locales`. |
| i18nextServer | `?InitOptions` | The i18next server side configuration. See [i18next's documentation](https://www.i18next.com/overview/configuration-options). |
| i18nextServerPlugins | `?{[key: string]: string}` (`{}`) | Set i18next server side plugins. See [available plugins](https://www.i18next.com/overview/plugins-and-utils). |
| i18nextClient | `?InitOptions` | The i18next client side configuration . See [i18next's documentation](https://www.i18next.com/overview/configuration-options). |
| i18nextClientPlugins | `?{[key: string]: string}` (`{}`) | Set i18next client side plugins. See [available plugins](https://www.i18next.com/overview/plugins-and-utils). |
| routes | `[segment: string]: string or object`(`{}`) | The translations mapping for your routes. See [translate routes](#-translate-routes). |
| showDefaultLocale | `boolean`(`false`) | Whether or not the defaultLocale should show up in the url just as other locales. |
## β¨ Contributors
Thanks goes to these wonderful people
([emoji key](https://allcontributors.org/docs/en/emoji-key)):
This project follows the
[all-contributors](https://github.com/all-contributors/all-contributors)
specification. Contributions of any kind welcome!
## β€οΈ Acknowledgments
This wouldn't have been possible without the awesome work from the
[Locize](https://locize.com/) and [Astro](https://astro.build/) teams.
Inspired by some of the greatly thought-out i18n implementations:
- [next-i18next](https://github.com/i18next/next-i18next)
- [react-i18next](https://github.com/i18next/react-i18next)
- [NextJS's Internationalized Routing](https://nextjs.org/docs/advanced-features/i18n-routing)
## π License
Code released under the [MIT License](https://choosealicense.com/licenses/mit/).
Copyright (c) 2022-present, Yassine Doghri
([@yassinedoghri](https://twitter.com/yassinedoghri))
[npm]: https://www.npmjs.com/package/astro-i18next
[npm-badge]: https://img.shields.io/npm/v/astro-i18next
[build]:
https://github.com/yassinedoghri/astro-i18next/actions/workflows/publish.yml
[build-badge]:
https://img.shields.io/github/actions/workflow/status/yassinedoghri/astro-i18next/publish.yml
[license]:
https://github.com/yassinedoghri/astro-i18next/blob/develop/LICENSE.md
[license-badge]:
https://img.shields.io/github/license/yassinedoghri/astro-i18next?color=blue
[contributions]: https://github.com/yassinedoghri/astro-i18next/issues
[contributions-badge]:
https://img.shields.io/badge/contributions-welcome-blueviolet.svg
[semantic-release]: https://github.com/semantic-release/semantic-release
[semantic-release-badge]:
https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
[stars]: https://github.com/yassinedoghri/astro-i18next/stargazers
[stars-badge]:
https://img.shields.io/github/stars/yassinedoghri/astro-i18next?style=social
[codecov]: https://codecov.io/gh/yassinedoghri/astro-i18next
[codecov-badge]:
https://codecov.io/gh/yassinedoghri/astro-i18next/branch/develop/graph/badge.svg?token=IFWNB6UJDJ
[example-up-badge]: https://img.shields.io/badge/status-up-brightgreen
[example-down-badge]: https://img.shields.io/badge/status-down-red