Skip to content

Commit

Permalink
refactor(language & message): move language and message stuff around (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cbourget authored and mbarbeau committed May 11, 2017
1 parent 98a5cb0 commit 6ed2210
Show file tree
Hide file tree
Showing 23 changed files with 87 additions and 110 deletions.
9 changes: 4 additions & 5 deletions src/demo-app/app/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import { IgoModule, provideSearchSourceOptions,
provideIChercheSearchSource,
provideNominatimSearchSource,
provideDataSourceSearchSource,
LanguageLoader, provideLanguageService,
LanguageLoader, provideLanguageLoader,
provideContextServiceOptions } from '../../lib';

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

export function translateLoader(http: Http) {

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

Expand All @@ -41,9 +42,7 @@ export function translateLoader(http: Http) {
basePath: './contexts',
contextListFile: '_contexts.json'
}),
provideLanguageService({
loader: translateLoader
})
provideLanguageLoader(languageLoader)
],
bootstrap: [AppComponent]
})
Expand Down
1 change: 1 addition & 0 deletions src/lib/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './module';

export * from './activity';
export * from './language';
export * from './media';
export * from './message';
export * from './request';
Expand Down
1 change: 1 addition & 0 deletions src/lib/core/language/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './shared';
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './language.loader';
export * from './language.service';
export * from './language.service.provider';
export * from './language-loader';
export * from './missing-translation.guard';
29 changes: 29 additions & 0 deletions src/lib/core/language/shared/language.loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { TranslateLoader } from '@ngx-translate/core';


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(`../../../../../src/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);
});

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { HttpModule } from '@angular/http';

import { TranslateModule } from '@ngx-translate/core';

import { IgoTestModule } from '../../../test/module';
import { IgoTestModule } from '../../../../test/module';

import { LanguageService } from './language.service';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { Http } from '@angular/http';
import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { TranslateService, TranslateLoader } from '@ngx-translate/core';

import { LanguageLoader } from './language.loader';


export function defaultLanguageLoader(http: Http) {
return new LanguageLoader();
}

export function provideLanguageLoader(loader?) {

return {
provide: TranslateLoader,
useFactory: (loader) || (defaultLanguageLoader),
deps: [Http]
};
}


@Injectable()
export class LanguageService {
Expand Down
4 changes: 1 addition & 3 deletions src/lib/core/message/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './message.enum';
export * from './message.interface';
export * from './message.service';
export * from './shared';
3 changes: 3 additions & 0 deletions src/lib/core/message/shared/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './message.enum';
export * from './message.interface';
export * from './message.service';
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 21 additions & 4 deletions src/lib/core/module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import { NgModule, ModuleWithProviders } from '@angular/core';

import { TranslateModule, MissingTranslationHandler,
TranslateService } from '@ngx-translate/core';

import { LanguageService, IgoMissingTranslationHandler,
provideLanguageLoader } from './language';

import { ActivityService } from './activity';
import { MediaService } from './media';
import { MessageService } from './message';
import { RequestService } from './request';
import { MessageService } from './message';


@NgModule({
imports: [],
exports: [],
imports: [
TranslateModule.forRoot({
missingTranslationHandler: {
provide: MissingTranslationHandler,
useClass: IgoMissingTranslationHandler
}
})
],
exports: []
})
export class IgoCoreModule {
static forRoot(): ModuleWithProviders {
Expand All @@ -18,7 +31,11 @@ export class IgoCoreModule {
ActivityService,
MediaService,
MessageService,
RequestService
RequestService,

TranslateService,
LanguageService,
provideLanguageLoader()
]
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe('TimeFilterItemComponent', () => {
it('should create', () => {
const dataSource = new WMSDataSource({
title: 'WMS',
type: 'wms',
url: 'foo',
projection: 'EPSG:3857',
params: {
Expand Down
1 change: 0 additions & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export * from './datasource';
export * from './feature';
export * from './filter';
export * from './form';
export * from './language';
export * from './layer';
export * from './map';
export * from './overlay';
Expand Down
3 changes: 0 additions & 3 deletions src/lib/language/index.ts

This file was deleted.

31 changes: 0 additions & 31 deletions src/lib/language/module.ts

This file was deleted.

29 changes: 0 additions & 29 deletions src/lib/language/shared/language-loader.ts

This file was deleted.

20 changes: 0 additions & 20 deletions src/lib/language/shared/language.service.provider.ts

This file was deleted.

4 changes: 1 addition & 3 deletions src/lib/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import 'rxjs/add/operator/finally';
import 'rxjs/add/operator/combineLatest';

import { IgoCoreModule } from './core/index';
import { IgoLanguageModule } from './language/index';
import { IgoContextModule } from './context/index';
import { IgoDataSourceModule } from './datasource/index';
import { IgoFeatureModule } from './feature/index';
Expand All @@ -30,7 +29,7 @@ import { IgoSharedModule } from './shared/index';
import { IgoToolModule } from './tool/index';

const IGO_MODULES = [
IgoLanguageModule,
IgoCoreModule,
IgoContextModule,
IgoDataSourceModule,
IgoFeatureModule,
Expand All @@ -51,7 +50,6 @@ const IGO_MODULES = [
MaterialModule.forRoot(),

IgoCoreModule.forRoot(),
IgoLanguageModule.forRoot(),
IgoDataSourceModule.forRoot(),
IgoContextModule.forRoot(),
IgoFeatureModule.forRoot(),
Expand Down
11 changes: 5 additions & 6 deletions src/test/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NgModule } from '@angular/core';
import { Http } from '@angular/http';

import { IgoLanguageModule, LanguageLoader, provideLanguageService } from '../lib/language';
import { IgoCoreModule, LanguageLoader, provideLanguageLoader } from '../lib';

import 'rxjs/add/operator/debounceTime.js';
import 'rxjs/add/operator/distinctUntilChanged';
Expand All @@ -11,18 +11,17 @@ import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/finally';


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

@NgModule({
imports: [
IgoLanguageModule.forRoot()
IgoCoreModule.forRoot()
],
providers: [
provideLanguageService({
loader: translateLoader
})
],
provideLanguageLoader(translateLoader)
]
})
export class IgoTestModule { }

0 comments on commit 6ed2210

Please sign in to comment.