I'm using next-i18next to handle internationalization of my next.js app.
Is it possible to change default language dynamically? For example based on the top level domain?
const defaultLanguage = topLevelDomain === "de" : "de" ? "it";
EDIT: I'm also using localeSubpaths so that's why I'm trying to investigate the topic.
For someone who use Nextjs
v10.0.0
up as written here, we have to use the newest configurations.next-i18next.config.js
next.config.js
And to change the language we have to use
next/link
andnext/router
:But you have to bear in mind, that:
At first render, the default language will always refer to the
Accept- Language
header sent by the browser. In other words, the default language will be based on your target user's browser language settings.Let say that
Accept-Language
header sent by the browser is as follows (bothde
andit
exists):then the default language will be
German
, ignoring the configurationdefaultLocale
atnext-i18next.config.js
.If both
de
andit
not listed inAccept-Language
for example:then the default language will be
Italian
following the configuration we made.For development purposes, we can change the browser language settings (I use google chrome) at
chrome://settings/?search=language
and order the languages based on your preference.We can set the default language for the next render by programmatically add
NEXT_LOCALE
cookies to the target user's browser, based on their selection. Example:Always check the documentation to get the newest update.