-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
returnObjects: true returns keys rather than the translation #240
Comments
Ok so replacing Setting the This can probably be closed unless you can think of a different workaround than using |
a) not giving a namespace in translate HOC will result in defaulting to b) passing resources on init - is not the best idea - you will need to include all languages you provide (use the xhr-plugin to load from your server or http://locize.com as a backend) => pass it like:
else it will find nothing and fallback to keys (or fallbackNS) honestly i'm wondering why your sample iterates over the array even if passed without lng. for acting different when using t vs i18n.t directly...will need to test that...should have same behaviour...will fix that asap if different |
Thanks for the reply and sorry for the confusion - my example is wrong - in fact my real code is as you describe (keyed by language not by namespace), I was just writing from memory. I should probably explain this is server side, for email rendering with ReactDOMServer, so I just load all required languages and namespaces from JSON files (it's all bundled into a single js file with webpack so saves time loading it later). It's a problem specific to Looking at this code https://github.com/i18next/i18next/blob/118e47d569650b3c6d81b9afe24c5c4b01d051fb/src/i18next.js#L221-L231 It seems that So when the underlying i18n.t is called the params are something like I guess one solution is to add the namespace to the ns array when it is extracted: https://github.com/i18next/i18next/blob/118e47d569650b3c6d81b9afe24c5c4b01d051fb/src/Translator.js#L65 At L65 we have the correct namespace but it isn't passed to Hope that just about makes sense! i18next.t('app1:blah.someString', { lng: null, ns: [] }); // works (returns 'This is some string')
i18next.t('app1:blah.someArray', { lng: null, ns: [], returnObjects: true }); // fails (returns ['app1:blah.someArray0', 'app1:blah.someArray1', ...] tl;drWas about to post the reply but then realised the issue is here: https://github.com/i18next/i18next/blob/118e47d569650b3c6d81b9afe24c5c4b01d051fb/src/Translator.js#L99
This should work I think (assuming joinArrays should always be false here): copy[m] = this.translate(`${key}${keySeparator}${m}`, {...options, ...{joinArrays: false, ns: namespaces}}); |
think yes...should be correct. applied your fix to i18next@7.1.3 |
Working perfectly now, thanks @jamuhl :) |
thanks belongs to you - for finding and fixing... |
I will preface this by saying I'm probably doing this wrong with regards to the translate function - I have been wrapping my components as follows:
Not providing any namespaces works fine for any plain namespace and translation. When using
returnObjects: true
it stops working and just returns an object or array of keys.I get that I'm supposed to be specifying namespaces in
translate(['ns1','ns2'])
but is there a technical reason it not to work without, given that it does work for returning strings. Would just like to avoid having to specify 6 or 7 namespaces every time I want an object in a component.For clarification:
ns1.json
index.js
component.js
Gives the following html:
The text was updated successfully, but these errors were encountered: