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.

Parent react components stay unchanged when using I18n.changeLanguage(lng)

See original GitHub issue

Describe the bug It in some way similar to #611 May be a bug, but may be i’m doing something wrong.

Occurs in react-i18next version 10.11.4

To Reproduce I create language switcher component. Use it in root component. In all other components I use Trans and useTranslation. After language switch only swticher component is updated. But, if I use changeLanguage directly in root component, app will be rerendered as expected. Should I use I18nextProvider or something?

import React, {useCallback} from 'react';
import {useTranslation} from 'react-i18next';

const LanguageSwitcher = ({className}) => {
  const {i18n} = useTranslation();

  const setEnglish = useCallback(() => {
    i18n.changeLanguage('en');
  }, [i18n]);

  const setRussian = useCallback(() => {
    i18n.changeLanguage('ru');
  }, [i18n]);

  return (
    <div className={className}>
      <button
        onClick={setEnglish}
      >
        English
      </button>
      <button
        onClick={setRussian}
      >
        Русский
      </button>
    </div>
  );
};

export default LanguageSwitcher;

Expected behaviour All components, that use useTranslation should recive new t and/or i18n and rerender when language switched.

OS (please complete the following information):

  • Device: Ubuntu 18.04
  • Browser: Yandex.Browser 19.4.2.698 beta (64-bit)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jamuhlcommented, Jul 24, 2019

downside -> when using multiple namespaces t can be bound to a namespace

it gets a t function -> it just does not get any update on languageChanged…that’s “all” (beside having a t function bound to global configured defaultNamespace)

1reaction
jamuhlcommented, Jul 24, 2019

The Trans component does not get a magical rerender on languageChange -> only hook, HOC get that, so correct would be passing t to the Trans component (Trans component does only enable translation of JSX nodes - nothing more):

import React from 'react';
import { Trans, useTranslation } from 'react-i18next';

const Content: React.FC = () => {
  const {t} = useTranslation();
  return (
    <div>
      <Trans
        t={t}
        i18nKey={`content`}
        defaults='My content'
      />
    </div>
  );
};

export default Content;
Read more comments on GitHub >

github_iconTop Results From Across the Web

i18next/i18n Change Language not working with the whole ...
I can't seem to get the whole site running on a different language. My index.tsx file import React from 'react'; import ReactDOM from...
Read more >
useTranslation() Hook - React Deep Dive - Educative.io
However, using the useTranslation() Hook often simplifies the component and makes it ... const changeLanguage = (lng) => {. 8. i18n.changeLanguage(lng);.
Read more >
[Solved]-Detect browser's language with i18next-browser ...
lng =es as well as when using a browser extension that let me change the browser language. Heres my working i18n.ts file: import...
Read more >
How to properly internationalize a ... - DEV Community ‍ ‍
'bold' : 'normal' }} type="submit" onClick={() => i18n.changeLanguage(lng)}> {lngs[lng] ...
Read more >
youtube-danmaku - Codul sursă - Greasy Fork
{ // By explicitly using `prop-types` you are opting into new production ... the creation of the component, // children are handled after...
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