#TopBar switch language without using state
1 messages · Page 1 of 1 (latest)
To set a default language for your Next.js application, you can use the i18n property in your next.config.js file. Here's an example of how you can set English as the default language and also support German and Polish:
module.exports = {
i18n: {
locales: ['en', 'de', 'pl'],
defaultLocale: 'en',
},
}
In this configuration, 'en' is the default locale, and 'de' and 'pl' are other supported locales.
If you want to allow users to switch languages, you can leverage the NEXT_LOCALE cookie. Next.js supports overriding the accept-language header with a NEXT_LOCALE=the-locale cookie. This cookie can be set using a language switcher. When a user comes back to the site, it will leverage the locale specified in the cookie when redirecting from / to the correct locale location.
For example, if a user prefers the locale de in their accept-language header but a NEXT_LOCALE=en cookie is set, when visiting / the user will be redirected to the en locale location until the cookie is removed or expired.