Next.js plugin + i18n API for Next.js - Load page translations and use them in an easy way!
Next.js plugin + i18n API for Next.js - Load page translations and use them in an easy way!
Easy i18n for Next.js +10
Next plugin + i18n API
Sponsored by:
</a>
The main goal of this library is to keep the translations as simple as possible in a Next.js environment.
Next-translate has two parts: Next.js plugin + i18n API.
Features ✨
In the configuration file, you specify each page that namespaces needs:
i18n.json
{
"pages": {
"*": ["common"],
"/": ["home"],
"/cart": ["cart"],
"/content/[slug]": ["content"],
"rgx:^/account": ["account"]
}
// rest of config here...
}Read here about how to add the namespaces JSON files.
Next-translate ensures that each page only has its namespaces with the current language. So if we have 100 locales, only 1 will be loaded.
In order to do this we use a webpack loader that loads the necessary translation files inside the Next.js methods (getStaticProps, getServerSideProps or getInitialProps). If you have one of these methods already on your page, the webpack loader will use your own method, but the defaults it will use are:
getStaticProps. This is the default method used on most pages, unless it is a page specified in the next two points. This is for performance, so the calculations are done in build time instead of request time.getServerSideProps. This is the default method for dynamic pages like [slug].js or [...catchall].js. This is because for these pages it is necessary to define the getStaticPaths and there is no knowledge of how the slugs should be for each locale. Likewise, how is it by default, only that you write the getStaticPaths then it will already use the getStaticProps to load the translations.getInitialProps. This is the default method for these pages that use a HoC. This is in order to avoid conflicts because HoC could overwrite a getInitialProps.This whole process is transparent, so in your pages you can directly consume the useTranslation hook to use the namespaces, and you don't need to do anything else.
If for some reason you use a getInitialProps in your _app.js file, then the translations will only be loaded into your getInitialProps from _app.js. We recommend that for optimization reasons you don't use this approach unless it is absolutely necessary.
yarn add next-translateThe next-translate-plugin is a tool that allows developers to efficiently handle translations on a page-by-page basis during the build process. It is distinct from the next-translate package, which allows developers to access the translations in the code where it is needed. The plugin works by parsing all pages, searching for the translations and rewriting the page file adding the translations to it. This makes the plugin a more efficient and flexible solution for handling translations within a Next.js application. It is recommended to install the plugin as a devDependency.
yarn add next-translate-plugin -DIn your next.config.js file:
const nextTranslate = require('next-translate-plugin')
module.exports = nextTranslate()Or if you already have next.config.js file and want to keep the changes in it, pass the config object to the nextTranslate(). For example for webpack you could do it like this:
const nextTranslate = require('next-translate-plugin')
module.exports = nextTranslate({
webpack: (config, { isServer, webpack }) => {
return config;
}
})As of Next.js 16, Turbopack is enabled by default. Pass { turbopack: true } as the second argument so that next-translate-plugin configures the Turbopack loader instead of the webpack one:
const nextTranslate = require('next-translate-plugin')
module.exports = nextTranslate({}, { turbopack: true })Or with your existing config:
const nextTranslate = require('next-translate-plugin')
// Note: only available in Next.js +16 (for 15, you need to force a boolean, default false)
const isTurbopack = !process.argv.includes('--webpack')
module.exports = nextTranslate(
{
// your Next.js config here
},
{ turbopack: isTurbopack }
)Note: Omitting
{ turbopack: true }when using Next.js 16+ with Turbopack will cause a startup error because the plugin would otherwise inject a webpack configuration that Turbopack doesn't accept.
Add a configuration file i18n.json (or i18n.js with module.exports) in the root of the project. Each page should have its namespaces. Take a look at it in the config section for more details.
{
"locales": ["en", "ca", "es"],
"defaultLocale": "en",
"pages": {
"*": ["common"],
"/": ["home", "example"],
"/about": ["about"]
}
}In the configuration file you can use both the configuration that we specified here and the own features about internationalization of Next.js 10.
By default the namespaces are specified on the /locales root directory in this way:
/locales
.
├── ca
│ ├── common.json
│ └── home.json
├── en
│ ├── common.json
│ └── home.json
└── es
├── common.json
└── home.jsonEach filename matches the namespace specified on the pages config property, while each file content should be similar to this:
{
"title": "Hello world",
"variable-example": "Using a variable {{count}}"
}However, you can use another destination to save your namespaces files using loadLocaleFrom configuration property:
i18n.js
{
// ...rest of config
"loadLocaleFrom": (lang, ns) =>
// You can use a dynamic import, fetch, whatever. You should
// return a Promise with the JSON file.
import(`./myTranslationsFiles/${lang}/${ns}.json`).then((m) => m.default),
}Then, use the translations in the page and its components:
pages/example.js
import useTranslation from 'next-translate/useTranslation'
export default function ExamplePage() {
const { t, lang } = useTranslation('common')
const example = t('variable-example', { count: 42 })
return
//
}You can consume the translations directly on your pages, you don't have to worry about loading the namespaces files manually on each page. The next-translate plugin loads only the namespaces that the page needs and only with the current language.
In the configuration file you can use both the configuration that we specified here and the own features about internationalization of Next.js 10.
| Option | Description | Type | Default |
|---|---|---|---|
defaultLocale |
ISO of the default locale ("en" as default). | string |
"en" |
locales |
An array with all the languages to use in the project. |
No open issues yet, or sync has not completed.