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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setDefaultLang is not working as fallback language #332

Closed
pndewit opened this issue Nov 23, 2016 · 3 comments
Closed

setDefaultLang is not working as fallback language #332

pndewit opened this issue Nov 23, 2016 · 3 comments

Comments

@pndewit
Copy link
Contributor

pndewit commented Nov 23, 2016

I'm submitting a ... (check one with "x")

[x] bug report => check the FAQ and search github for a similar issue or PR before submitting
[ ] support request => check the FAQ and search github for a similar issue before submitting
[ ] feature request

Current behavior
I use the following two calls to set the current language to French and the default (fallback) language to English.

TranslateService.setDefaultLang('en');
TranslateService.use('fr');

So if a translation is missing from the French translations file, it should fall back to the English translations file. This is not the case though.

The use function successfully adds the French translations, but the setDefaultLang is not adding the English translations. The English translations should even be the default (making the setDefaultLang call even unnecessary). I have also tried to call the addLangs function, but this is not working either.

Expected/desired behavior
I would like ng2-translate to load in all the translation files inside the i18n directory automatically to so that I wouldn't even have to use the use function and let the browser of the client decide which translations to try and use (falling back to the fallback language if it is not available).

I would expect that the "en.json" to be injected by default (unless specified otherwise by setDefaultLang) and I would also expect that the setDefaultLang to inject the given translations file.

Lastly (but I am not sure if this is according to what it is supposed to do) I would expect the addLangs function to inject the given translations. But I don't think that a list of available languages is something that ng2-translate 'should' be tracking anyway.

Reproduction of the problem
If the current behavior is a bug or you can illustrate your feature request better with an example, please provide the steps to reproduce and if possible a minimal demo of the problem via https://plnkr.co or similar (you can use this template as a starting point: http://plnkr.co/edit/tpl:btpW3l0jr5beJVjohy1Q).

http://plnkr.co/edit/4M7kUf0H8utxrF7bNskL?p=preview

What is the motivation / use case for changing the behavior?
The fallback language does not work unless the use function is called for it.

Please tell us about your environment:
Angular 2 v2.2.1 (also tried v2.2.2) with Webpack. But it is reproducible in other environments (check the Plunker linked above using your given starting point).

  • ng2-translate version:
    latest (v4.0.1)

  • Angular version:
    v2.2.1 / v2.2.2

  • Browser:
    Chrome

  • Language:
    TS

@pndewit
Copy link
Contributor Author

pndewit commented Nov 23, 2016

I could help with a PR if you like!

If anyone faces the same issue. A simple workaround is doing a use for the fallback language before setting it to the initial language (e.g. browser's language):

TranslateService.setDefaultLang('en');
TranslateService.use('en');

let browserLang = TranslateService.getBrowserLang();
TranslateService.use(browserLang.match(/en|fr/) ? browserLang : 'en');

@ocombe
Copy link
Member

ocombe commented Nov 23, 2016

I agree, this should be the case, and a PR would be awesome

@pndewit
Copy link
Contributor Author

pndewit commented Dec 5, 2016

What's the status of this PR? Found some time to review it? :)

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

No branches or pull requests

2 participants