From 6ed2210824787d38c7b13cf778dd1ece9464403b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-=C3=89ric?= Date: Thu, 11 May 2017 10:38:44 -0400 Subject: [PATCH] refactor(language & message): move language and message stuff around (#52) --- src/demo-app/app/module.ts | 9 +++--- src/lib/core/index.ts | 1 + src/lib/core/language/index.ts | 1 + src/lib/{ => core}/language/shared/index.ts | 3 +- .../core/language/shared/language.loader.ts | 29 +++++++++++++++++ .../language/shared/language.service.spec.ts | 2 +- .../language/shared/language.service.ts | 20 +++++++++++- .../shared/missing-translation.guard.ts | 0 src/lib/core/message/index.ts | 4 +-- src/lib/core/message/shared/index.ts | 3 ++ .../core/message/{ => shared}/message.enum.ts | 0 .../message/{ => shared}/message.interface.ts | 0 .../{ => shared}/message.service.spec.ts | 0 .../message/{ => shared}/message.service.ts | 0 src/lib/core/module.ts | 25 ++++++++++++--- .../time-filter-item.component.spec.ts | 1 - src/lib/index.ts | 1 - src/lib/language/index.ts | 3 -- src/lib/language/module.ts | 31 ------------------- src/lib/language/shared/language-loader.ts | 29 ----------------- .../shared/language.service.provider.ts | 20 ------------ src/lib/module.ts | 4 +-- src/test/module.ts | 11 +++---- 23 files changed, 87 insertions(+), 110 deletions(-) create mode 100644 src/lib/core/language/index.ts rename src/lib/{ => core}/language/shared/index.ts (50%) create mode 100644 src/lib/core/language/shared/language.loader.ts rename src/lib/{ => core}/language/shared/language.service.spec.ts (90%) rename src/lib/{ => core}/language/shared/language.service.ts (54%) rename src/lib/{ => core}/language/shared/missing-translation.guard.ts (100%) create mode 100644 src/lib/core/message/shared/index.ts rename src/lib/core/message/{ => shared}/message.enum.ts (100%) rename src/lib/core/message/{ => shared}/message.interface.ts (100%) rename src/lib/core/message/{ => shared}/message.service.spec.ts (100%) rename src/lib/core/message/{ => shared}/message.service.ts (100%) delete mode 100644 src/lib/language/index.ts delete mode 100644 src/lib/language/module.ts delete mode 100644 src/lib/language/shared/language-loader.ts delete mode 100644 src/lib/language/shared/language.service.provider.ts diff --git a/src/demo-app/app/module.ts b/src/demo-app/app/module.ts index 605f19e606..f0cc902640 100644 --- a/src/demo-app/app/module.ts +++ b/src/demo-app/app/module.ts @@ -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'); } @@ -41,9 +42,7 @@ export function translateLoader(http: Http) { basePath: './contexts', contextListFile: '_contexts.json' }), - provideLanguageService({ - loader: translateLoader - }) + provideLanguageLoader(languageLoader) ], bootstrap: [AppComponent] }) diff --git a/src/lib/core/index.ts b/src/lib/core/index.ts index 861b90b4ef..c4886e8432 100644 --- a/src/lib/core/index.ts +++ b/src/lib/core/index.ts @@ -1,6 +1,7 @@ export * from './module'; export * from './activity'; +export * from './language'; export * from './media'; export * from './message'; export * from './request'; diff --git a/src/lib/core/language/index.ts b/src/lib/core/language/index.ts new file mode 100644 index 0000000000..c3da79f741 --- /dev/null +++ b/src/lib/core/language/index.ts @@ -0,0 +1 @@ +export * from './shared'; diff --git a/src/lib/language/shared/index.ts b/src/lib/core/language/shared/index.ts similarity index 50% rename from src/lib/language/shared/index.ts rename to src/lib/core/language/shared/index.ts index eb30df708b..177aaa266f 100644 --- a/src/lib/language/shared/index.ts +++ b/src/lib/core/language/shared/index.ts @@ -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'; diff --git a/src/lib/core/language/shared/language.loader.ts b/src/lib/core/language/shared/language.loader.ts new file mode 100644 index 0000000000..3383047223 --- /dev/null +++ b/src/lib/core/language/shared/language.loader.ts @@ -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); + }); + + } +} diff --git a/src/lib/language/shared/language.service.spec.ts b/src/lib/core/language/shared/language.service.spec.ts similarity index 90% rename from src/lib/language/shared/language.service.spec.ts rename to src/lib/core/language/shared/language.service.spec.ts index 795f77198f..b26510c816 100644 --- a/src/lib/language/shared/language.service.spec.ts +++ b/src/lib/core/language/shared/language.service.spec.ts @@ -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'; diff --git a/src/lib/language/shared/language.service.ts b/src/lib/core/language/shared/language.service.ts similarity index 54% rename from src/lib/language/shared/language.service.ts rename to src/lib/core/language/shared/language.service.ts index 6c84c13b2e..22057e8120 100644 --- a/src/lib/language/shared/language.service.ts +++ b/src/lib/core/language/shared/language.service.ts @@ -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 { diff --git a/src/lib/language/shared/missing-translation.guard.ts b/src/lib/core/language/shared/missing-translation.guard.ts similarity index 100% rename from src/lib/language/shared/missing-translation.guard.ts rename to src/lib/core/language/shared/missing-translation.guard.ts diff --git a/src/lib/core/message/index.ts b/src/lib/core/message/index.ts index 9d580e874f..c3da79f741 100644 --- a/src/lib/core/message/index.ts +++ b/src/lib/core/message/index.ts @@ -1,3 +1 @@ -export * from './message.enum'; -export * from './message.interface'; -export * from './message.service'; +export * from './shared'; diff --git a/src/lib/core/message/shared/index.ts b/src/lib/core/message/shared/index.ts new file mode 100644 index 0000000000..9d580e874f --- /dev/null +++ b/src/lib/core/message/shared/index.ts @@ -0,0 +1,3 @@ +export * from './message.enum'; +export * from './message.interface'; +export * from './message.service'; diff --git a/src/lib/core/message/message.enum.ts b/src/lib/core/message/shared/message.enum.ts similarity index 100% rename from src/lib/core/message/message.enum.ts rename to src/lib/core/message/shared/message.enum.ts diff --git a/src/lib/core/message/message.interface.ts b/src/lib/core/message/shared/message.interface.ts similarity index 100% rename from src/lib/core/message/message.interface.ts rename to src/lib/core/message/shared/message.interface.ts diff --git a/src/lib/core/message/message.service.spec.ts b/src/lib/core/message/shared/message.service.spec.ts similarity index 100% rename from src/lib/core/message/message.service.spec.ts rename to src/lib/core/message/shared/message.service.spec.ts diff --git a/src/lib/core/message/message.service.ts b/src/lib/core/message/shared/message.service.ts similarity index 100% rename from src/lib/core/message/message.service.ts rename to src/lib/core/message/shared/message.service.ts diff --git a/src/lib/core/module.ts b/src/lib/core/module.ts index bdb6f34003..d1fb7ef622 100644 --- a/src/lib/core/module.ts +++ b/src/lib/core/module.ts @@ -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 { @@ -18,7 +31,11 @@ export class IgoCoreModule { ActivityService, MediaService, MessageService, - RequestService + RequestService, + + TranslateService, + LanguageService, + provideLanguageLoader() ] }; } diff --git a/src/lib/filter/time-filter-item/time-filter-item.component.spec.ts b/src/lib/filter/time-filter-item/time-filter-item.component.spec.ts index 291605342a..f1e7a756cd 100644 --- a/src/lib/filter/time-filter-item/time-filter-item.component.spec.ts +++ b/src/lib/filter/time-filter-item/time-filter-item.component.spec.ts @@ -34,7 +34,6 @@ describe('TimeFilterItemComponent', () => { it('should create', () => { const dataSource = new WMSDataSource({ title: 'WMS', - type: 'wms', url: 'foo', projection: 'EPSG:3857', params: { diff --git a/src/lib/index.ts b/src/lib/index.ts index 07bdfc4fbd..ddfc77d883 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -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'; diff --git a/src/lib/language/index.ts b/src/lib/language/index.ts deleted file mode 100644 index e3d02905d4..0000000000 --- a/src/lib/language/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './module'; - -export * from './shared'; diff --git a/src/lib/language/module.ts b/src/lib/language/module.ts deleted file mode 100644 index f31577381d..0000000000 --- a/src/lib/language/module.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { NgModule, ModuleWithProviders } from '@angular/core'; - -import { TranslateModule, MissingTranslationHandler, - TranslateService } from '@ngx-translate/core'; - -import { LanguageService, IgoMissingTranslationHandler, - provideLanguageService } from './shared'; - -@NgModule({ - imports: [ - TranslateModule.forRoot({ - missingTranslationHandler: { - provide: MissingTranslationHandler, - useClass: IgoMissingTranslationHandler - } - }) - ], - exports: [] -}) -export class IgoLanguageModule { - static forRoot(): ModuleWithProviders { - return { - ngModule: IgoLanguageModule, - providers: [ - TranslateService, - LanguageService, - provideLanguageService() - ] - }; - } -} diff --git a/src/lib/language/shared/language-loader.ts b/src/lib/language/shared/language-loader.ts deleted file mode 100644 index 42642dbe99..0000000000 --- a/src/lib/language/shared/language-loader.ts +++ /dev/null @@ -1,29 +0,0 @@ -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); - }); - - } -} diff --git a/src/lib/language/shared/language.service.provider.ts b/src/lib/language/shared/language.service.provider.ts deleted file mode 100644 index d09f54fa4b..0000000000 --- a/src/lib/language/shared/language.service.provider.ts +++ /dev/null @@ -1,20 +0,0 @@ -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(http: Http) { - return new LanguageLoader(); -} - -export function provideLanguageService(config: LanguageModuleConfig = {}) { - return { - provide: TranslateLoader, - useFactory: (config.loader) || (defaultTranslateLoader), - deps: [Http] - }; -} diff --git a/src/lib/module.ts b/src/lib/module.ts index a42305e02d..6585104d00 100644 --- a/src/lib/module.ts +++ b/src/lib/module.ts @@ -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'; @@ -30,7 +29,7 @@ import { IgoSharedModule } from './shared/index'; import { IgoToolModule } from './tool/index'; const IGO_MODULES = [ - IgoLanguageModule, + IgoCoreModule, IgoContextModule, IgoDataSourceModule, IgoFeatureModule, @@ -51,7 +50,6 @@ const IGO_MODULES = [ MaterialModule.forRoot(), IgoCoreModule.forRoot(), - IgoLanguageModule.forRoot(), IgoDataSourceModule.forRoot(), IgoContextModule.forRoot(), IgoFeatureModule.forRoot(), diff --git a/src/test/module.ts b/src/test/module.ts index 6eee1c298b..8862e00d05 100644 --- a/src/test/module.ts +++ b/src/test/module.ts @@ -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'; @@ -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 { }