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

Added support for mapping of culture name to Angular locale file name #4868

Merged
merged 6 commits into from
Jul 24, 2020
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
28 changes: 27 additions & 1 deletion docs/en/UI/Angular/Localization.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,37 @@ import { Component } from '@angular/core';
export class AppComponent {}
```

## Mapping of Culture Name to Angular Locale File Name

Some of the culture names defined in .NET do not match Angular locales. In such cases, the Angular app throws an error like below at runtime:

![locale-error](./images/locale-error.png)

If you see an error like this, you should pass the `cultureNameToLocaleFileNameMapping` property like below to CoreModule's forRoot static method.

```js
// app.module.ts

@NgModule({
imports: [
// other imports
CoreModule.forRoot({
// other options
cultureNameToLocaleFileNameMapping: {
"DotnetCultureName": "AngularLocaleFileName",
"pt-BR": "pt" // example
}
})
//...
```

See [all locale files in Angular](https://github.com/angular/angular/tree/master/packages/common/locales).


## See Also

* [Localization in ASP.NET Core](../../Localization.md)

## What's Next?

* [Permission Management](./Permission-Management.md)
* [Permission Management](./Permission-Management.md)
Binary file added docs/en/UI/Angular/images/locale-error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 7 additions & 2 deletions npm/ng-packs/packages/core/src/lib/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { ConfigState } from './states/config.state';
import { ProfileState } from './states/profile.state';
import { ReplaceableComponentsState } from './states/replaceable-components.state';
import { SessionState } from './states/session.state';
import { CORE_OPTIONS } from './tokens/options.token';
import { CORE_OPTIONS, coreOptionsFactory } from './tokens/options.token';
import { noop } from './utils/common-utils';
import './utils/date-extensions';
import { getInitialData, localeInitializer, configureOAuth } from './utils/initial-utils';
Expand Down Expand Up @@ -171,9 +171,14 @@ export class CoreModule {
useValue: { environment: options.environment },
},
{
provide: CORE_OPTIONS,
provide: 'CORE_OPTIONS',
useValue: options,
},
{
provide: CORE_OPTIONS,
useFactory: coreOptionsFactory,
deps: ['CORE_OPTIONS'],
},
{
provide: HTTP_INTERCEPTORS,
useClass: ApiInterceptor,
Expand Down
1 change: 1 addition & 0 deletions npm/ng-packs/packages/core/src/lib/models/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export namespace ABP {
environment: Partial<Config.Environment>;
skipGetAppConfiguration?: boolean;
sendNullsAsQueryParam?: boolean;
cultureNameToLocaleFileNameMapping?: Dictionary<string>;
}

export interface Test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Config } from '../models/config';
import { ConfigState } from '../states/config.state';
import { registerLocale } from '../utils/initial-utils';
import { createLocalizer, createLocalizerWithFallback } from '../utils/localization-utils';
import { CORE_OPTIONS } from '../tokens/options.token';

type ShouldReuseRoute = (future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot) => boolean;

Expand Down Expand Up @@ -44,11 +45,12 @@ export class LocalizationService {

registerLocale(locale: string) {
const router = this.injector.get(Router);
const { cultureNameToLocaleFileNameMapping: localeNameMap } = this.injector.get(CORE_OPTIONS);
const { shouldReuseRoute } = router.routeReuseStrategy;
router.routeReuseStrategy.shouldReuseRoute = () => false;
router.navigated = false;

return registerLocale(locale).then(() => {
return registerLocale(locale, localeNameMap).then(() => {
this.ngZone.run(async () => {
await router.navigateByUrl(router.url).catch(noop);
router.routeReuseStrategy.shouldReuseRoute = shouldReuseRoute;
Expand Down
11 changes: 11 additions & 0 deletions npm/ng-packs/packages/core/src/lib/tokens/options.token.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { InjectionToken } from '@angular/core';
import { ABP } from '../models/common';
import differentLocales from '../constants/different-locales';

export const CORE_OPTIONS = new InjectionToken<ABP.Root>('CORE_OPTIONS');

export function coreOptionsFactory({
cultureNameToLocaleFileNameMapping: localeNameMap = {},
...options
}: ABP.Root) {
return {
...options,
cultureNameToLocaleFileNameMapping: { ...differentLocales, ...localeNameMap },
} as ABP.Root;
}
14 changes: 8 additions & 6 deletions npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Store } from '@ngxs/store';
import { OAuthService } from 'angular-oauth2-oidc';
import { tap } from 'rxjs/operators';
import { GetAppConfiguration } from '../actions/config.actions';
import differentLocales from '../constants/different-locales';
import { ABP } from '../models/common';
import { ConfigState } from '../states/config.state';
import { CORE_OPTIONS } from '../tokens/options.token';
Expand Down Expand Up @@ -45,22 +44,25 @@ function checkAccessToken(store: Store, injector: Injector) {
export function localeInitializer(injector: Injector) {
const fn = () => {
const store: Store = injector.get(Store);
const options = injector.get(CORE_OPTIONS);

const lang = store.selectSnapshot(state => state.SessionState.language) || 'en';

return new Promise((resolve, reject) => {
registerLocale(lang).then(() => resolve('resolved'), reject);
registerLocale(lang, options.cultureNameToLocaleFileNameMapping).then(
() => resolve('resolved'),
reject,
);
});
};

return fn;
}

export function registerLocale(locale: string) {
export function registerLocale(locale: string, localeNameMap: ABP.Dictionary<string>) {
return import(
/* webpackInclude: /(af|ar|am|ar-SA|as|az-Latn|be|bg|bn-BD|bn-IN|bs|ca|ca-ES-VALENCIA|cs|cy|da|de|de|el|en-GB|en|es|en|es-US|es-MX|et|eu|fa|fi|en|fr|fr|fr-CA|ga|gd|gl|gu|ha|he|hi|hr|hu|hy|id|ig|is|it|it|ja|ka|kk|km|kn|ko|kok|en|en|lb|lt|lv|en|mk|ml|mn|mr|ms|mt|nb|ne|nl|nl-BE|nn|en|or|pa|pa-Arab|pl|en|pt|pt-PT|en|en|ro|ru|rw|pa-Arab|si|sk|sl|sq|sr-Cyrl-BA|sr-Cyrl|sr-Latn|sv|sw|ta|te|tg|th|ti|tk|tn|tr|tt|ug|uk|ur|uz-Latn|vi|wo|xh|yo|zh-Hans|zh-Hant|zu)\.js$/ */
/* webpackChunkName: "[request]"*/
`@angular/common/locales/${differentLocales[locale] || locale}.js`
/* webpackChunkName: "_locale-[request]"*/
`@angular/common/locales/${localeNameMap[locale] || locale}.js`
).then(module => {
registerLocaleData(module.default);
});
Expand Down