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.

About the react-i18next example

See original GitHub issue

First of all thanks for the example! https://github.com/i18next/react-i18next/blob/master/example/nextjs I’m wondering what is the best practice to access t inside nested components?

For example if we have SomeComponent inside myComponent:

// pages/index.js

function myComponent({ t}) {
  return (
    <div>
      {t('welcome')}
      <p>{t('integrates_react-i18next')}</p>
      <SomeComponent>
    </div>
  );
}

would it be better to pass t as a prop to the SomeComponent or use the HOC translate()(SomeComponent).

Also, should we set:

// Passing down initial translations
// use req.i18n instance on serverside to avoid overlapping requests set the language wrong
Extended.getInitialProps = async ({ req }) => {
  if (req && !process.browser) {
    req.i18n.toJSON = () => null; // do not serialize i18next instance and send to client

    const initialI18nStore = {};
    req.i18n.languages.forEach((l) => {
      initialI18nStore[l] = req.i18n.services.resourceStore.data[l];
    });

    return {
      i18n: req.i18n, // use the instance on req - fixed language on request (avoid issues in race conditions with lngs of different users)
      initialI18nStore,
      initialLanguage: req.i18n.language,
    };
  }
  return {};
};

only in the parent (page) component or each time we use translate HOC?

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
jamuhlcommented, Jul 12, 2017

good question…even on our larger projects we never came to a real conclusion…we basically do both…with a tendence to use the hoc on bigger components while just passing down t as prop for smaller - but there is no real best practice…more a matter of taste.

Extending the getInitialProps is only needed on page level components -> all children get the i18n set to context from the wrapping translate hoc -> so inner translate hoc get the i18n instance set via context and need not getting it initially set via props.

1reaction
jamuhlcommented, Jul 13, 2017
Read more comments on GitHub >

github_iconTop Results From Across the Web

Step by step guide - react-i18next documentation
I18next is the core of the i18n functionality while react-i18next extends and glues it to react. Create a new file i18n.
Read more >
react-i18next examples - CodeSandbox
React I18next Examples. Learn how to use react-i18next by viewing and forking example apps that make use of react-i18next on CodeSandbox.
Read more >
React-i18next: Internationalization and translation ... - Lokalise
Pluralization is quite an important feature of every language. As an example, in English, we make most plural noun forms by adding an...
Read more >
How to translate your React app with react-i18next
You learn how to you can translate your React app with react-i18next. The tutorial also explains how to manage the translations json files...
Read more >
How to Internationalize a React App using i18next & Hygraph
In this post, we will take a deep dive into how to internationalize a React Application using i18next and Hygraph.
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