transloco: Language fallback doesn't work properly
I’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-CNfailed. getNextLangs()returns['zh-Hans', 'zh', 'en'].failedCounteris 0. It will try to loadzh-Hans. And increasefailedCounterto 1- Load
zh-Hansfailed getNextLangs()returns['zh', 'en'], becausehandleFailurereceiveszh-Hansas parameter instead ofzh-Hans-CN.failedCounteris 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:
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 3
- Comments: 23 (12 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 inNextLangsin 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. 😄
@zhongsp hey! Really busy times for me, I’ll try to get to it this week, but no promises 🙏
Once I’ll get some time I’ll address it!