1.0.3 • Published 8 years ago
angular-internationalisation v1.0.3
angular-internationalisation for Angular 2.x
Usage
Installation
npm install angular-internationalisation
Basic usage
In order to use angular-internationalisation, simply add the dependency to your module definition, like this:
import { InternationalisationModule } from "angular-internationalisation";
@NgModule({
imports: [ InternationalisationModule ]
})
export class AppModule {}Next, inject InternationalisationService somewhere, for example into a component or service, and define the locales you want your app to use:
import { InternationalisationService } from "angular-internationalisation";
@Injectable()
export class ExampleService {
constructor (private internationalisationService: InternationalisationService) {}
doSomething (): void {
this.internationalisationService.setLocales({
de: {
welcomeMessage: "Hallo!",
textBody: "Beispieltext"
},
en: {
welcomeMessage: "Hello!",
textBody: "Sample text"
}
});
}
}To set the current locale to English (for example), use this.internationalisationService.setCurrentLocale("en"). The current locale is persisted into the browser's localStorage. This means, it is restored on page reload.
Include the actual text into your HTML-Markup as follows:
<body>
<h1>Demoapp</h1>
<div aw-int="welcomeMessage"></div>
<div aw-int="textBody"></div>
</body>Advanced usage
InternationalisationService exposes the following functions:
setLocalessets multiple locales (see example above). Should only be called once.setCurrentLocalesets the current locale (see example above)setDefaultLocalesets the default locale. The default locale is selected if no locale setting can be found in the browser'slocalStorage.setDefaultLocaleaccepts one parameter (the locale's name), just assetCurrentLocaledoes.getCurrentLocalegets the name of the current localegetCurrentLocaleValuegets the current locale's translation value for the specified key