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.

Use I18nextProvider with a components library

See original GitHub issue

Question about react-i18next

Hello,

I have a question about the utilization of I18nextProvider.

Here is my code :

i18next
  .use(initReactI18next)
  .init({
    resources: {
      en: {
        translation: {
          "Validate": "Validate"
        },
      },
      fr: {
        translation: {
          "Validate": "Valider"
        }
      },
      lng: "fr",
      fallbackLng: "fr",
      interpolation: {
        escapeValue: false
      }
    }
  });

i18next.changeLanguage('fr')

class App extends Component {
  render() {
    return (
     <I18nextProvider i18n={i18next}>
        <Toto/>
    </I18nextProvider>
    );
  }
}

export default App;

And my component Toto is :

class Toto extends Component {
  render() {
    const { t } = this.props;

    return (
      <p>{t('Validate')}</p>
    )
  }
}
const translatedToto = withTranslation()(Toto);
export { translatedToto as Toto };

If my Toto is in the same project that my component App it works well. But if my Toto component is in an external package, the translation doesn’t works and I have the following message in the console :

react-i18next:: You will need pass in an i18next instance by using i18nextReactModule

I am currently developing a components library so I use npm link to make the link between my components library and my test project.

So my question is the following : There is a particular way to use I18nextProvider in this specific case ? Generally, what is the best practice to use react-i18next with a components library ?

Thanks.

Damien

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:11
  • Comments:20 (8 by maintainers)

github_iconTop GitHub Comments

6reactions
bnbarakcommented, Apr 14, 2020

You can use <I18nextProvider /> to reference the same instance.

Here is my solution to refactoring the i18nInstance to a separate library (for a lerna monorepo for example)

// utils-lib.js lives outside of the front-end/app project
import { initReactI18next } from "react-i18next";

export const i18nInstance = i18n
  .use(otherThings)
  .use(initReactI18next);

  i18nInstance.init(generateI18nInitOptions(), error => {
    // handle your errors
  });
// App.tsx or App root
import { i18nInstance } from 'utils-lib';
const App = () => 
  <I18nextProvider i18n={i18nInstance}>
     <MyComp />
  </I18nextProvider>
import { useTranslation } from 'react-i18next';

const MyComp = () => {
  const { t, i18n } = useTranslation();
  return <div>{t('tag')}</div>  
}
3reactions
jamuhlcommented, Nov 10, 2019

you can transpile them…but exclude react from being included…there at anytime should be only one react instance…that’s all

Read more comments on GitHub >

github_iconTop Results From Across the Web

I18nextProvider - react-i18next documentation
The I18nextProvider does take an i18next instance via prop i18n and ... multiple i18next instances - eg. if you provide a component library...
Read more >
Use parent i18next with a React components library
I have a mono repo with an app and a components library called ui . ... Inject the i18next via the I18nextProvider and...
Read more >
How to have multiple instances of i18next for component library
Step 1: create i18next instance. In component library, we will need to create an i18next instance, and not use initReactI18next to initialize it ......
Read more >
A Guide to React Localization with i18next | Phrase
React-i18next is a powerful set of components, hooks, and plugins that sit on top of i18next. Learn how to use it to internationalize...
Read more >
How to translate your React app with react-i18next
As we want to use react-i18next to localize our application, add it to you ... all our components we have to wrap the...
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