Getting Started
Last updated
Was this helpful?
Last updated
Was this helpful?
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.
Setup the next config file according to your needs by following the documentation on
The ni18n
config works pretty much as an i18next
config does. With a few minor differences .
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.
Example of how your folder structure should look like.
Finally, you can call useTranslation
on your files, or any other react-i18next
component you may need.
ni18n
only exports appWithI18Next
, loadTranslations
, clientNamespaces
and useSyncLanguage
. All other functions you need can be imported from react-i18next
or i18next
directly.
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
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 in their documentation.
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
We also recommend to always use either or or both in a page, else you will load all the namespaces on your application for every page.