-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(translate): add translate support
- Loading branch information
Showing
8 changed files
with
85 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
export * from './language.service'; | ||
export * from './language-loader'; | ||
export * from './missing-translation.guard'; | ||
|
||
export * from './language.service.provider'; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Http, Response } from '@angular/http'; | ||
import { Observable } from 'rxjs/Observable'; | ||
|
||
import { TranslateLoader } from '@ngx-translate/core'; | ||
import 'rxjs/add/operator/map'; | ||
import 'rxjs/add/operator/combineLatest'; | ||
|
||
declare function require(arg: string): any; | ||
|
||
export class LanguageLoader implements TranslateLoader { | ||
constructor(private http?: Http, | ||
private prefix: string = '/assets/locale/', | ||
private suffix: string = '.json') {} | ||
|
||
public getTranslation(lang: string): any { | ||
const translation = require(`../../../assets/locale/${lang}.json`); | ||
const igoLocale$ = Observable.of(translation); | ||
|
||
if (!this.http) { | ||
return igoLocale$; | ||
} | ||
|
||
const appLocale$ = this.http.get(`${this.prefix}${lang}${this.suffix}`) | ||
.map((res: Response) => res.json()); | ||
|
||
return igoLocale$.combineLatest(appLocale$, (igoTranslation, appTranslation) => { | ||
return Object.assign(igoTranslation, appTranslation); | ||
}); | ||
|
||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Http } from '@angular/http'; | ||
import { TranslateLoader } from '@ngx-translate/core'; | ||
|
||
import { LanguageLoader } from './language-loader'; | ||
|
||
export interface LanguageModuleConfig { | ||
loader?: (http: Http) => LanguageLoader; | ||
} | ||
|
||
export function defaultTranslateLoader() { | ||
return new LanguageLoader(); | ||
} | ||
|
||
export function provideLanguageService(config: LanguageModuleConfig = {}) { | ||
return { | ||
provide: TranslateLoader, | ||
useFactory: (config.loader) || (defaultTranslateLoader), | ||
deps: [Http] | ||
}; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,22 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Http, Response } from '@angular/http'; | ||
|
||
import { Injectable, NgZone } from '@angular/core'; | ||
import { TranslateService } from '@ngx-translate/core'; | ||
|
||
declare function require(arg: string): any; | ||
|
||
@Injectable() | ||
export class LanguageService { | ||
|
||
constructor(private translate: TranslateService, | ||
private http: Http) { | ||
constructor(private translate: TranslateService, private zone: NgZone) { | ||
const lang = this.getLanguage(); | ||
|
||
const translation = require(`../../../assets/locale/${lang}.json`); | ||
this.translate.setTranslation(lang, translation, true); | ||
// this.readTranslation('/lib/assets/locale/', lang); | ||
|
||
this.translate.setDefaultLang('en'); | ||
this.translate.use(lang); | ||
this.translate.setDefaultLang(lang); | ||
} | ||
|
||
public getLanguage(): string { | ||
const browserLang = this.translate.getBrowserLang(); | ||
return browserLang.match(/en|fr/) ? browserLang : 'en'; | ||
} | ||
|
||
public readTranslation(prefix: string, lang: string = this.getLanguage()): any { | ||
return this.http.get(`${prefix}${lang}.json`) | ||
.map((res: Response) => res.json()) | ||
.subscribe((translation) => { | ||
this.translate.setTranslation(lang, translation, true); | ||
}); | ||
public setLanguage(language: string) { | ||
this.translate.use(language); | ||
this.translate.reloadLang(language); | ||
this.zone.run(() => {}); | ||
} | ||
|
||
public setTranslation(lang: string, translation: Object): void { | ||
this.translate.setTranslation(lang, translation, true); | ||
} | ||
|
||
} |