Localize custom UI component implemented using remote module
Localize custom UI components implemented using remote module during development to save time spent on translating the ready-to-use custom UI component.
Localize custom validators, converters and request handlers
Localize custom validators, converters and request handlers using Angular DI (dependency injection). To do this:
-
Retrieve the Creatio UI language of the current user from the
userCulture
system variable. We recommend using a dedicated function. To do this, import theSysValuesService
functionality from the@creatio-devkit/common
library into the component.@creatio-devkit/common
includes theSysValuesService
service to localize system variables.View the example that retrieves the Creatio UI language of the current user in the custom UI component implemented using remote module below.
some-component.component.ts/* Import the required functionality from the libraries. */
import { SysValuesService } from '@creatio-devkit/common';
...
constructor() {}
async function getUserCulture(): Promise<void> {
/* Create an instance of the "SysValuesService" service from "@creatio-devkit/common." */
const sysValuesService = new SysValuesService();
/* Upload the system variable values. */
const sysValues = await sysValuesService.loadSysValues();
return sysValues.userCulture.displayValue;
} -
Use an external library or implement the custom service to localize custom UI component. We recommend using
@ngx-translate
external library to localize custom UI components. Learn more: official vendor documentation (GitHub).For example,
SomeTranslationService
is an external library or custom service to localize custom UI components. -
If needed, enable support for emitting type metadata for decorators that works with the
reflect-metadata
module.-
Install the
reflect-metadata
npm package if needed. To do this, run thenpm i reflect-metadata
command at the command line terminal of Microsoft Visual Studio Code. The installation might take some time. -
Open the
tsconfig.json
file of the Angular project and make sure theexperimentalDecorators
andemitDecoratorMetadata
properties are set totrue
.tsconfig.json file{
...,
"compilerOptions": {
...,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
...,
},
}
-
-
Receive the dependencies of the validator, converter, or request handler.
- Open the
app.module.ts
file. - If needed, implement the
resolveDependency()
method in thebootstrapCrtModule()
method. ThebootstrapCrtModule()
method registers the custom validator, converter, or request handler flagged using theCrtModule
decorator. TheresolveDependency()
method receives the dependencies of the validator, converter, or request handler. - Import the required functionality from the libraries into the class.
- Save the file.
View an example that receives the dependencies of the custom validator, converter, or request handler using the
resolveDependency()
method below.app.module.ts file/* Import the required functionality from the libraries. */
import { DoBootstrap, Injector, ProviderToken } from '@angular/core';
import { bootstrapCrtModule } from '@creatio-devkit/common';
...
export class AppModule implements DoBootstrap {
constructor(private _injector: Injector) {}
ngDoBootstrap(): void {
/* Bootstrap "CrtModule" definitions. */
bootstrapCrtModule('some_package', AppModule, {
/* Receive the dependencies. */
resolveDependency: (token) => this._injector.get(<ProviderToken<unknown>>token),
});
}
} - Open the
-
Retrieve the instance of the translation service.
- Open the file that implements a custom validator, converter or request handler. For example, open the
some-handler.handler.ts
file that implements a custom request handler. - Receive the instant translated value using the
instant()
method. - Import the required functionality from the libraries into the class.
- Save the file.
View an example that retrieves the instance of the
SomeTranslationService
using Angular DI (dependency injection) below.some-handler.handler.ts file/* Import the required functionality from the libraries. */
import { BaseRequestHandler, CrtRequestHandler } from "@creatio-devkit/common";
import { SomeRequest } from "./some-request.request";
import { SomeTranslationService } from "./some-translation-service.service";
/* Add the "CrtRequestHandler" decorator to the "SomeHandler" class. */
@CrtRequestHandler({
type: 'usr.SomeHandler',
requestType: 'usr.SomeRequest',
})
export class SomeHandler extends BaseRequestHandler{
constructor(private _someTranslationService: SomeTranslationService) {
super();
}
public async handle(request: SomeRequest): Promise<void> {
/* Receive the instant translated value. */
const localizedMessage = this._someTranslationService.instant('SomeRequest.Message');
alert(localizedMessage);
}
} - Open the file that implements a custom validator, converter or request handler. For example, open the
Localize properties of custom UI component
-
Retrieve the Creatio UI language of the current user from the
userCulture
system variable. We recommend using a dedicated function. To do this, import theSysValuesService
functionality from the@creatio-devkit/common
library into the component.@creatio-devkit/common
includes theSysValuesService
service to localize system variables.View the example that retrieves the Creatio UI language of the current user in the custom UI component implemented using remote module below.
some-component.component.ts/* Import the required functionality from the libraries. */
import { SysValuesService } from '@creatio-devkit/common';
...
constructor() {}
async function getUserCulture(): Promise<void> {
/* Create an instance of the "SysValuesService" service from "@creatio-devkit/common." */
const sysValuesService = new SysValuesService();
/* Upload the system variable values. */
const sysValues = await sysValuesService.loadSysValues();
return sysValues.userCulture.displayValue;
} -
Use an external library or implement the custom service to localize custom UI component. We recommend using
@ngx-translate
external library to localize custom UI component. Learn more: official vendor documentation (GitHub).For example,
SomeTranslationService
is an external library or custom service to localize custom UI components. -
Flag the properties to localize.
- Open the
some_component.component.ts
file. - Flag the component using the
CrtInterfaceDesignerItem
decorator that has thetoolbarConfig
property. The property manages the element layout in the library of the Freedom UI Designer. - Import the functionality of the
CrtInterfaceDesignerItem
decorator from the@creatio-devkit/common
library into the component. - Mark the properties to localize using the
localize()
method. - Save the file.
View the example that flags the
Caption
property of theSomeComponent
custom UI component implemented using remote module below.some_component.component.ts file/* Import the required functionality from the libraries. */
import { CrtInterfaceDesignerItem } from '@creatio-devkit/common';
...
/* Add the "CrtInterfaceDesignerItem" decorator to the "SomeComponent" component. */
@CrtInterfaceDesignerItem({
/* Manage the element layout in the library of the Freedom UI Designer. */
toolbarConfig: {
/* The localizable name of the component. */
caption: localize('SomeComponent.Caption'),
...
}
})
... - Open the
-
Translate the localizable values of the validator, converter or request handler.
- Open the
app.module.ts
file. - Call the
localizeMetadata()
method in thebootstrapCrtModule()
method. ThelocalizeMetadata()
method is required to translate the properties flagged using thelocalize()
method. The key to translate the value is an incoming parameter of thelocalizeMetadata()
method. The method returns the translated value. - Import the required functionality from the libraries into the class.
- Save the file.
View an example that calls the
localizeMetadata()
method below.app.module.ts file/* Import the required functionality from the libraries. */
import { DoBootstrap, Injector } from '@angular/core';
import { bootstrapCrtModule } from '@creatio-devkit/common';
import { SomeTranslationService } from "./some-translation-service.service";
...
export class AppModule implements DoBootstrap {
constructor(private _injector: Injector) {}
ngDoBootstrap(): void {
const translationService = this._injector.get(SomeTranslationService);
/* Bootstrap "CrtModule" definitions. */
bootstrapCrtModule('some_package', AppModule, {
localizeMetadata: (key: string) => translationService.instant(key),
});
}
} - Open the
Upload the translations of custom UI component from static content
- Find the URL to upload the translations. To do this, use the
__webpack_public_path__
global variable. Learn more: official webpack documentation. - Open the
app.module.ts
file. - Add app initializer that requests the Creatio UI language of the current user and saves the value to the external library or custom service to localize custom UI component.
- Implement the mechanism that uploads translations.
- Save the file.
View an example that uploads the translations of custom UI component from static content below.
/* Import the required functionality from the libraries. */
import { BrowserModule } from "@angular/platform-browser";
import { HttpClient } from '@angular/core';
import { HttpClientModule } from '@creatio-devkit/common';
import { NgModule } from "@angular/core";
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { SomeTranslationService } from "./some-translation-service.service";
declare const __webpack_public_path__: string;
@NgModule({
providers: [{
provide: APP_INITIALIZER,
useFactory: (someTranslationService) => async () => {
const culture = await getUserCulture();
someTranslationService.use(culture)
},
multi: true,
deps: [SomeTranslationService],
}],
imports: [
BrowserModule,
HttpClientModule,
TranslateModule.forRoot({
defaultLanguage: 'en-US',
loader: {
provide: TranslateLoader,
useFactory: (httpClient: HttpClient) => {
return new TranslateHttpLoader(
httpClient,
__webpack_public_path__ + '/assets/i18n/',
'.json'
);
},
deps: [HttpClient],
},
}),
],
...
})
See also
Custom UI component implemented using remote module
Custom validator implemented using remote module
Custom converter implemented using remote module
Custom request handler implemented using remote module
Resources
__webpack_public_path__
global variable (official webpack documentation)