Getting Started
There are two configuration files that are necessary for ni18n
to work as expected in most cases. The nextjs.config.js
and a file to hold ni18n
's own configuration. Then you will need the translations files for your application and to setup your pages/_app
file accordingly in order to be able to use the translations in your application.
1. Next.js config
Setup the next config file according to your needs by following the documentation on next.js i18n-routing
2. Ni18n config
The ni18n
config works pretty much as an i18next
config does. With a few minor differences listed in the F.A.Q..
But the basic setup is pretty simple.
i18next >= 21 The first item in the
ns
array will be set as yourdefault
and allow you to calluseTranslation()
without any parameters to use it. If you want to manually select the default, then you can set thedefaultNS
property.
Note: The
name
andlocation
of the file don't really matter, you can call it anything and put it anywhere you want.
3. Translation Files
You will also need to provide the translation files for your application. They are expected to be on the public
folder. The default structure is /public/locales/{{lng}}/{{ns}}.json
, but it can be changed following the instructions here
Example of how your folder structure should look like.
4. Next.js App
Translations won't work without a I18nextProvider
, so we need to set it up. It is required to wrap your App
with the appWithI18Next
function provided by ni18n
. Which will initialize the i18next
instance and provide it as a context for all the children. You can read more about Next.js Custom App in their documentation.
5. Using Translations
Finally, you can call useTranslation
on your files, or any other react-i18next
component you may need.
Caveats
Client Side vs Server Side
At this point, all your translation files will be loaded on the client side, if you need them loaded on the server, read more on Server Side Loading
We also recommend to always use either loadTranslations or clientNamespaces or both in a page, else you will load all the namespaces on your application for every page.
Importing
ni18n
only exports appWithI18Next
, loadTranslations
, clientNamespaces
and useSyncLanguage
. All other functions you need can be imported from react-i18next
or i18next
directly.
Last updated