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.

Override current language with useTranslation

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
stukennedycommented, Jun 2, 2021

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.

import React, { useEffect, useState } from 'react';
import { I18nextProvider } from 'react-i18next';

import i18n from './i18n';
import TranslateMe from './TranslateMe';

const Wrapper = () => {
  const [i18next, seti18next] = useState(i18n.cloneInstance());

  useEffect(() => {
    const instance = i18n.cloneInstance();
    seti18next(instance);
  }, []);

  return (
    <I18nextProvider i18n={i18next}>
      <TranslateMe />
    </I18nextProvider>
  );
};
export default Wrapper;
import React from 'react';
import { useTranslation } from 'react-i18next';

const TranslateMe = () => {
  const { t, i18n } = useTranslation();

  const onChangeValue = (event) => {
    i18n.changeLanguage(event.target.value);
  }

  return (
    <>
      <h1>{t('my-translation-key')}</h1>
      <div onChange={onChangeValue}>
        <input type="radio" value="en-GB" name="languageType" /> English
        <input type="radio" value="fr-FR" name="languageType" /> French
        <input type="radio" value="nl-NL" name="languageType" /> Dutch
      </div>
    </>
  );
};
export default TranslateMe;
1reaction
bittttttencommented, Jun 2, 2020

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:

Screenshot 2020-06-02 at 15 04 24

Right now I am just importing the raw language file and bypassing this library but it would be great to have it supported.

Read more comments on GitHub >

github_iconTop 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 >

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