Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(translate): add translate support #19

Merged
merged 6 commits into from
Apr 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ module.exports = function (config) {
nocache: false,
served: true
},
{
pattern: './src/demo-app/assets/locale/*.json',
watched: false,
included: false,
nocache: false,
served: true
},
{ pattern: './src/test/test.ts', watched: false }
],
preprocessors: {
Expand Down
5 changes: 1 addition & 4 deletions src/demo-app/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ export class AppComponent implements OnInit {
constructor(public featureService: FeatureService,
public layerService: LayerService,
public overlayService: OverlayService,
public language: LanguageService) {

// this.language.readTranslation('/assets/locale/');
}
public language: LanguageService) {}

ngOnInit() {
this.map.removeLayers();
Expand Down
15 changes: 12 additions & 3 deletions src/demo-app/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { HttpModule, Http } from '@angular/http';
import { MaterialModule } from '@angular/material';

import { IgoModule, provideDefaultSearchSources } from '../../lib';
import { IgoModule, provideDefaultSearchSources,
LanguageLoader, provideLanguageService } from '../../lib';

import { AppComponent } from './app.component';

export function translateLoader(http: Http) {
return new LanguageLoader(http, './assets/locale/', '.json');
}

@NgModule({
declarations: [
AppComponent
Expand All @@ -19,7 +25,10 @@ import { AppComponent } from './app.component';
IgoModule.forRoot()
],
providers: [
...provideDefaultSearchSources()
...provideDefaultSearchSources(),
provideLanguageService({
loader: translateLoader
})
],
bootstrap: [AppComponent]
})
Expand Down
7 changes: 4 additions & 3 deletions src/lib/language/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { NgModule, ModuleWithProviders } from '@angular/core';
import { TranslateModule, MissingTranslationHandler,
TranslateService } from '@ngx-translate/core';


import { LanguageService, IgoMissingTranslationHandler } from './shared';
import { LanguageService, IgoMissingTranslationHandler,
provideLanguageService } from './shared';

@NgModule({
imports: [
Expand All @@ -23,7 +23,8 @@ export class IgoLanguageModule {
ngModule: IgoLanguageModule,
providers: [
TranslateService,
LanguageService
LanguageService,
provideLanguageService()
]
};
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib/language/shared/index.ts
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';
31 changes: 31 additions & 0 deletions src/lib/language/shared/language-loader.ts
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);
});

}
}
20 changes: 20 additions & 0 deletions src/lib/language/shared/language.service.provider.ts
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]
};
}
32 changes: 7 additions & 25 deletions src/lib/language/shared/language.service.ts
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);
}

}