Language fallback doesn't work properly
See original GitHub issueI’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request
[ ] Other... Please describe:
Current behavior
Custom TranslocoFallbackStrategy
not working properly. It will skip some of the fallback langs. Because the this.failedCounter
is misused. the fallbacks
variable is re-evaluate on each failure, because the parameter passed to this.fallbackStrategy.getNextLangs(lang)
is the current failed language. It’s not the original failed language. So when this.failedCounter
’s value increases to 1. It will ignore the first element in new fallbacks
.
Given the original lang zh-Hans-CN
:
- Load
zh-Hans-CN
failed. getNextLangs()
returns['zh-Hans', 'zh', 'en']
.failedCounter
is 0. It will try to loadzh-Hans
. And increasefailedCounter
to 1- Load
zh-Hans
failed getNextLangs()
returns['zh', 'en']
, becausehandleFailure
receiveszh-Hans
as parameter instead ofzh-Hans-CN
.failedCounter
is 1. It will try to loaden
. Error Here: ‘zh’ is skipped!
private handleFailure(lang: string, mergedOptions) {
const splitted = lang.split('/');
const fallbacks = mergedOptions.fallbackLangs || this.fallbackStrategy.getNextLangs(lang);
const nextLang = fallbacks[this.failedCounter];
this.failedLangs.add(lang);
Expected behavior
Given the original lang zh-Hans-CN
, and getNextLangs()
returns ['zh-Hans', 'zh', 'en']
.
Transloco should try to load files in the following order:
- zh-Hans-CN.json
- zh-Hans.json
- zh.json
- en.json
In above scenario, zh.json
will be skipped.
Minimal reproduction of the problem with instructions
For bug reports please provide the STEPS TO REPRODUCE and if possible a MINIMAL DEMO of the problem, for that you could use our stackblitz example
What is the motivation / use case for changing the behavior?
Environment
Angular version: X.Y.Z
Browser:
- [ ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
For Tooling issues:
- Node version: XX
- Platform:
Others:
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:23 (22 by maintainers)
As a fix for the current situation the getNextLangs should be called only once on the first language failure.
getNextLangs()
is only called at the first time of loading translation failed. And the langs inNextLangs
in tried one by one, until loading a translation successfully. In the meanwhile,getNextLangs()
must not be evaluated again.In the future I think that getNextLangs should be replaced with getNextLang, so the strategy will decide individually for each failed lang what should be loaded next.
@shaharkazaz
@shaharkazaz Thanks very much for replying.
This is my custom
TranslocoFallbackStrategy
:And add it to root providers:
I thought this is all I need to code to make the language fallback work. However, this is not working as I expected. 😄