本文介绍了ng2-翻译customLoader&每种语言多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ionic2应用程序中,我正在使用ng2-转换应用程序中的字符串.

In an Ionic2 app I'm using ng2-translate the strings in my app.

现在,我需要将翻译文件分为每种语言的多个文件,例如"de.json"和"de_gs1ais.json".由于ng2-translate每种语言仅限于一个文件,因此我尝试实现自定义加载程序:

Now I need to split the translation file into several files per language, for example "de.json" and "de_gs1ais.json". As ng2-translate is limited to one file per language I've tried to implement a custom loader:

class CustomLoader implements TranslateLoader {
  private http: Http;
  public getTranslation(lang: String): Observable<any> {
    return this.http.get("assets/i18n" + "/" + lang + ".json").map((res) => res.json());

  }
}

该类实现了标准的ng2-translate行为,而对于以下类,我希望可以加载2个文件de.json和de_gs1ais.json:

This class implements the standard ng2-translate behaviour, while with the following class I'd expect the 2 files de.json and de_gs1ais.json to be loaded:

class CustomLoader implements TranslateLoader {
  private http: Http;
  public getTranslation(lang: String): Observable<any> {
    return this.http.get("assets/i18n" + "/" + lang + ".json").merge(
    this.http.get("assets/i18n" + "/" + lang + "_gs1ais.json")).map((res) => res.json());

  }
}

问题在于再次仅加载了第一个文件(de.json).更改代码中可观察对象的顺序会导致另一个文件被加载.我认为合并"应将可观察对象合并在一起,并用所有数据创建一个大"可观察流.

The problem is that again only the first file (de.json) is loaded. Changing the order of the observables in the codes result in the other file being loaded.I thought "merge" should merge together the observables, creating one "big" observable stream with all the data.

任何人都可以帮忙吗?

谢谢

推荐答案

您需要的是forkJoin而不是合并.

What you need is a forkJoin instead of the merge.

合并不会合并数据- https://www.learnrxjs.io/operator/combination/merge.html

forkJoin可以- https://www.learnrxjs.io/operators/combination/forkjoin.html

forkJoin does - https://www.learnrxjs.io/operators/combination/forkjoin.html

 public getTranslation(lang: String): Observable<any> {
    let languageObservables = Rx.Observable.forkJoin(              
          this.http.get("assets/i18n" + "/" + lang + ".json"),
          this.http.get("assets/i18n" + "/" + lang + "_gs1ais.json")
    );
    return languageObservables.map((res) => res.json())
  }

这篇关于ng2-翻译customLoader&amp;每种语言多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 07:01