Next-i18next: Multiple namespaces don't work

Created on 15 Jul 2020  路  2Comments  路  Source: isaachinman/next-i18next

when i use const { t, i18n } = useTranslation(['common', 'header']); for example, t functions don't work for the strings which is in second element of the array, if I switch places it works well. Btw both namespace is loaded in i18n.store.data object but don't work

I use

Home.getInitialProps = async () => ({
  namespacesRequired: ['header', 'common'],
});

and t function as

{t('title')} // where title is in header.json file

Most helpful comment

_EDIT: (this is in fact working as intended, see my edit bellow)_

I am having a similar problem with .withTranslation([]) HOC. Only the first namespace is working.

For example:

const Foo = ({ t }) => <p>{t('fromTest1')} ; {t('fromTest2')}</p>
export default withTranslation(['test1', 'test2'])(Foo)
// Will output "<p>Translated Test 1 ; fromTest2</p>"

whereas

const Foo = ({ t }) => <p>{t('fromTest1')} ; {t('fromTest2')}</p>
export default withTranslation(['test2', 'test1'])(Foo)
// Will output "<p>fromTest1 ; Translated Test 2</p>"

EDIT After some research it appears that from the second namespace you should prefix using the namespace name like so: namespace:your.key

typescript const Foo = ({ t }) => <p>{t('fromTest1')} ; {t('test2:fromTest2')}</p> export default withTranslation(['test1', 'test2'])(Foo) // Will output "<p>Translated Test 1 ; Translated Test 2</p>"

All 2 comments

_EDIT: (this is in fact working as intended, see my edit bellow)_

I am having a similar problem with .withTranslation([]) HOC. Only the first namespace is working.

For example:

const Foo = ({ t }) => <p>{t('fromTest1')} ; {t('fromTest2')}</p>
export default withTranslation(['test1', 'test2'])(Foo)
// Will output "<p>Translated Test 1 ; fromTest2</p>"

whereas

const Foo = ({ t }) => <p>{t('fromTest1')} ; {t('fromTest2')}</p>
export default withTranslation(['test2', 'test1'])(Foo)
// Will output "<p>fromTest1 ; Translated Test 2</p>"

EDIT After some research it appears that from the second namespace you should prefix using the namespace name like so: namespace:your.key

typescript const Foo = ({ t }) => <p>{t('fromTest1')} ; {t('test2:fromTest2')}</p> export default withTranslation(['test1', 'test2'])(Foo) // Will output "<p>Translated Test 1 ; Translated Test 2</p>"

Please read the i18next documentation. You will need to denote the second (non default) namespace with a colon separator.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jonesus picture Jonesus  路  6Comments

isaachinman picture isaachinman  路  7Comments

denny7 picture denny7  路  6Comments

ronfogel picture ronfogel  路  3Comments

slava-lu picture slava-lu  路  6Comments