-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SHIBUI-814 Fixed tests, implemented pipe for i18n
- Loading branch information
Showing
16 changed files
with
272 additions
and
33 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
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
File renamed without changes.
File renamed without changes.
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,53 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { StoreModule } from '@ngrx/store'; | ||
import { EffectsModule } from '@ngrx/effects'; | ||
|
||
import { reducers } from './reducer'; | ||
import { HttpClientModule } from '@angular/common/http'; | ||
import { I18nService } from './service/i18n.service'; | ||
import { MessageEffects } from './effect/message.effect'; | ||
import { I18nPipe } from './pipe/i18n.pipe'; | ||
import { CoreModule } from '../core/core.module'; | ||
|
||
export const COMPONENTS = []; | ||
export const DIRECTIVES = []; | ||
export const PIPES = [ | ||
I18nPipe | ||
]; | ||
|
||
@NgModule({ | ||
imports: [ | ||
CommonModule, | ||
HttpClientModule, | ||
CoreModule | ||
], | ||
declarations: [ | ||
...PIPES, | ||
...COMPONENTS, | ||
...DIRECTIVES | ||
], | ||
exports: [ | ||
...PIPES, | ||
...COMPONENTS, | ||
...DIRECTIVES | ||
], | ||
}) | ||
export class I18nModule { | ||
static forRoot() { | ||
return { | ||
ngModule: RootI18nModule, | ||
providers: [ | ||
I18nService | ||
] | ||
}; | ||
} | ||
} | ||
|
||
@NgModule({ | ||
imports: [ | ||
StoreModule.forFeature('i18n', reducers), | ||
EffectsModule.forFeature([MessageEffects]), | ||
], | ||
}) | ||
export class RootI18nModule { } |
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,72 @@ | ||
import { I18nPipe } from './i18n.pipe'; | ||
import { I18nService } from '../service/i18n.service'; | ||
import { CommonModule } from '@angular/common'; | ||
import { StoreModule, combineReducers, Store } from '@ngrx/store'; | ||
|
||
import * as fromI18n from '../reducer'; | ||
import { TestBed, ComponentFixture } from '@angular/core/testing'; | ||
import { Component } from '@angular/core'; | ||
import { MessagesLoadSuccessAction } from '../action/message.action'; | ||
|
||
@Component({ | ||
template: ` | ||
<span>{{ foo | i18n:{ foo: 'bar' } }}</span> | ||
` | ||
}) | ||
class TestHostComponent { | ||
private _foo: string; | ||
|
||
public get foo(): string { | ||
return this._foo; | ||
} | ||
|
||
public set foo(val: string) { | ||
this._foo = val; | ||
} | ||
} | ||
|
||
describe('Pipe: I18n translation', () => { | ||
let fixture: ComponentFixture<TestHostComponent>; | ||
let instance: TestHostComponent; | ||
let store: Store<fromI18n.State>; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
{ provide: I18nService, useValue: { | ||
interpolate: (value: string, interpolated: { [prop: string]: string } = {}): string => { | ||
return Object.entries(interpolated).reduce((current, interpolate) => { | ||
let reg = new RegExp(`{\\s*${interpolate[0]}\\s*}`, 'gm'); | ||
return current.replace(reg, interpolate[1]); | ||
}, value); | ||
} | ||
} } | ||
], | ||
imports: [ | ||
CommonModule, | ||
StoreModule.forRoot({ | ||
'message': combineReducers(fromI18n.reducers), | ||
}) | ||
], | ||
declarations: [ | ||
I18nPipe, | ||
TestHostComponent | ||
], | ||
}); | ||
store = TestBed.get(Store); | ||
|
||
spyOn(store, 'dispatch').and.callThrough(); | ||
|
||
fixture = TestBed.createComponent(TestHostComponent); | ||
instance = fixture.componentInstance; | ||
}); | ||
|
||
it('should set the correct text', () => { | ||
store.dispatch(new MessagesLoadSuccessAction({ foo: 'hi there' })); | ||
|
||
store.select(fromI18n.getMessages).subscribe(() => { | ||
fixture.detectChanges(); | ||
expect(fixture.nativeElement.textContent).toContain('hi there'); | ||
}); | ||
}); | ||
}); |
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,40 @@ | ||
import { PipeTransform, Pipe, ChangeDetectorRef, OnDestroy } from '@angular/core'; | ||
import { Store } from '@ngrx/store'; | ||
import * as fromI18n from '../reducer'; | ||
import { Subscription } from 'rxjs'; | ||
import { I18nService } from '../service/i18n.service'; | ||
|
||
@Pipe({ | ||
name: 'i18n', | ||
pure: false | ||
}) | ||
export class I18nPipe implements PipeTransform, OnDestroy { | ||
|
||
sub: Subscription; | ||
|
||
messages: { [propName: string]: string } = {}; | ||
|
||
value: string; | ||
|
||
constructor( | ||
private store: Store<fromI18n.State>, | ||
private i18nService: I18nService, | ||
private _ref: ChangeDetectorRef | ||
) { | ||
this.sub = this.store.select(fromI18n.getMessages).subscribe(messages => { | ||
this.messages = messages ? messages : {}; | ||
this._ref.markForCheck(); | ||
}); | ||
} | ||
|
||
transform(value: string, interpolated: { [prop: string]: string } = {}): any { | ||
interpolated = interpolated || {}; | ||
let val = this.messages.hasOwnProperty(value) ? this.messages[value] : value; | ||
this.value = this.i18nService.interpolate(val, interpolated); | ||
return this.value; | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.sub.unsubscribe(); | ||
} | ||
} |
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 { | ||
createSelector, | ||
createFeatureSelector | ||
} from '@ngrx/store'; | ||
|
||
import * as fromMessages from './message.reducer'; | ||
import * as fromRoot from '../../app.reducer'; | ||
import { getCurrentLanguage, getCurrentCountry } from '../../shared/util'; | ||
|
||
export interface I18nState { | ||
messages: fromMessages.MessageState; | ||
} | ||
|
||
export interface State extends fromRoot.State { | ||
core: I18nState; | ||
} | ||
|
||
export const reducers = { | ||
messages: fromMessages.reducer | ||
}; | ||
|
||
export const getCoreFeature = createFeatureSelector<I18nState>('i18n'); | ||
export const getMessageStateFn = (state: I18nState) => state.messages; | ||
|
||
export const getMessageState = createSelector(getCoreFeature, getMessageStateFn); | ||
export const getLocale = createSelector(getMessageState, fromMessages.getLocale); | ||
export const getLanguage = createSelector(getLocale, locale => getCurrentLanguage(locale)); | ||
export const getCountry = createSelector(getLocale, locale => getCurrentCountry(locale)); | ||
|
||
export const getMessages = createSelector(getMessageState, fromMessages.getMessages); | ||
export const getFetchingMessages = createSelector(getMessageState, fromMessages.isFetching); |
File renamed without changes.
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
Oops, something went wrong.