Leo
Leo

Reputation: 521

How fix language change issues in production when no error in console using next-i18next?

my changeLanguage function is not working in production. Using the debug functionality of i18n i do not have any warning, more so the language change is detected correctly each time i click on the select menu. i have log locale on the console and it returns undefined Can someone explain to me what i do wrong, please? See below my changeLanguage

 const changeLanguage = (e) => {
    const locale = e.target.value;
     i18n.changeLanguage(locale);
    router.push(router.pathname, router.asPath, { locale });
  };

Upvotes: 0

Views: 2007

Answers (1)

illia chill
illia chill

Reputation: 2056

You don't need to have a function to change the locale.

With nextjs you have router(), this fit perfectly to any cases.

Also, you have <Link> with locale property.

1st possible way - change locale with URL. So, you can .map your locales with links and then change it.

<Link href='/test' locale={router.locale} key="your_key">
 <a>test</a>
</Link>

2nd possible way - change with function

router.push("/test", null, {locale: "your_desired_locale_or_variable"})

Your locales are defined in your config file, probably you have en locale and not en-us

Upvotes: 2

Related Questions