Question: Multiple 'useTranslations' calls in the same component
See original GitHub issueI 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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Related StackOverflow Question
Sounds good!
Yep, I agree. For really large apps I think the automatically generated IDs from
react-intland 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.Hi @arunmenon1975,
nice to hear from you and thank you for considering
next-intlfor your app 🙂 .Having multiple
useTranslationshook 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?