Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing ns to config sets the defaultNS to the first item in the array #1658

Closed
JCQuintas opened this issue Sep 22, 2021 · 9 comments
Closed

Comments

@JCQuintas
Copy link

馃挜 Regression Report

Whenever you pass the ns option when initialising the i18next instance, it sets the defaultNS to the first item in the ns array. Which means useTranslation() without any namespace returns the wrong values.

I'm using the React nomeclature here, but I'm fairly sure this issue is in the default package, since react-i18next didn't change, and you can see the defaultNS is set when using the debug option.

Last working version

Worked up to version: 20.6.1

Stopped working in version: 21.0.1

To Reproduce

Steps to reproduce the behavior:

Check the sandbox, in the file src/i18n.ts we init the library, if we pass ns to it, the first item in the array will become the defaultNS.

https://codesandbox.io/s/i18next-cant-use-multiple-namespaces-r6li9

Expected behavior

I was expecting the previous behaviour, where ns was not tied to defaultNS, if this is an intended change then it should be documented on docs and migration.

@adrai adrai closed this as completed in df1249c Sep 23, 2021
@adrai
Copy link
Member

adrai commented Sep 23, 2021

This was intended, but not aware of someone defining the default "translation" namespace in ns option but not in defaultNS option.
Should be fixed with v21.0.2

@jamuhl
Copy link
Member

jamuhl commented Sep 23, 2021

just to make it more clear:

  • defautlNS -> is a global defaultNS (global pick in i18next.t if no namespace is provided in key or options)
  • ns -> defines namespaces to load (or being loaded)
  • the array of namespaces used in useTranslations -> defines needed namespaces before rendering and for convenience sets the first as default (using i18next.getFixedT)

@JCQuintas
Copy link
Author

This was intended, but not aware of someone defining the default "translation" namespace in ns option but not in defaultNS option.
Should be fixed with v21.0.2

I'm not sure I understand your point. In my head the issue doesn't seem to be that translation is missing from ns, but that previously the code below kept the defaultNS as translation, but now it doesn't. It will change the defaultNS to the first item in the ns array.

{
  resources: {
    en: { ns2, translation }
  },
  ns: ["ns2"],
}

If you check the example on codesanbox, version v21.0.2 didn't change anything

@jamuhl
Copy link
Member

jamuhl commented Sep 23, 2021

either don't set ns in i18n options, or set defaultNS too

@JCQuintas
Copy link
Author

Ok, it feels like an odd behaviour. But if that is intended, then can we have it documented on the configuration options?

@jamuhl
Copy link
Member

jamuhl commented Sep 23, 2021

What is odd? You pass in a namespace array in options only containing ["ns2"] why should the defaultNS keep a namespace you do not use based on your configuration?...(translation is not in your ns array)

@adrai
Copy link
Member

adrai commented Sep 23, 2021

@JCQuintas
Copy link
Author

What is odd? You pass in a namespace array in options only containing ["ns2"] why should the defaultNS keep a namespace you do not use based on your configuration?...(translation is not in your ns array)

Mainly because you have two config variables that sort of interact with each other. It is not a big problem now it is documented though.

Previously you could use the translation namespace without having to configure it, now you would need to if using the ns field. It is just something you need to be aware of when configuring your app.

@JCQuintas
Copy link
Author

@adrai thanks, I think that makes things clearer in case someone falls on this. 馃憤

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants