问题描述
我有一个Angular 10应用程序,将为多个客户使用。每个客户可以(不是必须)拥有自己的一组翻译文本。如何实现这种多重回退? 完整的后备方案应如下所示:
[client.fr-CA]-> [client.fr]-> [client.en]->(global-fr-CA)->(global-fr)-> global-en
因此,如果在client.fr-CA中找不到文本,则将其搜索到client.fr中,依此类推,直到到达包含应用程序中所有文本的global-en。
我还尝试使用Angular自己的i18n实现,并且还尝试了ngx-translate。他们两个都只有一个后备。我什至找不到实现这种不同语言环境风格的方法:(fr-CA)->(fr)-> en。
我应该实现自己的翻译机制吗? :)
谢谢!
解决方法
没有一个好的方法,但是我会向您提出一些建议,以便您可以使用异步管道获得翻译的价值
想象你有这个
<p>{{ 'name' | translate }}</p>
您必须将其更改为
<p>{{name$ | async }}</>
并根据您的条件在您的控制器中获取翻译价值,假设顺序为fr-> en-> de
name$: Observable<string> = this.translate.getTranslation(fr).pipe(
switchMap(frTranslations=>
iif(() => !!frTranslations['name'] // check here if exist,of(frTranslations['name']),this.translate.getTranslation(en).pipe(
switchMap(enTranslations=>
iif(() => !!enTranslations['name'] // check here if exist,of(enTranslations['name']),this.translate.getTranslation(de) ...
)
)
)
)
)
);
它需要更多的重构,但是我希望您能理解,它的意思是检查翻译是否确实存在,如果是,则获取值,如果不是,则移至下一个调用