Skip to content

Instantly share code, notes, and snippets.

Created April 27, 2016 08:55
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
ng2-translate file loader with localstorage to speed up things
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) {;
.map((res: Response) => res.json())
.subscribe((res: any) => {;
localStorage.setItem('hmx-lang-' + lang, JSON.stringify(res));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment