ng2-translate customLoader & multiple files per language

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 12:42:41

问题


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

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

  }
}

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

  }
}

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.

Can anyone help please?

Thanks


回答1:


What you need is a forkJoin instead of the merge.

Merge doesn't combine data - https://www.learnrxjs.io/operators/combination/merge.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())
  }


来源:https://stackoverflow.com/questions/43697709/ng2-translate-customloader-multiple-files-per-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!