react-i18next icon indicating copy to clipboard operation
react-i18next copied to clipboard

any way to change namespace like i18n.changeLanguage("en") ?

Open johnantoni opened this issue 8 years ago • 6 comments

Hi, is there any way to change the namespace like i18n.changeLanguage("en")

E.g.

i18n.changeNamespace("blue-theme");

johnantoni avatar Mar 18 '18 19:03 johnantoni

no need for that:

you can do i18next.t('namespace:key') to get a key from another namespace

when using the translate hoc or I18n render prop you get a t function set to the first namespace passed in: https://react.i18next.com/components/translate-hoc.html or https://react.i18next.com/components/i18n-render-prop.html

    <I18n ns={['defaultNamespace', 'anotherNamespace']}>
      {
        (t, { i18n, t, ready }) => (
           // t will return keys of 'defaultNamespace
        )
      }
    </I18n>

you also can use: https://www.i18next.com/api.html#getfixedt to get a fixed t function

jamuhl avatar Mar 18 '18 21:03 jamuhl

thanks @jamuhl, i'll give that a go

johnantoni avatar Mar 18 '18 22:03 johnantoni

If you need to set namespace based on a per customer base (client subdomain) -> correctly that is done by using a language in form lng-script-Region-x-customerId eg. en-x-github. -x- is the marker for custom language variation.

jamuhl avatar Dec 21 '19 05:12 jamuhl

There is also setDefaultNamespace which changes the default namespace obviously.

veelenga avatar Dec 08 '21 21:12 veelenga

@veelenga

  • default -> used as default
  • useTranslation -> gets a t function bound to the namespace passed
  • getFixedT -> create a t function bound to a namespace
  • prepend namespace in from of key to use that namespace t(ns:key)

jamuhl avatar Dec 09 '21 07:12 jamuhl

https://www.i18next.com/overview/api#setdefaultnamespace

Zhuoqin avatar Jun 28 '22 17:06 Zhuoqin