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(language): add support language in lib #13

Merged
merged 8 commits into from
Apr 18, 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
10 changes: 5 additions & 5 deletions demo-app/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<md-card-title>search-bar.component</md-card-title>
<md-card-content>
<igo-search-bar
[placeholder]="'Search'"
[placeholder]="'Search Results' | translate"
(search)="handleSearch($event)">
</igo-search-bar>

Expand Down Expand Up @@ -31,7 +31,7 @@

<md-card>
<md-card-subtitle>Map module</md-card-subtitle>
<md-card-title>map-browser.component</md-card-title>
<md-card-title>map-browser.component</md-card-title>
<md-card-content class="igo-map-browser-container">
<igo-map-browser
[map]="map" [view]="mapView"
Expand All @@ -43,16 +43,16 @@

<md-card>
<md-card-subtitle>Layer module</md-card-subtitle>
<md-card-title>layer-list.component</md-card-title>
<md-card-title>layer-list.component</md-card-title>
<md-card-content>
<igo-layer-list [layers]="map.layers$ | async"></igo-layer-list>
</md-card-content>
</md-card>

<md-card>
<md-card-subtitle>Filter module</md-card-subtitle>
<md-card-title>time-filter.component</md-card-title>
<md-card-title>time-filter.component</md-card-title>
<md-card-content>
<igo-time-filter-list [layers]="map.layers$ | async"></igo-time-filter-list>
</md-card-content>
</md-card>
</md-card>
13 changes: 0 additions & 13 deletions demo-app/app/app.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
import { TestBed, async } from '@angular/core/testing';
import { Http } from '@angular/http';
import { MaterialModule } from '@angular/material';
import { TranslateModule, TranslateLoader,
TranslateStaticLoader } from 'ng2-translate';

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

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

export function createTranslateLoader(http: Http) {
return new TranslateStaticLoader(http, './locale', '.json');
}

describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
MaterialModule,
TranslateModule.forRoot({
provide: TranslateLoader,
useFactory: createTranslateLoader,
deps: [Http]
}),
IgoModule.forRoot()
],
declarations: [
Expand Down
9 changes: 7 additions & 2 deletions demo-app/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { BehaviorSubject } from 'rxjs/BehaviorSubject';

import { IgoMap, LayerService,
OverlayService, QueryFormat,
Feature, FeatureService, WMSLayerOptions } from '../../lib/src';
Feature, FeatureService,
WMSLayerOptions, LanguageService } from '../../lib/src';

@Component({
selector: 'igo-demo',
Expand All @@ -24,7 +25,11 @@ export class AppComponent implements OnInit {

constructor(public featureService: FeatureService,
public layerService: LayerService,
public overlayService: OverlayService) {}
public overlayService: OverlayService,
private language: LanguageService) {

language.readTranslation('/assets/locale/');
}

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

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

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

export function createTranslateLoader(http: Http) {
return new TranslateStaticLoader(http, './locale', '.json');
}


@NgModule({
declarations: [
AppComponent
Expand All @@ -24,11 +16,6 @@ export function createTranslateLoader(http: Http) {
FormsModule,
HttpModule,
MaterialModule.forRoot(),
TranslateModule.forRoot({
provide: TranslateLoader,
useFactory: createTranslateLoader,
deps: [Http]
}),
IgoModule.forRoot()
],
providers: [
Expand Down
2 changes: 2 additions & 0 deletions demo-app/assets/locale/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
3 changes: 3 additions & 0 deletions demo-app/assets/locale/fr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion assets/locale/fr.json → lib/assets/locale/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"Search Results": "Résultats de recherche",
"Search for an address or a place": "Rechercher une adresse, un lieu ou une couche",
"Search disabled": "L'outil de recherche est désactivé dans ce contexte",
"Start Date": "Date de début",
"Show Layer": "Afficher couche",
"Start Date": "Date de début",
"Time Analysis": "Analyse Temporelle",
"Title": "Titre",

Expand Down
22 changes: 14 additions & 8 deletions lib/src/language/module.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import { NgModule, ModuleWithProviders } from '@angular/core';
import { MissingTranslationHandler } from 'ng2-translate';

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


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

@NgModule({
imports: [],
imports: [
TranslateModule.forRoot({
missingTranslationHandler: {
provide: MissingTranslationHandler,
useClass: IgoMissingTranslationHandler
}
})
],
exports: []
})
export class IgoLanguageModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: IgoLanguageModule,
providers: [
LanguageService,
{
provide: MissingTranslationHandler,
useClass: IgoMissingTranslationHandler
}
TranslateService,
LanguageService
]
};
}
Expand Down
13 changes: 10 additions & 3 deletions lib/src/language/shared/language.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { TestBed, inject } from '@angular/core/testing';
import { TranslateModule } from 'ng2-translate';
import { TranslateModule, MissingTranslationHandler,
TranslateService } from '@ngx-translate/core';

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

import { LanguageService } from './language.service';
import { IgoMissingTranslationHandler } from './missing-translation.guard';


describe('LanguageService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
IgoTestModule,
TranslateModule
TranslateModule.forRoot({
missingTranslationHandler: {
provide: MissingTranslationHandler,
useClass: IgoMissingTranslationHandler
}
})
],
providers: []
providers: [ TranslateService ]
});
});

Expand Down
38 changes: 33 additions & 5 deletions lib/src/language/shared/language.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
import { Injectable } from '@angular/core';
import { TranslateService } from 'ng2-translate';
import { Http, Response } from "@angular/http";

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

declare function require(arg:string): any;

@Injectable()
export class LanguageService {

constructor(public translate: TranslateService) {
translate.setDefaultLang('en');
const browserLang = translate.getBrowserLang();
translate.use(browserLang.match(/en|fr/) ? browserLang : 'en');
constructor(private translate: TranslateService,
private http: Http) {
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);
}

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 setTranslation(lang: string, translation: Object): void {
this.translate.setTranslation(lang, translation, true);
}

}
2 changes: 1 addition & 1 deletion lib/src/language/shared/missing-translation.guard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MissingTranslationHandler,
MissingTranslationHandlerParams } from 'ng2-translate';
MissingTranslationHandlerParams } from '@ngx-translate/core';

export class IgoMissingTranslationHandler
implements MissingTranslationHandler {
Expand Down
11 changes: 0 additions & 11 deletions lib/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { NgModule, ModuleWithProviders } from '@angular/core';
import { Http } from '@angular/http';
import { MaterialModule } from '@angular/material';
import { TranslateModule, TranslateLoader,
TranslateStaticLoader } from 'ng2-translate';

import 'rxjs/add/operator/debounceTime.js';
import 'rxjs/add/operator/distinctUntilChanged';
Expand All @@ -25,9 +22,6 @@ import { IgoQueryModule } from './query/index';
import { IgoSearchModule } from './search/index';
import { IgoSharedModule } from './shared/index';

export function createTranslateLoader(http: Http) {
return new TranslateStaticLoader(http, './locale', '.json');
}

const IGO_MODULES = [
IgoLanguageModule,
Expand All @@ -44,11 +38,6 @@ const IGO_MODULES = [
@NgModule({
imports: [
MaterialModule.forRoot(),
TranslateModule.forRoot({
provide: TranslateLoader,
useFactory: createTranslateLoader,
deps: [Http]
}),

IgoCoreModule.forRoot(),
IgoLanguageModule.forRoot(),
Expand Down
2 changes: 1 addition & 1 deletion lib/src/shared/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MaterialModule } from '@angular/material';
import { TranslateModule } from 'ng2-translate';
import { TranslateModule } from '@ngx-translate/core';

import { ClickoutDirective } from './clickout';
import { CollapsibleComponent, CollapseDirective } from './collapsible';
Expand Down
15 changes: 2 additions & 13 deletions lib/test/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { NgModule } from '@angular/core';
import { Http } from '@angular/http';
import { TranslateModule, TranslateLoader,
TranslateStaticLoader } from 'ng2-translate';
import { TranslateModule } from '@ngx-translate/core';

import { IgoLanguageModule } from '../src/language';

Expand All @@ -15,18 +13,9 @@ import 'rxjs/add/operator/finally';

import 'openlayers';


export function createTranslateLoader(http: Http) {
return new TranslateStaticLoader(http, 'base/assets/locale', '.json');
}

@NgModule({
imports: [
TranslateModule.forRoot({
provide: TranslateLoader,
useFactory: createTranslateLoader,
deps: [Http]
}),
TranslateModule.forRoot(),
IgoLanguageModule.forRoot()
],
exports: [
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
"@angular/http": "^4.0.0",
"@angular/material": "git+https://github.com/angular/material2-builds.git",
"@angular/platform-browser": "^4.0.0",
"@ngx-translate/core": "^6.0.1",
"core-js": "^2.4.1",
"openlayers": "^4.0.1",
"md2": "0.0.18",
"ng2-translate": "^5.0.0",
"openlayers": "^4.0.1",
"rxjs": "^5.1.0",
"ts-md5": "^1.2.0",
"zone.js": "^0.8.4"
Expand All @@ -42,7 +42,7 @@
"@angular/platform-server": "^4.0.0",
"@angular/router": "^4.0.0",
"@types/jasmine": "2.5.38",
"@types/node": "^6.0.67",
"@types/node": "^6.0.68",
"@types/openlayers": "^4.0.1",
"angular2-template-loader": "^0.6.2",
"awesome-typescript-loader": "^3.1.2",
Expand Down
2 changes: 1 addition & 1 deletion webpack.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = {
/^rxjs\//,
/^md2/,
/^ts-md5/,
/^ng2-translate/,
/^ngx-translate/,
/^zone.js/,
/openlayers/
],
Expand Down