Core: setDefaultLang is not working as fallback language

Created on 23 Nov 2016  路  3Comments  路  Source: ngx-translate/core

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

help wanted feature request

Most helpful comment

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');

All 3 comments

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');

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

louisdoe picture louisdoe  路  3Comments

jstoup111 picture jstoup111  路  4Comments

rbaumi picture rbaumi  路  4Comments

bjornharvold picture bjornharvold  路  3Comments

guysan picture guysan  路  4Comments