我需要使用localstorage通过ng2-translate将选定的语言存储在我的Angular2应用程序中。
我发现这个类应该在localstorage中存储所选语言:
import {TranslateLoader} from "ng2-translate/ng2-translate";
import {Observable} from "rxjs/Observable";
import {Response, Http} from "angular2/http";
export class TranslateLSLoader implements TranslateLoader {
constructor(private http: Http, private prefix: string = 'i18n', private suffix: string = '.json') {}
/**
* Gets the translations from the localStorage and update them with the ones from the server
* @param lang
* @returns {any}
*/
public getTranslation(lang: string): Observable<any> {
return Observable.create(observer => {
let translations = localStorage.getItem('ng2-translate-' + lang);
if(translations) {
observer.next(JSON.parse(translations));
}
this.http.get(`${this.prefix}/${lang}${this.suffix}`)
.map((res: Response) => res.json())
.subscribe((res: any) => {
observer.next(res);
localStorage.setItem('hmx-lang-' + lang, JSON.stringify(res));
observer.complete();
});
});
}
}
由于它不起作用,我将其修改为我的angular2版本:
import {TranslateLoader} from "ng2-translate/ng2-translate";
import {Observable} from "rxjs/Observable";
import {Http, Response} from "@angular/http";
import {Injectable} from "@angular/core";
@Injectable()
export class TranslateLSLoader implements TranslateLoader {
constructor(private http: Http) {}
/**
* Gets the translations from the localStorage and update them with the ones from the server
* @param lang
* @returns {any}
*/
public getTranslation(lang: string): Observable<any> {
return Observable.create(observer => {
let translations = localStorage.getItem('ng2-translate-' + lang);
if(translations) {
observer.next(JSON.parse(translations));
}
this.http.get(`${'i18n'}/${lang}${'.json'}`)
.map((res: Response) => res.json())
.subscribe((res: any) => {
observer.next(res);
localStorage.setItem('hmx-lang-' + lang, JSON.stringify(res));
observer.complete();
});
});
}
}
现在我没有错误但是它不起作用,当我重新加载页面时,它选择了defaut语言。
我还必须在我的导入中配置ng2-translate模块:
TranslateModule.forRoot({
provide: TranslateLoader,
useClass: TranslateLSLoader
})
你知道代码有什么问题吗?
答案 0 :(得分:1)
一个迟到的答案,但我找到了你的问题,以找出如何使用本地存储实现它,但发现没有人帮助你。不知何故,我能够找到如何解决它 首先创建Translate服务对象并将语言设置到其中:
this.translate.addLangs(['en','hn','hu']);
确保您要使用的语言应该可以使用上面添加的语言集。
如下所示更改TranslateLSLoader
export class TranslateLSLoader implements TranslateLoader {
constructor(private http: Http) {}
public getTranslation(lang: string): Observable<any> {
return Observable.create(observer => {
let translations = localStorage.getItem(lang);
if(translations) {
observer.next(JSON.parse(translations));
}
});
}
}
在调用TranslateLSLoader
从localStorage获取数据之前,您需要先将数据存储到localStorage中,如下所示
localStorage.setItem(languageCode, lang_json);
现在从getTranslation
TranslateLSLoader
this.translateLSLoader.getTranslation(languageCode).subscribe(data => this.translationData(data, languageCode), error => console.log(error));
translationData(data:any, languageCode:any){
//set current translation
this.translate.setTranslation(languageCode,data,false);
this.translate.use(languageCode);
}
它对我有用,如果您有任何问题,请告诉我。