0

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 });
  };
4
  • 1
    How is changeLanguage invoked? Commented Jul 12, 2022 at 20:39
  • Hi @adrai yes, changeLanguage is invoked . I managed to fix the undefined matter by lifting up my locale in my layout component then pass it as props to my navbar . However it is still not changing the language. There is one this that i have noticed different from localhost is that the lang tag in the html is not changing in production. :) Commented Jul 12, 2022 at 20:47
  • one more thing i have tried to add the the route /en-us in production it returns error 404 Commented Jul 12, 2022 at 21:15
  • solved here: github.com/i18next/next-i18next/issues/1910 Commented Jul 14, 2022 at 9:47

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

it doesn't work
Great Illia! I was forced to use the Link but it wasn't the best choice for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.