Localization

Before you read about the Localization Pipe and the Localization Service, you should know about localization keys.

The Localization key format consists of 2 sections which are Resource Name and Key. ResourceName::Key

If you do not specify the resource name, it will be defaultResourceName which is declared in environment.ts

  1. const environment = {
  2. //...
  3. localization: {
  4. defaultResourceName: 'MyProjectName',
  5. },
  6. };

So these two are the same:

  1. <h1>{{ '::Key' | abpLocalization }}</h1>
  2. <h1>{{ 'MyProjectName::Key' | abpLocalization }}</h1>

Using the Localization Pipe

You can use the abpLocalization pipe to get localized text as in this example:

  1. <h1>{{ 'Resource::Key' | abpLocalization }}</h1>

The pipe will replace the key with the localized text.

You can also specify a default value as shown below:

  1. <h1>{{ { key: 'Resource::Key', defaultValue: 'Default Value' } | abpLocalization }}</h1>

To use interpolation, you must give the values for interpolation as pipe parameters, for example:

Localization data is stored in key-value pairs:

  1. {
  2. //...
  3. AbpAccount: { // AbpAccount is the resource name
  4. Key: "Value",
  5. PagerInfo: "Showing {0} to {1} of {2} entries"
  6. }
  7. }

So we can use this key like this:

  1. <h1>{{ 'AbpAccount::PagerInfo' | abpLocalization:'20':'30':'50' }}</h1>
  2. <!-- Output: Showing 20 to 30 of 50 entries -->

Using the Localization Service

First of all you should import the LocalizationService from @abp/ng.core

  1. import { LocalizationService } from '@abp/ng.core';
  2. class MyClass {
  3. constructor(private localizationService: LocalizationService) {}
  4. }

After that, you are able to use localization service.

You can add interpolation parameters as arguments to instant() and get() methods.

  1. this.localizationService.instant('AbpIdentity::UserDeletionConfirmation', 'John');
  2. // with fallback value
  3. this.localizationService.instant(
  4. { key: 'AbpIdentity::UserDeletionConfirmation', defaultValue: 'Default Value' },
  5. 'John',
  6. );
  7. // Output
  8. // User 'John' will be deleted. Do you confirm that?

To get a localized text as Observable use get method instead of instant:

  1. this.localizationService.get('Resource::Key');
  2. // with fallback value
  3. this.localizationService.get({ key: 'Resource::Key', defaultValue: 'Default Value' });

Using the Config State

In order to you getLocalization method you should import ConfigState.

  1. import { ConfigState } from '@abp/ng.core';

Then you can use it as followed:

  1. this.store.selectSnapshot(ConfigState.getLocalization('ResourceName::Key'));

getLocalization method can be used with both localization key and LocalizationWithDefault interface.

  1. this.store.selectSnapshot(
  2. ConfigState.getLocalization(
  3. {
  4. key: 'AbpIdentity::UserDeletionConfirmation',
  5. defaultValue: 'Default Value',
  6. },
  7. 'John',
  8. ),
  9. );

Localization resources are stored in the localization property of ConfigState.

RTL Support

As of v2.9 ABP has RTL support. If you are generating a new project with v2.9 and above, everything is set, you do not need to do any changes. If you are migrating your project from an earlier version, please follow the 2 steps below:

Step 1. Create Chunks for Bootstrap LTR and RTL

Find styles configuration in angular.json and make sure the chunks in your project has bootstrap-rtl.min and bootstrap-ltr.min as shown below.

  1. {
  2. "projects": {
  3. "MyProjectName": {
  4. "architect": {
  5. "build": {
  6. "options": {
  7. "styles": [
  8. {
  9. "input": "node_modules/@fortawesome/fontawesome-free/css/all.min.css",
  10. "inject": true,
  11. "bundleName": "fontawesome-all.min"
  12. },
  13. {
  14. "input": "node_modules/@fortawesome/fontawesome-free/css/v4-shims.min.css",
  15. "inject": true,
  16. "bundleName": "fontawesome-v4-shims.min"
  17. },
  18. {
  19. "input": "node_modules/@abp/ng.theme.shared/styles/bootstrap-rtl.min.css",
  20. "inject": false,
  21. "bundleName": "bootstrap-rtl.min"
  22. },
  23. {
  24. "input": "node_modules/bootstrap/dist/css/bootstrap.min.css",
  25. "inject": true,
  26. "bundleName": "bootstrap-ltr.min"
  27. },
  28. "apps/dev-app/src/styles.scss"
  29. ],
  30. }
  31. }
  32. }
  33. }
  34. }
  35. }

Step 2. Clear Lazy Loaded Fontawesome in AppComponent

If you have created and injected chunks for Fontawesome as seen above, you no longer need the lazy loading in the AppComponent which was implemented before v2.9. Simply remove them. The AppComponent in the template of the new version looks like this:

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'app-root',
  4. template: `
  5. <abp-loader-bar></abp-loader-bar>
  6. <router-outlet></router-outlet>
  7. `,
  8. })
  9. 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

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

  1. // app.module.ts
  2. @NgModule({
  3. imports: [
  4. // other imports
  5. CoreModule.forRoot({
  6. // other options
  7. cultureNameLocaleFileMap: {
  8. "DotnetCultureName": "AngularLocaleFileName",
  9. "pt-BR": "pt" // example
  10. }
  11. })
  12. //...

See all locale files in Angular.

See Also

What’s Next?