Override current language with useTranslation
See original GitHub issueI’m not sure if there’s currently a way to do this but it would be interesting if there was a way to override the language set as a useTranslate option.
For an example, const [ translateToEnglish ] = useTranslate("reports", { lng: 'en'})
.
It would load the reports namespace in english, and that t
function would always translate to english, rather than the browser’s language (in the case you are using a language detector).
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
useTranslation (hook) - react-i18next documentation
useTranslation params · Loading namespaces · Overriding the i18next instance · Optional keyPrefix option · Not using Suspense.
Read more >how do I change language in react-i18next - Stack Overflow
Try this, pass the language inside the function to invoke different languages. import React from 'react' import { useTranslation } from ...
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 >React localization with i18next - LogRocket Blog
It accepts a key that looks up the translation object and returns the string that matches the key for the current language.
Read more >Translation features - i18next
Hint: i18next provides the functionality for all languages. You can override the existing rules via 'i18n.pluralExtension.addRule(lng, obj)'
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You can achieve this by cloning the instance of i18next and feeding it to a provider that encapsulates the components you want to treat differently. I had a similar use-case; a site running in one language, and a component that the user can switch the language on independently of the site. This approach worked excellently for me, where
i18n
is a module that creates the instance of i18next.I wanted to piggy back on this since I have a use case that I don’t think I can solve with the above links. We have a language switcher, were we always want to display the users language rather than the site’s language so we’d like to force a language with useTranslation to display the user’s language.
For example, on the Asos website you can visit the German website for ASOS, and if you visit whilst being in The Netherlands you get presented with a message in Dutch:
Right now I am just importing the raw language file and bypassing this library but it would be great to have it supported.