question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redirecting to dynamic translated path not working as expected when changing language

See original GitHub issue

Describe the bug

I created a language switcher component to use different languages, which can be accessed globally inside my app. Also there are different dynamic paths in my application that can change according to the selected language. E.g. the FAQ page can be accessed with /help-contact/faq (english version) and /hilfe-kontakt/faq (german version). If the language is now getting changed on a dynamic page like the FAQ page, the new dynamic path should be read from the translation files and the new url should be pushed accordingly:

import { useTranslation } from "next-i18next";
import { useRouter } from 'next/router';

const CountrySwitch = () => {
  const { t } = useTranslation();
  const router = useRouter();

  const onSelect = () => {
    const pathname = router.pathname;
    const asPath = t('faqPath', { lng: selectedLocale });

    router.push(
        {
          pathname,
        },
        asPath,
        { locale: selectedLocale }
    );
  }

  return (
    <div>
      <Button
        key="submit"
        onClick={onSelect}
      >
        Select new language
      </Button>
    </div>
  )
}

Unfortunately t('faqPath', { lng: selectedLocale }) is still getting the old language path string instead of the new one. But when ommiting the router.push() statement at the end of the function everything works as expected and the correct translation is being returned.

IMPORTANT: This bug only happens when changing the language from the default one to any other language. When changing it back to the default language, everything works as expected again.

Is there any known error when using the useTranslation hook from next-i18next module to have dynamic language paths combined with the useRouter from next?

Occurs in next-i18next version

^11.0.0

Steps to reproduce

See code above

Expected behaviour

When changing the language the new path translation should be retrieved according to the selected language and then pushed.

OS (please complete the following information)

  • Device: MBP 2018 16"
  • Browser: Chrome 101.0.4951.64

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
isaachinmancommented, May 30, 2022

That is not a minimal reproduction.

It always returns the translation for en

That’s because your de/common.json file is empty.

Please seek help on StackOverflow.

0reactions
isaachinmancommented, May 30, 2022

That is most likely because the data for selectedLocale is not actually loaded, because it is different than the current locale.

next-i18next only sends the current locale and current namespaces down to the client. If we sent all namespaces or all locales, there wouldn’t be much point in an i18n package.

I would suggest keeping your localised route data in a simple JavaScript object, but if you really want to treat it as localised content, you will need to call i18n.addResource, or perhaps addResourceBundle, before you try to translate into a locale besides the current one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to translate location URL when changing language?
My issue is when I change the language, the url does not follow (which is to expect, since i18next doesn't talk directly to...
Read more >
RFC: I18n Routing · Discussion #17078 · vercel/next.js - GitHub
I find it unfortunate that translating non dynamic route segments is not part of this RFC. It is a hugely important feature (for...
Read more >
Using language detection and dynamic machine translation in ...
If the detected language is enabled for dynamic translation in Virtual Agent Chat Settings, Virtual Agent asks the user if they want to...
Read more >
Redirects from aliased paths aren't triggered - Drupal
When I create a new redirect from /node/1 to /node/2 it works fine but when I use path alias e.g. /foo to /node/2...
Read more >
Advanced Features: Internationalized Routing - Next.js
When a user visits the application root (generally / ), Next.js will try to automatically detect which locale the user prefers based on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found