-
-
Save un33k/43c3e9bb1c762b229afb71e9593c41b9 to your computer and use it in GitHub Desktop.
ng2-translate file loader with localstorage to speed up things
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment