@nwx/i18n
A simple translation module for Angular applications
How to install
npm i @nwx/i18n |OR| yarn add @nwx/i18n
How to use
import { AppCfg, TargetPlatform, HttpMethod } from '@nwx/cfg';
import { LogLevels } from '@nwx/logger';
export const environment: AppCfg = {
appName: 'Neekware',
target: TargetPlatform.web,
production: false,
log: {
level: LogLevels.info
},
i18n: {
availableLanguages: {
en: {
name: 'English',
locale: '@angular/common/locales/en',
localeExtra: '@angular/common/locales/extra/en'
},
fr: {
name: 'Français',
locale: '@angular/common/locales/fr',
localeExtra: '@angular/common/locales/extra/fr'
},
de: {
name: 'Deutsch',
locale: '@angular/common/locales/de',
localeExtra: '@angular/common/locales/extra/de'
}
},
enabledLanguages: [
'en',
'fr'
]
}
};
import { CfgModule } from '@nwx/cfg';
import { LoggerModule } from '@nwx/logger';
import { environment } from '../environments/environment';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
CfgModule.forRoot(environment),
LoggerModule,
I18nModule.forRoot()
],
bootstrap: [AppComponent]
})
export class AppModule {}
import { Component } from '@angular/core';
import { CfgService, DefaultCfg } from '@nwx/cfg';
import { LogService } from '@nwx/logger';
import { I18nService } from '@nwx/i18n';
@Component({
selector: 'app-root',
template: `<h1>{{'COMMON.WELCOME' | translate}} to {{ title }}!</h1>`
})
export class AppComponent {
title = 'Neekware';
constructor(public cfg: CfgService, public log: LogService, public i18n: I18nService) {
this.title = this.cfg.options.appName;
this.log.info('AppComponent loaded ...');
}
}
Include your translated files in the /assets/i18n
directory of your application.
/assets/i18n/en.json
{
"COMMON.WELCOME": "Welcome",
"COMMON.ABOUT": "About"
}
/assets/i18n/fr.json
{
"COMMON.WELCOME": "Bienvenue",
"COMMON.ABOUT": "Sur"
}
Advanced usage:
import { Component } from '@angular/core';
import { CfgService, DefaultCfg } from '@nwx/cfg';
import { LogService } from '@nwx/logger';
import { I18nService } from '@nwx/i18n';
@Component({
selector: 'app-root',
template: `<h1>{{'COMMON.WELCOME' | translate}} to {{ title }}!</h1>`
})
export class AppComponent {
direction = 'ltr';
title = 'Neekware';
constructor(public cfg: CfgService, public log: LogService, public i18n: I18nService) {
this.title = this.cfg.options.appName;
i18n.xlate.get('COMMON.WELCOME').subscribe((res: string) => {
this.log.info(res);
});
if (i18n.isLanguageRTL('he')) {
this.direction = 'rtl';
}
i18n.setCurrentLanguage('fr');
}
}
Note:
@nwx/i18n
uses the great @ngx-translate
package under the hood.
Running the tests
To run the tests against the current environment:
npm run ci:all
License
Released under a (MIT) license.
Version
X.Y.Z Version
`MAJOR` version -- making incompatible API changes
`MINOR` version -- adding functionality in a backwards-compatible manner
`PATCH` version -- making backwards-compatible bug fixes