From 91af69ffec3b267c6d5a68529672340d7788ec95 Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Mon, 18 May 2020 21:03:42 +0300 Subject: [PATCH 1/7] feat: add localization methods with fallbacks --- .../src/lib/services/localization.service.ts | 52 +++--- .../lib/tests/localization.service.spec.ts | 172 +++++++++++++++++- .../core/src/lib/utils/localization-utils.ts | 34 ++++ 3 files changed, 233 insertions(+), 25 deletions(-) create mode 100644 npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts diff --git a/npm/ng-packs/packages/core/src/lib/services/localization.service.ts b/npm/ng-packs/packages/core/src/lib/services/localization.service.ts index 8adef08f46..5fea751b14 100644 --- a/npm/ng-packs/packages/core/src/lib/services/localization.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/localization.service.ts @@ -2,11 +2,12 @@ import { Injectable, NgZone, Optional, SkipSelf } from '@angular/core'; import { ActivatedRouteSnapshot, Router } from '@angular/router'; import { Actions, ofActionSuccessful, Store } from '@ngxs/store'; import { noop, Observable } from 'rxjs'; +import { map } from 'rxjs/operators'; import { SetLanguage } from '../actions/session.actions'; -import { ApplicationConfiguration } from '../models/application-configuration'; import { Config } from '../models/config'; import { ConfigState } from '../states/config.state'; import { registerLocale } from '../utils/initial-utils'; +import { localize, localizeWithFallback } from '../utils/localization-utils'; type ShouldReuseRoute = (future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot) => boolean; @@ -77,30 +78,35 @@ export class LocalizationService { return this.store.selectSnapshot(ConfigState.getLocalization(key, ...interpolateParams)); } - isLocalized(key, sourceName) { - if (sourceName === '_') { - // A convention to suppress the localization - return true; - } - - const localization = this.store.selectSnapshot( - ConfigState.getOne('localization'), - ) as ApplicationConfiguration.Localization; - sourceName = sourceName || localization.defaultResourceName; - if (!sourceName) { - return false; - } + localize(resourceName: string, key: string, defaultValue: string): Observable { + return this.store + .select(ConfigState.getOne('localization')) + .pipe(map(localize(resourceName, key, defaultValue))); + } - const source = localization.values[sourceName]; - if (!source) { - return false; - } + localizeSync(resourceName: string, key: string, defaultValue: string): string { + return localize( + resourceName, + key, + defaultValue, + )(this.store.selectSnapshot(ConfigState.getOne('localization'))); + } - const value = source[key]; - if (value === undefined) { - return false; - } + localizeWithFallback( + resourceNames: string[], + keys: string[], + defaultValue: string, + ): Observable { + return this.store + .select(ConfigState.getOne('localization')) + .pipe(map(localizeWithFallback(resourceNames, keys, defaultValue))); + } - return true; + localizeWithFallbackSync(resourceNames: string[], keys: string[], defaultValue: string): string { + return localizeWithFallback( + resourceNames, + keys, + defaultValue, + )(this.store.selectSnapshot(ConfigState.getOne('localization'))); } } diff --git a/npm/ng-packs/packages/core/src/lib/tests/localization.service.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/localization.service.spec.ts index 8c5b859f9f..e17d53e463 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/localization.service.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/localization.service.spec.ts @@ -1,7 +1,7 @@ import { Router } from '@angular/router'; import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator/jest'; -import { Store, Actions } from '@ngxs/store'; -import { Observable, of, Subject } from 'rxjs'; +import { Actions, Store } from '@ngxs/store'; +import { of, Subject } from 'rxjs'; import { LocalizationService } from '../services/localization.service'; describe('LocalizationService', () => { @@ -75,4 +75,172 @@ describe('LocalizationService', () => { } }); }); + + describe('#localize', () => { + test.each` + resource | key | defaultValue | expected + ${'foo'} | ${'bar'} | ${'DEFAULT'} | ${'baz'} + ${'x'} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} + ${'a'} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} + ${''} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} + ${undefined} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} + ${'foo'} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} + ${'x'} | ${'y'} | ${'DEFAULT'} | ${'z'} + ${'a'} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} + ${''} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} + ${undefined} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} + ${'foo'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} + ${'x'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} + ${'a'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} + ${''} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} + ${undefined} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} + ${'foo'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} + ${'x'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} + ${'a'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} + ${''} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} + ${undefined} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} + `( + 'should return observable $expected when resource name is $resource and key is $key', + async ({ resource, key, defaultValue, expected }) => { + store.select.andReturn( + of({ + values: { foo: { bar: 'baz' }, x: { y: 'z' } }, + defaultResourceName: 'x', + }), + ); + + const result = await service.localize(resource, key, defaultValue).toPromise(); + + expect(result).toBe(expected); + }, + ); + }); + + describe('#localizeSync', () => { + test.each` + resource | key | defaultValue | expected + ${'foo'} | ${'bar'} | ${'DEFAULT'} | ${'baz'} + ${'x'} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} + ${'a'} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} + ${''} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} + ${undefined} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} + ${'foo'} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} + ${'x'} | ${'y'} | ${'DEFAULT'} | ${'z'} + ${'a'} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} + ${''} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} + ${undefined} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} + ${'foo'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} + ${'x'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} + ${'a'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} + ${''} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} + ${undefined} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} + ${'foo'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} + ${'x'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} + ${'a'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} + ${''} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} + ${undefined} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} + `( + 'should return $expected when resource name is $resource and key is $key', + ({ resource, key, defaultValue, expected }) => { + store.selectSnapshot.andReturn({ + values: { foo: { bar: 'baz' }, x: { y: 'z' } }, + defaultResourceName: 'x', + }); + + const result = service.localizeSync(resource, key, defaultValue); + + expect(result).toBe(expected); + }, + ); + }); + + describe('#localizeWithFallback', () => { + test.each` + resources | keys | defaultValue | expected + ${['foo']} | ${['bar']} | ${'DEFAULT'} | ${'baz'} + ${['x']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['a', 'b', 'c']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${[]} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['foo']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['x']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['a', 'b', 'c']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${[]} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['foo']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'baz'} + ${['x']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${['a', 'b', 'c']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${['']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${[]} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${['foo']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['x']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['a', 'b', 'c']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${[]} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['foo']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${['x']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${['a', 'b', 'c']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${['']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${[]} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + `( + 'should return observable $expected when resource names are $resources and keys are $keys', + async ({ resources, keys, defaultValue, expected }) => { + store.select.andReturn( + of({ + values: { foo: { bar: 'baz' }, x: { y: 'z' } }, + defaultResourceName: 'x', + }), + ); + + const result = await service + .localizeWithFallback(resources, keys, defaultValue) + .toPromise(); + + expect(result).toBe(expected); + }, + ); + }); + + describe('#localizeWithFallbackSync', () => { + test.each` + resources | keys | defaultValue | expected + ${['foo']} | ${['bar']} | ${'DEFAULT'} | ${'baz'} + ${['x']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['a', 'b', 'c']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${[]} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['foo']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['x']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['a', 'b', 'c']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${[]} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['foo']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'baz'} + ${['x']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${['a', 'b', 'c']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${['']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${[]} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${['foo']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['x']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['a', 'b', 'c']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${[]} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['foo']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${['x']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${['a', 'b', 'c']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${['']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${[]} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + `( + 'should return $expected when resource names are $resources and keys are $keys', + ({ resources, keys, defaultValue, expected }) => { + store.selectSnapshot.andReturn({ + values: { foo: { bar: 'baz' }, x: { y: 'z' } }, + defaultResourceName: 'x', + }); + + const result = service.localizeWithFallbackSync(resources, keys, defaultValue); + + expect(result).toBe(expected); + }, + ); + }); }); diff --git a/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts new file mode 100644 index 0000000000..7d5a2c9e70 --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts @@ -0,0 +1,34 @@ +import { ApplicationConfiguration } from '../models/application-configuration'; + +export function localize(resourceName: string, key: string, defaultValue: string) { + return function(localization: ApplicationConfiguration.Localization) { + if (resourceName === '_') return key; + + const resource = localization.values[resourceName]; + + if (!resource) return defaultValue; + + return resource[key] || defaultValue; + }; +} + +export function localizeWithFallback( + resourceNames: string[], + keys: string[], + defaultValue: string, +) { + return function(localization: ApplicationConfiguration.Localization) { + resourceNames = resourceNames.concat(localization.defaultResourceName).filter(Boolean); + + for (let i = 0; i < resourceNames.length; i++) { + const resourceName = resourceNames[i]; + + for (let j = 0; j < keys.length; j++) { + const localized = localize(resourceName, keys[j], null)(localization); + if (localized) return localized; + } + } + + return defaultValue; + }; +} From 3ebbb709f6ad1d9777457b6e794378ba88bbb74d Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Mon, 18 May 2020 21:08:12 +0300 Subject: [PATCH 2/7] fix: avoid lint errors --- .../packages/core/src/lib/utils/localization-utils.ts | 2 ++ .../src/lib/components/loader-bar/loader-bar.component.ts | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts index 7d5a2c9e70..530adc0621 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts @@ -1,6 +1,7 @@ import { ApplicationConfiguration } from '../models/application-configuration'; export function localize(resourceName: string, key: string, defaultValue: string) { + /* tslint:disable-next-line:only-arrow-functions */ return function(localization: ApplicationConfiguration.Localization) { if (resourceName === '_') return key; @@ -17,6 +18,7 @@ export function localizeWithFallback( keys: string[], defaultValue: string, ) { + /* tslint:disable-next-line:only-arrow-functions */ return function(localization: ApplicationConfiguration.Localization) { resourceNames = resourceNames.concat(localization.defaultResourceName).filter(Boolean); diff --git a/npm/ng-packs/packages/theme-shared/src/lib/components/loader-bar/loader-bar.component.ts b/npm/ng-packs/packages/theme-shared/src/lib/components/loader-bar/loader-bar.component.ts index 41a5850131..512d959c5f 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/components/loader-bar/loader-bar.component.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/components/loader-bar/loader-bar.component.ts @@ -30,10 +30,6 @@ export class LoaderBarComponent implements OnDestroy, OnInit { @Input() color = '#77b6ff'; - @Input() - filter = (action: StartLoader | StopLoader) => - action.payload.url.indexOf('openid-configuration') < 0; - @Input() isLoading = false; @@ -47,6 +43,10 @@ export class LoaderBarComponent implements OnDestroy, OnInit { stopDelay = 800; + @Input() + filter = (action: StartLoader | StopLoader) => + action.payload.url.indexOf('openid-configuration') < 0; + private readonly clearProgress = () => { this.progressLevel = 0; this.cdRef.detectChanges(); From 20bfd29a5c6250a6e095bd776a6959001e97b59d Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Mon, 18 May 2020 23:09:09 +0300 Subject: [PATCH 3/7] refactor: set array lengths before loops --- .../packages/core/src/lib/utils/localization-utils.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts index 530adc0621..2a14b8d014 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts @@ -22,10 +22,13 @@ export function localizeWithFallback( return function(localization: ApplicationConfiguration.Localization) { resourceNames = resourceNames.concat(localization.defaultResourceName).filter(Boolean); - for (let i = 0; i < resourceNames.length; i++) { + const resourceCount = resourceNames.length; + const keyCount = keys.length; + + for (let i = 0; i < resourceCount; i++) { const resourceName = resourceNames[i]; - for (let j = 0; j < keys.length; j++) { + for (let j = 0; j < keyCount; j++) { const localized = localize(resourceName, keys[j], null)(localization); if (localized) return localized; } From 6eb28e35c92a49acf7659d22fb25efafe5704580 Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Tue, 19 May 2020 17:34:17 +0300 Subject: [PATCH 4/7] refactor: reverse the higher-order functions --- .../src/lib/services/localization.service.ts | 30 +++-- .../src/lib/tests/localization-utils.spec.ts | 90 +++++++++++++++ .../lib/tests/localization.service.spec.ts | 108 +++++++++--------- .../packages/core/src/lib/utils/index.ts | 1 + .../core/src/lib/utils/localization-utils.ts | 30 +++-- 5 files changed, 179 insertions(+), 80 deletions(-) create mode 100644 npm/ng-packs/packages/core/src/lib/tests/localization-utils.spec.ts diff --git a/npm/ng-packs/packages/core/src/lib/services/localization.service.ts b/npm/ng-packs/packages/core/src/lib/services/localization.service.ts index 5fea751b14..8708eecfdb 100644 --- a/npm/ng-packs/packages/core/src/lib/services/localization.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/localization.service.ts @@ -7,7 +7,7 @@ import { SetLanguage } from '../actions/session.actions'; import { Config } from '../models/config'; import { ConfigState } from '../states/config.state'; import { registerLocale } from '../utils/initial-utils'; -import { localize, localizeWithFallback } from '../utils/localization-utils'; +import { createLocalizer, createLocalizerWithFallback } from '../utils/localization-utils'; type ShouldReuseRoute = (future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot) => boolean; @@ -79,17 +79,15 @@ export class LocalizationService { } localize(resourceName: string, key: string, defaultValue: string): Observable { - return this.store - .select(ConfigState.getOne('localization')) - .pipe(map(localize(resourceName, key, defaultValue))); + return this.store.select(ConfigState.getOne('localization')).pipe( + map(createLocalizer), + map(localize => localize(resourceName, key, defaultValue)), + ); } localizeSync(resourceName: string, key: string, defaultValue: string): string { - return localize( - resourceName, - key, - defaultValue, - )(this.store.selectSnapshot(ConfigState.getOne('localization'))); + const localization = this.store.selectSnapshot(ConfigState.getOne('localization')); + return createLocalizer(localization)(resourceName, key, defaultValue); } localizeWithFallback( @@ -97,16 +95,14 @@ export class LocalizationService { keys: string[], defaultValue: string, ): Observable { - return this.store - .select(ConfigState.getOne('localization')) - .pipe(map(localizeWithFallback(resourceNames, keys, defaultValue))); + return this.store.select(ConfigState.getOne('localization')).pipe( + map(createLocalizerWithFallback), + map(localizeWithFallback => localizeWithFallback(resourceNames, keys, defaultValue)), + ); } localizeWithFallbackSync(resourceNames: string[], keys: string[], defaultValue: string): string { - return localizeWithFallback( - resourceNames, - keys, - defaultValue, - )(this.store.selectSnapshot(ConfigState.getOne('localization'))); + const localization = this.store.selectSnapshot(ConfigState.getOne('localization')); + return createLocalizerWithFallback(localization)(resourceNames, keys, defaultValue); } } diff --git a/npm/ng-packs/packages/core/src/lib/tests/localization-utils.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/localization-utils.spec.ts new file mode 100644 index 0000000000..f8bead7eda --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/tests/localization-utils.spec.ts @@ -0,0 +1,90 @@ +import { createLocalizer, createLocalizerWithFallback } from '../utils/localization-utils'; + +describe('Localization Utils', () => { + describe('#createLocalizer', () => { + const localize = createLocalizer({ + values: { foo: { bar: 'baz' }, x: { y: 'z' } }, + defaultResourceName: 'x', + currentCulture: null, + languages: [], + }); + + test.each` + resource | key | defaultValue | expected + ${'_'} | ${'TEST'} | ${'DEFAULT'} | ${'TEST'} + ${'foo'} | ${'bar'} | ${'DEFAULT'} | ${'baz'} + ${'x'} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} + ${'a'} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} + ${''} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} + ${undefined} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} + ${'foo'} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} + ${'x'} | ${'y'} | ${'DEFAULT'} | ${'z'} + ${'a'} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} + ${''} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} + ${undefined} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} + ${'foo'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} + ${'x'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} + ${'a'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} + ${''} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} + ${undefined} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} + ${'foo'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} + ${'x'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} + ${'a'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} + ${''} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} + ${undefined} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} + `( + 'should return $expected when resource name is $resource and key is $key', + ({ resource, key, defaultValue, expected }) => { + const result = localize(resource, key, defaultValue); + + expect(result).toBe(expected); + }, + ); + }); + + describe('#createLocalizerWithFallback', () => { + const localizeWithFallback = createLocalizerWithFallback({ + values: { foo: { bar: 'baz' }, x: { y: 'z' } }, + defaultResourceName: 'x', + currentCulture: null, + languages: [], + }); + + test.each` + resources | keys | defaultValue | expected + ${['', '_']} | ${['TEST', 'OTHER']} | ${'DEFAULT'} | ${'TEST'} + ${['foo']} | ${['bar']} | ${'DEFAULT'} | ${'baz'} + ${['x']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['a', 'b', 'c']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${[]} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['foo']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['x']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['a', 'b', 'c']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${[]} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['foo']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'baz'} + ${['x']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${['a', 'b', 'c']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${['']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${[]} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${['foo']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['x']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['a', 'b', 'c']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${[]} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['foo']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${['x']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${['a', 'b', 'c']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${['']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${[]} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + `( + 'should return $expected when resource names are $resources and keys are $keys', + ({ resources, keys, defaultValue, expected }) => { + const result = localizeWithFallback(resources, keys, defaultValue); + + expect(result).toBe(expected); + }, + ); + }); +}); diff --git a/npm/ng-packs/packages/core/src/lib/tests/localization.service.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/localization.service.spec.ts index e17d53e463..9d6f6ba3bf 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/localization.service.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/localization.service.spec.ts @@ -79,6 +79,7 @@ describe('LocalizationService', () => { describe('#localize', () => { test.each` resource | key | defaultValue | expected + ${'_'} | ${'TEST'} | ${'DEFAULT'} | ${'TEST'} ${'foo'} | ${'bar'} | ${'DEFAULT'} | ${'baz'} ${'x'} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} ${'a'} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} @@ -119,6 +120,7 @@ describe('LocalizationService', () => { describe('#localizeSync', () => { test.each` resource | key | defaultValue | expected + ${'_'} | ${'TEST'} | ${'DEFAULT'} | ${'TEST'} ${'foo'} | ${'bar'} | ${'DEFAULT'} | ${'baz'} ${'x'} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} ${'a'} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} @@ -156,32 +158,33 @@ describe('LocalizationService', () => { describe('#localizeWithFallback', () => { test.each` - resources | keys | defaultValue | expected - ${['foo']} | ${['bar']} | ${'DEFAULT'} | ${'baz'} - ${['x']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} - ${['a', 'b', 'c']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} - ${['']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} - ${[]} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} - ${['foo']} | ${['y']} | ${'DEFAULT'} | ${'z'} - ${['x']} | ${['y']} | ${'DEFAULT'} | ${'z'} - ${['a', 'b', 'c']} | ${['y']} | ${'DEFAULT'} | ${'z'} - ${['']} | ${['y']} | ${'DEFAULT'} | ${'z'} - ${[]} | ${['y']} | ${'DEFAULT'} | ${'z'} - ${['foo']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'baz'} - ${['x']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} - ${['a', 'b', 'c']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} - ${['']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} - ${[]} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} - ${['foo']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} - ${['x']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} - ${['a', 'b', 'c']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} - ${['']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} - ${[]} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} - ${['foo']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} - ${['x']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} - ${['a', 'b', 'c']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} - ${['']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} - ${[]} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + resources | keys | defaultValue | expected + ${['', '_']} | ${['TEST', 'OTHER']} | ${'DEFAULT'} | ${'TEST'} + ${['foo']} | ${['bar']} | ${'DEFAULT'} | ${'baz'} + ${['x']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['a', 'b', 'c']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${[]} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['foo']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['x']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['a', 'b', 'c']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${[]} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['foo']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'baz'} + ${['x']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${['a', 'b', 'c']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${['']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${[]} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${['foo']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['x']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['a', 'b', 'c']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${[]} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['foo']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${['x']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${['a', 'b', 'c']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${['']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${[]} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} `( 'should return observable $expected when resource names are $resources and keys are $keys', async ({ resources, keys, defaultValue, expected }) => { @@ -203,32 +206,33 @@ describe('LocalizationService', () => { describe('#localizeWithFallbackSync', () => { test.each` - resources | keys | defaultValue | expected - ${['foo']} | ${['bar']} | ${'DEFAULT'} | ${'baz'} - ${['x']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} - ${['a', 'b', 'c']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} - ${['']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} - ${[]} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} - ${['foo']} | ${['y']} | ${'DEFAULT'} | ${'z'} - ${['x']} | ${['y']} | ${'DEFAULT'} | ${'z'} - ${['a', 'b', 'c']} | ${['y']} | ${'DEFAULT'} | ${'z'} - ${['']} | ${['y']} | ${'DEFAULT'} | ${'z'} - ${[]} | ${['y']} | ${'DEFAULT'} | ${'z'} - ${['foo']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'baz'} - ${['x']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} - ${['a', 'b', 'c']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} - ${['']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} - ${[]} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} - ${['foo']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} - ${['x']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} - ${['a', 'b', 'c']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} - ${['']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} - ${[]} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} - ${['foo']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} - ${['x']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} - ${['a', 'b', 'c']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} - ${['']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} - ${[]} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + resources | keys | defaultValue | expected + ${['', '_']} | ${['TEST', 'OTHER']} | ${'DEFAULT'} | ${'TEST'} + ${['foo']} | ${['bar']} | ${'DEFAULT'} | ${'baz'} + ${['x']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['a', 'b', 'c']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${[]} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['foo']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['x']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['a', 'b', 'c']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['']} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${[]} | ${['y']} | ${'DEFAULT'} | ${'z'} + ${['foo']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'baz'} + ${['x']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${['a', 'b', 'c']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${['']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${[]} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} + ${['foo']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['x']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['a', 'b', 'c']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${[]} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['foo']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${['x']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${['a', 'b', 'c']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${['']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${[]} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} `( 'should return $expected when resource names are $resources and keys are $keys', ({ resources, keys, defaultValue, expected }) => { diff --git a/npm/ng-packs/packages/core/src/lib/utils/index.ts b/npm/ng-packs/packages/core/src/lib/utils/index.ts index 6f6b0585d9..4dde8134ef 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/index.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/index.ts @@ -3,6 +3,7 @@ export * from './form-utils'; export * from './generator-utils'; export * from './initial-utils'; export * from './lazy-load-utils'; +export * from './localization-utils'; export * from './number-utils'; export * from './route-utils'; export * from './rxjs-utils'; diff --git a/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts index 2a14b8d014..31a73b818d 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts @@ -1,8 +1,8 @@ import { ApplicationConfiguration } from '../models/application-configuration'; -export function localize(resourceName: string, key: string, defaultValue: string) { +export function createLocalizer(localization: ApplicationConfiguration.Localization) { /* tslint:disable-next-line:only-arrow-functions */ - return function(localization: ApplicationConfiguration.Localization) { + return function(resourceName: string, key: string, defaultValue: string) { if (resourceName === '_') return key; const resource = localization.values[resourceName]; @@ -13,13 +13,20 @@ export function localize(resourceName: string, key: string, defaultValue: string }; } -export function localizeWithFallback( - resourceNames: string[], - keys: string[], - defaultValue: string, -) { +export function createLocalizerWithFallback(localization: ApplicationConfiguration.Localization) { + const findLocalization = createLocalizationFinder(localization); /* tslint:disable-next-line:only-arrow-functions */ - return function(localization: ApplicationConfiguration.Localization) { + return function(resourceNames: string[], keys: string[], defaultValue: string) { + const { localized } = findLocalization(resourceNames, keys); + return localized || defaultValue; + }; +} + +function createLocalizationFinder(localization: ApplicationConfiguration.Localization) { + const localize = createLocalizer(localization); + + /* tslint:disable-next-line:only-arrow-functions */ + return function(resourceNames: string[], keys: string[]) { resourceNames = resourceNames.concat(localization.defaultResourceName).filter(Boolean); const resourceCount = resourceNames.length; @@ -29,11 +36,12 @@ export function localizeWithFallback( const resourceName = resourceNames[i]; for (let j = 0; j < keyCount; j++) { - const localized = localize(resourceName, keys[j], null)(localization); - if (localized) return localized; + const key = keys[j]; + const localized = localize(resourceName, key, null); + if (localized) return { resourceName, key, localized }; } } - return defaultValue; + return { resourceName: undefined, key: undefined, localized: undefined }; }; } From 024a64015789d35fee98425458cb0fd10d9d8618 Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Tue, 19 May 2020 17:34:31 +0300 Subject: [PATCH 5/7] feat: add createLocalizationPipeKeyGenerator --- .../src/lib/tests/localization-utils.spec.ts | 52 ++++++++++++++++++- .../core/src/lib/utils/localization-utils.ts | 12 +++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/npm/ng-packs/packages/core/src/lib/tests/localization-utils.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/localization-utils.spec.ts index f8bead7eda..f5319edc2d 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/localization-utils.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/localization-utils.spec.ts @@ -1,4 +1,8 @@ -import { createLocalizer, createLocalizerWithFallback } from '../utils/localization-utils'; +import { + createLocalizationPipeKeyGenerator, + createLocalizer, + createLocalizerWithFallback, +} from '../utils/localization-utils'; describe('Localization Utils', () => { describe('#createLocalizer', () => { @@ -87,4 +91,50 @@ describe('Localization Utils', () => { }, ); }); + + describe('#createLocalizationPipeKeyGenerator', () => { + const generateLocalizationPipeKey = createLocalizationPipeKeyGenerator({ + values: { foo: { bar: 'baz' }, x: { y: 'z' } }, + defaultResourceName: 'x', + currentCulture: null, + languages: [], + }); + + test.each` + resources | keys | defaultKey | expected + ${['', '_']} | ${['TEST', 'OTHER']} | ${'DEFAULT'} | ${'TEST'} + ${['foo']} | ${['bar']} | ${'DEFAULT'} | ${'foo::bar'} + ${['x']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['a', 'b', 'c']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${[]} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['foo']} | ${['y']} | ${'DEFAULT'} | ${'x::y'} + ${['x']} | ${['y']} | ${'DEFAULT'} | ${'x::y'} + ${['a', 'b', 'c']} | ${['y']} | ${'DEFAULT'} | ${'x::y'} + ${['']} | ${['y']} | ${'DEFAULT'} | ${'x::y'} + ${[]} | ${['y']} | ${'DEFAULT'} | ${'x::y'} + ${['foo']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'foo::bar'} + ${['x']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'x::y'} + ${['a', 'b', 'c']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'x::y'} + ${['']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'x::y'} + ${[]} | ${['bar', 'y']} | ${'DEFAULT'} | ${'x::y'} + ${['foo']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['x']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['a', 'b', 'c']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${[]} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} + ${['foo']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${['x']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${['a', 'b', 'c']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${['']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + ${[]} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} + `( + 'should return $expected when resource names are $resources and keys are $keys', + ({ resources, keys, defaultKey, expected }) => { + const result = generateLocalizationPipeKey(resources, keys, defaultKey); + + expect(result).toBe(expected); + }, + ); + }); }); diff --git a/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts index 31a73b818d..a30c4e25a6 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts @@ -22,6 +22,18 @@ export function createLocalizerWithFallback(localization: ApplicationConfigurati }; } +export function createLocalizationPipeKeyGenerator( + localization: ApplicationConfiguration.Localization, +) { + const findLocalization = createLocalizationFinder(localization); + + /* tslint:disable-next-line:only-arrow-functions */ + return function(resourceNames: string[], keys: string[], defaultKey: string) { + const { resourceName, key } = findLocalization(resourceNames, keys); + return !resourceName ? defaultKey : resourceName === '_' ? key : `${resourceName}::${key}`; + }; +} + function createLocalizationFinder(localization: ApplicationConfiguration.Localization) { const localize = createLocalizer(localization); From 8c184b7f41125034841e7c96ce128f85a3f9c122 Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Tue, 19 May 2020 22:43:35 +0300 Subject: [PATCH 6/7] refactor: use arrow functions --- .../core/src/lib/utils/localization-utils.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts index a30c4e25a6..ed094f7fc7 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/localization-utils.ts @@ -1,8 +1,7 @@ import { ApplicationConfiguration } from '../models/application-configuration'; export function createLocalizer(localization: ApplicationConfiguration.Localization) { - /* tslint:disable-next-line:only-arrow-functions */ - return function(resourceName: string, key: string, defaultValue: string) { + return (resourceName: string, key: string, defaultValue: string) => { if (resourceName === '_') return key; const resource = localization.values[resourceName]; @@ -15,8 +14,8 @@ export function createLocalizer(localization: ApplicationConfiguration.Localizat export function createLocalizerWithFallback(localization: ApplicationConfiguration.Localization) { const findLocalization = createLocalizationFinder(localization); - /* tslint:disable-next-line:only-arrow-functions */ - return function(resourceNames: string[], keys: string[], defaultValue: string) { + + return (resourceNames: string[], keys: string[], defaultValue: string) => { const { localized } = findLocalization(resourceNames, keys); return localized || defaultValue; }; @@ -27,8 +26,7 @@ export function createLocalizationPipeKeyGenerator( ) { const findLocalization = createLocalizationFinder(localization); - /* tslint:disable-next-line:only-arrow-functions */ - return function(resourceNames: string[], keys: string[], defaultKey: string) { + return (resourceNames: string[], keys: string[], defaultKey: string) => { const { resourceName, key } = findLocalization(resourceNames, keys); return !resourceName ? defaultKey : resourceName === '_' ? key : `${resourceName}::${key}`; }; @@ -37,8 +35,7 @@ export function createLocalizationPipeKeyGenerator( function createLocalizationFinder(localization: ApplicationConfiguration.Localization) { const localize = createLocalizer(localization); - /* tslint:disable-next-line:only-arrow-functions */ - return function(resourceNames: string[], keys: string[]) { + return (resourceNames: string[], keys: string[]) => { resourceNames = resourceNames.concat(localization.defaultResourceName).filter(Boolean); const resourceCount = resourceNames.length; From a3fb373e013f4048fa38d7b0fab4cde14fd13462 Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Tue, 19 May 2020 23:21:55 +0300 Subject: [PATCH 7/7] refactor: add a getter for languageChange stream --- .../core/src/lib/services/localization.service.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/services/localization.service.ts b/npm/ng-packs/packages/core/src/lib/services/localization.service.ts index 8708eecfdb..408dea1058 100644 --- a/npm/ng-packs/packages/core/src/lib/services/localization.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/localization.service.ts @@ -20,6 +20,10 @@ export class LocalizationService { return this.store.selectSnapshot(state => state.SessionState.language); } + get languageChange(): Observable { + return this.actions.pipe(ofActionSuccessful(SetLanguage)); + } + constructor( private actions: Actions, private store: Store, @@ -35,9 +39,7 @@ export class LocalizationService { } private listenToSetLanguage() { - this.actions - .pipe(ofActionSuccessful(SetLanguage)) - .subscribe(({ payload }) => this.registerLocale(payload)); + this.languageChange.subscribe(({ payload }) => this.registerLocale(payload)); } setRouteReuse(reuse: ShouldReuseRoute) {