A translator library for Angular, based Translator Core
npm install rxjs @ubs-platform/translator-core @ubs-platform/translator-ngx
--legacy-peer-deps
It should work on Angular 17. However it not tested yet and i don't recommend this way either. We are working at the new upgrade for Angular 17
- Create directory named
lang
inapplication path/assets/
- And then; Create 3 json files in the
lang
directory
en.json:
[
{
"stringMap": {
"hello": "Hello, Welcome!"
}
},
{
"prefix": "languages",
"stringMap": {
"tr": "Turkish",
"en": "English",
"de": "German"
}
}
]
de.json
[
{
"stringMap": {
"hello": "Hallo, Willkommen!"
}
},
{
"prefix": "languages",
"stringMap": {
"tr": "Türkisch",
"en": "Englisch",
"de": "Deutsch"
}
}
]
tr.json:
[
{
"stringMap": {
"hello": "Merhaba, Hoş Geldiniz"
}
},
{
"prefix": "languages",
"stringMap": {
"tr": "Türkçe",
"en": "İngilizce",
"de": "Almanca"
}
}
]
Fun fact: In json files, you can define TranspationPart array or one translation part if you need just a TranspationPart
-
In app.module.ts or your library
-
Import
UbsTranslatorNgxModule
to usetranslate
pipe andHttpClientModule
to import required json files viaHttpClient
. -
Provide
TranslatorRepositoryService
to keep translations andLANGUAGE_JSON_URL
to load translation files as desired
-
import { HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { UbsTranslatorNgxModule, TranslatorRepositoryService, LANGUAGE_JSON_URL, EnvironmentController } from '@ubs-platform/translator-ngx';
import { AppComponent } from './app.component';
import { BrowserModule } from '@angular/platform-browser';
// Initialize the environment controller
// and start in default language
// and you can call it first at `main.ts` before module get bootstrapped
EnvironmentController.getEnvironmentController('en');
@NgModule({
imports: [UbsTranslatorNgxModule, HttpClientModule, BrowserModule],
providers: [
TranslatorRepositoryService,
{
provide: LANGUAGE_JSON_URL,
useValue: [(language: string) => `assets/lang/${language}.json`],
},
],
declarations: [AppComponent],
bootstrap: [AppComponent],
})
export class AppModule {}
- In component:
import { Component } from '@angular/core';
import { EnvironmentController, LANGUAGE_JSON_URL, TranslatorRepositoryService, UbsTranslatorNgxModule } from '@ubs-platform/translator-ngx';
import { HttpClientModule } from '@angular/common/http';
@Component({
selector: 'translator-ngx-example-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent {
/**
* Changes the language
*/
changeLanguage(arg0: string) {
EnvironmentController.getEnvironmentController().setLanguage(arg0);
}
title = 'translator-ngx-example';
}
- And the component template, you can use
translate
pipe to translating these texts
<button (click)="changeLanguage('en')">{{ 'languages.en' | translate }}</button>
<button (click)="changeLanguage('tr')">{{ 'languages.tr' | translate }}</button>
<button (click)="changeLanguage('de')">{{ 'languages.de' | translate }}</button>
<h1>{{ 'hello' | translate }}</h1>
In standalone components, there is similar operations in Angular modules.
-
Import
UbsTranslatorNgxModule
to usetranslate
pipe andHttpClientModule
to import required json files viaHttpClient
. -
Provide
TranslatorRepositoryService
to keep translations andLANGUAGE_JSON_URL
to load translation files as desired -
You can use same template and json files at in The section: In modular library/or application
// Initialize the environment controller and start in default language
EnvironmentController.getEnvironmentController('en');
import { Component } from '@angular/core';
import { EnvironmentController, LANGUAGE_JSON_URL, TranslatorRepositoryService, UbsTranslatorNgxModule } from '@ubs-platform/translator-ngx';
import { HttpClientModule } from '@angular/common/http';
@Component({
standalone: true,
imports: [UbsTranslatorNgxModule, HttpClientModule],
providers: [
TranslatorRepositoryService,
{
provide: LANGUAGE_JSON_URL,
useValue: [(language: string) => `assets/lang/${language}.json`],
},
],
selector: 'translator-ngx-example-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent {
changeLanguage(arg0: string) {
EnvironmentController.getEnvironmentController('en').setLanguage(arg0);
}
title = 'translator-ngx-example';
}
In translate part, some parameter definitions can be made with curly parentheses.
{
"stringMap": {
"person-saved": "The person named {name} has been saved"
}
}
Extra paramaters can be passed via
{{ "person-saved" | translate: {name: 'Jason'} }}
After the render, you will see as:
The person named Jason has been saved
We are planning to making documentation more detailed and upgrade the angular version at soon.
If you have any questions, improvements or any issue, you can open a issue or pull request without hesitation.
- Version of Translator Core library has been upgraded to 1.0.7
- Upgraded to Angular 16
- Readme.md file is edited
- Worked on getting runnable