Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
N

next-translate

> 前端框架
Open source

Next.js plugin + i18n API for Next.js - Load page translations and use them in an easy way!

2.7K stars0 likes4 views
WebsiteGitHub

About

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>

  • 1. About next-translate
    • How are translations loaded?
  • 2. Getting started
    • Install
    • Add next-translate plugin
    • Add i18n.js config file
    • Create your namespaces files
    • Use translations in your pages
  • 3. Configuration
  • 4. API
    • useTranslation
    • createTranslation
    • withTranslation
    • Trans Component
    • DynamicNamespaces
    • getT
    • I18nProvider
    • appWithI18n
    • loadNamespaces
  • 5. Plurals
  • 6. Use HTML inside the translation
  • 7. Nested translations
  • 8. Fallbacks
  • 9. Formatter
  • 10. How to change the language
  • 11. How to save the user-defined language
  • 12. How to use multi-language in a page
  • 13. How to use next-translate in a mono-repo
  • 14. Use Next 13 app directory
  • 15. Demos
    • Demo from Next.js
    • Basic demo
    • Complex demo
    • With app directory demo
    • Without the webpack loader demo
  • Contributors ✨

1. About next-translate

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 ✨

  • ・ Automatic page optimization (pages dir).
  • ️ ・ React 18 server/client pages/components (app dir).
  • ・ Easy to use and configure.
  • ・ Basic i18n support: interpolation, plurals, useTranslation hook, Trans component...
  • ️ ・ It loads only the necessary translations (for page and for locale).
  • ・ Tiny (~1kb) and tree shakable. No dependencies.

How are translations loaded?

In the configuration file, you specify each page that namespaces needs:

i18n.json

javascript
{
  "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.

2. Getting started

Install

  • yarn add next-translate

Add next-translate plugin

The 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 -D

In your next.config.js file:

javascript
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:

javascript
const nextTranslate = require('next-translate-plugin')

module.exports = nextTranslate({
  webpack: (config, { isServer, webpack }) => {
    return config;
  }
})

Using Turbopack (Next.js 16+)

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:

javascript
const nextTranslate = require('next-translate-plugin')

module.exports = nextTranslate({}, { turbopack: true })

Or with your existing config:

javascript
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 i18n.js config file

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.

json
{
  "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.

Create your namespaces files

By default the namespaces are specified on the /locales root directory in this way:

/locales

bash
.
├── ca
│   ├── common.json
│   └── home.json
├── en
│   ├── common.json
│   └── home.json
└── es
    ├── common.json
    └── home.json

Each filename matches the namespace specified on the pages config property, while each file content should be similar to this:

json
{
  "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

javascript
{
  // ...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),
}

Use translations in your pages

Then, use the translations in the page and its components:

pages/example.js

javascript
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.

3. Configuration

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.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScriptglobalizationhacktoberfesti18nlocalization

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架