translate
Lightweight (±3KB) translation library for Angular applications.Live Demo
Table of Contents
- Translate Pipe - Title Service
- [Translating application title](#translating-application-title)
- Translate Service- [Using from code](#using-from-code)
- [Custom language without external files](#custom-language-without-external-files)
- Formatted translations
- Testing components
- Watching for language change
- Custom translation path
- Loading from multiple locations
- Cache busting
- Define active language
- Restricting supported languages
- Using with your own pipes
- Lazy LoadingInstalling
npm install @ngstack/translate
Using with the application
Createen.json
file in the src/app/assets/i18n
folder of your application.{
"TITLE": "Hello from NgStack/translate!"
}
Import
TranslateModule
into you main application module,
configure TranslateService
to load during application startup.You will also need
HttpClientModule
module dependency.import { NgModule, APP_INITIALIZER } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { TranslateModule } from '@ngstack/translate';
// needed to load translation before application starts
export function setupTranslateService(service: TranslateService) {
return () => service.load();
}
@NgModule({
imports: [
BrowserModule,
HttpClientModule,
TranslateModule.forRoot({
// options
})
],
providers: [
// needed to load translation before application starts
{
provide: APP_INITIALIZER,
useFactory: setupTranslateService,
deps: [TranslateService],
multi: true
}
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule {}
In the main application template, use the following snippet:
<h2>
{{ 'TITLE' | translate }}
</h2>
Features
Translate Pipe
<element>{{ 'KEY' | translate }}</element>
<element [attribute]="property | translate"></element>
<element attribute="{{ property | translate }}"></element>
<element [innerHTML]="'KEY' | translate"></element>
<element>{{ 'PROPERTY.PATH' | translate }}</element>
<element>{{ 'FORMAT' | translate:params }}</element>
- (2.3.0)
<element [translate]="'KEY'"></element>
- (2.3.0)
<element [translate]="'FORMAT'" [translateParams]="{ msg: hello }"></element>
- (2.3.0)
<element translate="KEY"></element>
Title Service
- Sets page title value with automatic translation
- Watches for language changes and updates the title accordingly
Translating application title
Update the localization files for your application and addAPP.TITLE
resource key:{
"APP": {
"TITLE": "My Application"
}
}
Update the title from the code, the main application component is a perfect place for that:
import { TitleService } from '@ngstack/translate';
@Component({...})
export class AppComponent implements OnInit {
constructor(private titleService: TitleService) {}
ngOnInit() {
this.titleService.setTitle('APP.TITLE');
}
}
Now every time the language is changed, the page title is going to get changed automatically.
Translate Service
- Load translations on language change
- Translation from code
- Defining translation data from code
- Merging multiple translations
- Loading translations from multiple locations
- Automatic fallback for missing translations
- Defining supported languages
- Configurable cache busting
- Lazy loading support
- Visual debugging mode to simplify development and testing
Using from code
You can import and use translate service in the code:@Component({...})
export class MyComponent {
text: string;
constructor(translate: TranslateService) {
this.text = translate.get('SOME.PROPERTY.PATH');
}
}
Custom language without external files
An example for providing translation data from within the application, without loading external files.@NgModule({...})
export class AppModule {
constructor(translate: TranslateService) {
translate.use('en', {
'TITLE': 'Hello from @ngstack/translate!'
});
}
}
Formatted translations
You can use runtime string substitution when translating text{
"FORMATTED": {
"HELLO_MESSAGE": "Hello, {username}!"
}
}
Then in the HTML:
<div>{{ 'FORMATTED.HELLO_MESSAGE' | translate:{ 'username': 'world' } }}</div>
Or in the Code:
@Component({...})
export class MyComponent {
text: string;
constructor(translate: TranslateService) {
this.text = translate.get(
'FORMATTED.HELLO_MESSAGE',
{ username: 'world' }
);
}
}
Should produce the following result at runtime:
Hello, world!
You can use multiple values in the format string. Note, however, that TranslateService checks only the top-level properties of the parameter object.
Advanced topics
You can provide custom parameters for theforRoot
method of the TranslateModule
interface TranslateSettings {
debugMode?: boolean;
disableCache?: boolean;
supportedLangs?: string[];
translationRoot?: string;
translatePaths?: string[];
activeLang?: string;
}
For example:
TranslateModule.forRoot({
debugMode: true,
activeLang: 'fr'
});
Testing components
When testing localisation with a single translation file it is sometimes hard to tell if a component text switches to a different language. You can simplify testing of the end-applications and components by enabling the debug mode.While in the debug mode, the service automatically prepends active language id to very translated result. That allows to verify that your components support i18n correctly and do not contain hard-coded text.
TranslateModule.forRoot({
debugMode: true
});
Now, if using
en
as the active language, all strings should start with the [en]
prefix.Watching for language change
You can watch for language change event utilising theactiveLangChanged
event:@Component({...})
export class MyComponent {
constructor(translate: TranslateService) {
translate.activeLangChanged.subscribe(
(event: { previousValue: string; currentValue: string }) => {
console.log(event.previousValue);
console.log(event.currentValue);
}
);
}
}
Custom translation path
By default TranslateService loads files stored atassets/i18n
folder.
You can change the TranslateService.translationRoot
property to point to a custom location if needed.TranslateModule.forRoot({
translationRoot: '/some/path'
});
Loading from multiple locations
To provide multiple locations use theTranslateService.translatePaths
collection property.TranslateModule.forRoot({
translatePaths: ['assets/lib1/i18n', 'assets/lib2/i18n']
});
The files are getting fetched and merged in the order of declarations, and applied on the top of the default data loaded from
TranslateService.translationRoot
path.Cache busting
You can disable browser caching and force application always load translation files by usingTranslateService.disableCache
property.TranslateModule.forRoot({
disableCache: true
});
Define active language
The service takes browser language as an active language at startup. You can useactiveLang
property to define a custom value and override browser settings.TranslateModule.forRoot({
activeLang: 'fr'
});
Restricting supported languages
It is possible to restrict supported languages to a certain set of values. You can avoid unnecessary HTTP calls by providingTranslateService.supportedLangs
values.TranslateModule.forRoot({
supportedLangs: ['fr', 'de']
});
The service will try to load resource files only for given set of languages, and will use fallback language for all unspecified values.
By default this property is empty and service will probe all language files. The service always takes into account the Active and Fallback languages, even if you do not specify them in the list.
Using with your own pipes
It is possible to useTranslateService
with your own implementations.You can see the basic pipe implementation below:
import { Pipe, PipeTransform } from '@angular/core';
import { TranslateService, TranslateParams } from '@ngstack/translate';
@Pipe({
name: 'myTranslate',
pure: false
})
export class CustomTranslatePipe implements PipeTransform {
constructor(private translate: TranslateService) {}
transform(key: string, params?: TranslateParams): string {
return this.translate.get(key, params);
}
}
Then in the HTML templates you can use your pipe like following:
<p>
Custom Pipe: {{ 'TITLE' | myTranslate }}
</p>
Lazy Loading
To enable Lazy Loading useTranslateModule.forRoot()
in the main application,
and TranslateModule.forChild()
in all lazy-loaded feature modules.For more details please refer to Lazy Loading Feature Modules