Question: Multiple 'useTranslations' calls in the same component

See original GitHub issue

I have been evaluating a few intl libraries for my nextjs based project. I just came across next-intl and thus far it looks great. I especially like that it follows certain intl standard(the ICU format for example) and also the fact that it is partially based off the robust formatjs library. Loading only relevant translations for the page and locale is a cool feature and something i was looking forward to.

Before i can finalise on the messages structure i had a couple of basic doubts: will having multiple useTranslations hook calls with the same component have any negative bearing on performance? If not, can it be considered an anti-pattern or is it perfectly ok to go ahead? In the repo example each relevant component has only a single useTranslations call and thats why i had a doubt.

My current structure (page/components based translations) was created while i evaluated another library. An example:

const HomePage = () => {
  const t = useTranslations('Mainblock');
  const t1 = useTranslations('MainNavigation');
  const {locale, locales, route} = useRouter();
  const otherLocale = locales?.find((cur) => cur !== locale);
  return (
    <div>
       
      <h1> {t('heading')} </h1>
      <p>
        {t('title')}
      </p>
      <p>
        {t('welcomeMessage')}
      </p>
      <Link href={route} locale={otherLocale}>
        <a>{t1('switchLocale', {locale: otherLocale})}</a>
      </Link>

    </div>
  )
}

export function getStaticProps({locale}: GetStaticPropsContext) {
  return {
    props: {
      messages: {
        ...require(`../messages/shared/${locale}.json`),
        ...require(`../messages/home/${locale}.json`)
      },
      now: new Date().getTime()
    }
  };
}

The t1 method calls a different namespace that i have defined in the ‘shared.json’ file:

{
...
"MainNavigation": {
        "index": "Home",
        "about": "About",
        "switchLocale": "Switch to {locale, select, fr {French} en {English}}"
    },
...
}

while the ‘Mainblock’ namespace comes from ‘home.json’

{
...
    "Mainblock" : {
         "welcomeMessage": "Welcome to my site",
        "heading": "This is the heading",
        "title": "This is the title"
}
...
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
amannncommented, Apr 2, 2021

Sounds good!

Yep, I agree. For really large apps I think the automatically generated IDs from react-intl and also message descriptions can be useful – I made a note about this in the FAQ.

At least for the apps I’ve worked on so far, which tend to be more on the small to medium size as well, I was happy so far we’ve the tradeoffs of next-intl.

1reaction
amannncommented, Apr 1, 2021

Hi @arunmenon1975,

nice to hear from you and thank you for considering next-intl for your app 🙂 .

Having multiple useTranslations hook calls in the same component is perfectly fine from a performance perspective.

In the guide, there’s a section about how I recommend to structure messages. Personally I’m in favour of scoping messages to a component, but you can of course do what suits you best.

I guess if you cross reference messages from different namespaces, the issue is mostly that you have to get a bit creative with how you call your returned translation function. As mentioned in the guide, you can however also retrieve all available messages in your component and use full paths at the individual call sites. That way you only have a single hook call and a single translation function.

Does this help?

Read more comments on GitHub >

github_iconTop Results From Across the Web

TS error with multiple namespace when passing the ... - GitHub
In my opinion this is a bit dangerous, I feel like if you do specify multiple namespace to the useTranslation hook, then all...
Read more >
react-i18next with multiple translation-files - Stack Overflow
Now I want update this so that i18next would take the translation files placed in the different component-folders and their children-folders.
Read more >
Multiple Translation Files - react-i18next documentation
In order to use multiple namespaces/translation files, you need to specify it when calling useTranslation : const { t } = useTranslation(['translation', ...
Read more >
A Guide to React Localization with i18next | Phrase
React-i18next is a powerful set of components, hooks, and plugins that ... <Translation> 's t() is exactly the same as the one useTranslation()...
Read more >
Next.js - Multi Language - YouTube
In this episode we are going to make our #Nextjs and #Strapi site be multilingual.Now I'm going to show you how to make...
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