Browse Source

Merge pull request #1838 from abpframework/fix/localization

feat(core): add default option to localization
pull/1841/head
Yasin Aydın 7 years ago
committed by GitHub
parent
commit
bf2e46f376
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      npm/ng-packs/packages/core/src/lib/models/config.ts
  2. 46
      npm/ng-packs/packages/core/src/lib/pipes/localization.pipe.ts
  3. 16
      npm/ng-packs/packages/core/src/lib/states/config.state.ts
  4. 5
      npm/ng-packs/packages/core/src/lib/states/session.state.ts
  5. 5
      npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.html
  6. 7
      npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.ts
  7. 4
      npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts

5
npm/ng-packs/packages/core/src/lib/models/config.ts

@ -30,4 +30,9 @@ export namespace Config {
export interface Requirements { export interface Requirements {
layouts: Type<any>[]; layouts: Type<any>[];
} }
export interface LocalizationWithDefault {
key: string;
defaultValue: string;
}
} }

46
npm/ng-packs/packages/core/src/lib/pipes/localization.pipe.ts

@ -1,46 +1,20 @@
import { Pipe, PipeTransform, OnDestroy } from '@angular/core'; import { Pipe, PipeTransform } from '@angular/core';
import { Store } from '@ngxs/store'; import { Store } from '@ngxs/store';
import { Config } from '../models';
import { ConfigState } from '../states'; import { ConfigState } from '../states';
import { takeUntilDestroy } from '../utils';
import { distinctUntilChanged, takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs';
@Pipe({ @Pipe({
name: 'abpLocalization', name: 'abpLocalization',
// tslint:disable-next-line: no-pipe-impure
pure: false // required to update the value
}) })
export class LocalizationPipe implements PipeTransform, OnDestroy { export class LocalizationPipe implements PipeTransform {
initialValue = '';
value: string;
destroy$ = new Subject();
constructor(private store: Store) {} constructor(private store: Store) {}
transform(value: string = '', ...interpolateParams: string[]): string { transform(value: string | Config.LocalizationWithDefault = '', ...interpolateParams: string[]): string {
if (this.initialValue !== value) { return this.store.selectSnapshot(
this.initialValue = value; ConfigState.getLocalization(
this.destroy$.next(); value,
...interpolateParams.reduce((acc, val) => (Array.isArray(val) ? [...acc, ...val] : [...acc, val]), []),
this.store ),
.select( );
ConfigState.getCopy(
value,
...interpolateParams.reduce((acc, val) => (Array.isArray(val) ? [...acc, ...val] : [...acc, val]), [])
)
)
.pipe(
takeUntil(this.destroy$),
takeUntilDestroy(this),
distinctUntilChanged()
)
.subscribe(copy => (this.value = copy));
}
return this.value;
} }
ngOnDestroy() {}
} }

16
npm/ng-packs/packages/core/src/lib/states/config.state.ts

@ -115,8 +115,7 @@ export class ConfigState {
/** /**
* *
* @param deprecated, Use getLocalization instead. To be delete in v1 * @deprecated, Use getLocalization instead. To be delete in v1
*
*/ */
static getCopy(key: string, ...interpolateParams: string[]) { static getCopy(key: string, ...interpolateParams: string[]) {
if (!key) key = ''; if (!key) key = '';
@ -166,14 +165,21 @@ export class ConfigState {
return selector; return selector;
} }
static getLocalization(key: string, ...interpolateParams: string[]) { static getLocalization(key: string | Config.LocalizationWithDefault, ...interpolateParams: string[]) {
let defaultValue: string;
if (typeof key !== 'string') {
defaultValue = key.defaultValue;
key = key.key;
}
if (!key) key = ''; if (!key) key = '';
const keys = key.split('::') as string[]; const keys = key.split('::') as string[];
const selector = createSelector( const selector = createSelector(
[ConfigState], [ConfigState],
(state: Config.State) => { (state: Config.State) => {
if (!state.localization) return key; if (!state.localization) return defaultValue || key;
const { defaultResourceName } = state.environment.localization; const { defaultResourceName } = state.environment.localization;
if (keys[0] === '') { if (keys[0] === '') {
@ -207,7 +213,7 @@ export class ConfigState {
}); });
} }
return localization || key; return localization || defaultValue || key;
}, },
); );

5
npm/ng-packs/packages/core/src/lib/states/session.state.ts

@ -4,6 +4,7 @@ import { ABP, Session } from '../models';
import { GetAppConfiguration } from '../actions/config.actions'; import { GetAppConfiguration } from '../actions/config.actions';
import { LocalizationService } from '../services/localization.service'; import { LocalizationService } from '../services/localization.service';
import { from, combineLatest } from 'rxjs'; import { from, combineLatest } from 'rxjs';
import { switchMap } from 'rxjs/operators';
@State<Session.State>({ @State<Session.State>({
name: 'SessionState', name: 'SessionState',
@ -28,7 +29,9 @@ export class SessionState {
language: payload, language: payload,
}); });
return combineLatest([dispatch(new GetAppConfiguration()), from(this.localizationService.registerLocale(payload))]); return dispatch(new GetAppConfiguration()).pipe(
switchMap(() => from(this.localizationService.registerLocale(payload))),
);
} }
@Action(SetTenant) @Action(SetTenant)

5
npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.html

@ -1,5 +1,5 @@
<div class="error"> <div class="error">
<button id="abp-close-button mr-4" type="button" class="close" (click)="destroy()"> <button id="abp-close-button mr-3" type="button" class="close" (click)="destroy()">
<span aria-hidden="true">&times;</span> <span aria-hidden="true">&times;</span>
</button> </button>
<div class="row centered"> <div class="row centered">
@ -13,7 +13,8 @@
</div> </div>
<div class="error-actions"> <div class="error-actions">
<a (click)="destroy()" routerLink="/" class="btn btn-primary btn-md mt-2" <a (click)="destroy()" routerLink="/" class="btn btn-primary btn-md mt-2"
><span class="glyphicon glyphicon-home"></span> {{ '::Menu:Home' | abpLocalization }} ><span class="glyphicon glyphicon-home"></span>
{{ { key: '::Menu:Home', defaultValue: 'Home' } | abpLocalization }}
</a> </a>
</div> </div>
</div> </div>

7
npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.ts

@ -1,14 +1,15 @@
import { Component, Renderer2, ElementRef } from '@angular/core'; import { Component, Renderer2, ElementRef } from '@angular/core';
import { Config } from '@abp/ng.core';
@Component({ @Component({
selector: 'abp-error', selector: 'abp-error',
templateUrl: './error.component.html', templateUrl: './error.component.html',
styleUrls: ['error.component.scss'] styleUrls: ['error.component.scss'],
}) })
export class ErrorComponent { export class ErrorComponent {
title = 'Oops!'; title: string | Config.LocalizationWithDefault = 'Oops!';
details = 'Sorry, an error has occured.'; details: string | Config.LocalizationWithDefault = 'Sorry, an error has occured.';
renderer: Renderer2; renderer: Renderer2;

4
npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts

@ -36,11 +36,11 @@ export const DEFAULT_ERROR_MESSAGES = {
}, },
defaultError500: { defaultError500: {
title: '500', title: '500',
details: 'AbpAccount::InternalServerErrorMessage', details: { key: 'AbpAccount::InternalServerErrorMessage', defaultValue: 'Error detail not sent by server.' },
}, },
defaultErrorUnknown: { defaultErrorUnknown: {
title: 'Unknown Error', title: 'Unknown Error',
details: 'AbpAccount::InternalServerErrorMessage', details: { key: 'AbpAccount::InternalServerErrorMessage', defaultValue: 'Error detail not sent by server.' },
}, },
}; };

Loading…
Cancel
Save