-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useTranslation - override language #1637
Comments
workaround: const { i18n } = useTranslation('ns1');
const t = i18n.getFixedT('en'); Does this work for you? |
I had actually thought about that - my uncertainty was around if that |
And yeah as far as we're currently concerned, we would load translation files upfront - we wouldn't be using any other special backends for these component libraries. |
ok, try v12.3.0 this should work: const i18n = i18next.createInstance(...);
const MySharedComponent = ({ lng }) => {
cons { t } = useTranslation('ns1', { i18n, lng });
return t('foo');
}; |
not a fan of that change...might work for this scenario...but all others (using this not serverside with backends) will complain: cons { t } = useTranslation('ns1', { lng });
// -> i get missings....why useTranslation does not assert lng get loaded?!? |
…vent missings when lazy loading translations #1637
react-i18next v12.3.1 + i18next v22.5.0 should also address that |
@adrai the result of these changes is that the
Is this intentional change? I thought |
There is no guarantee resolvedLanguage is always defined, but can you please provide a minimal reproducible example showing resolvedLanguage to be undefined? Just tested in a normal client side environment, and at least there it seems to be defined. |
Or do you mean this? i18next/i18next#1960 |
Yes, sorry about that |
🚀 Feature Proposal
Allow
useTranslation
to take an optionallng
prop to override the language passed togetFixedT
(link).Motivation
In a server-side rendering (SSR) environment, there is no single language, since a single server could handle different requests in different languages. This requires you to manage multiple
i18n
instances, one per language, in order for auseTranslation
somewhere down the react tree to work. While something like next-i18next may handle this for you, we have use-cases for shared component libraries that may execute in both SSR and client-side environments, which requires to hand-roll some code for synchronizing language between app and library, and initializing ani18n
instance (or multiple) for the component library to use (with its own translation files).To correctly support SSR in the component library, this seems to require creating an
i18n
instance per supported language, and initializing each of them with the translation files, so that the correct one for the given request's language can be passed down via context. However if we could instead just pass down, say, the current language via context, we could have just a singlei18n
instance, and we could wrapuseTranslation
in a hook that passes that language to it.Example
Simple example:
How this might be used in a component lib (let's keep it simple and say our component takes a
lng
prop itself):The text was updated successfully, but these errors were encountered: