Last active
October 12, 2017 15:58
-
-
Save rendfall/8473a75e6b96bcec8c68290dc362d1d0 to your computer and use it in GitHub Desktop.
Angular 2+ - internationalization with Polyglot.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
const Polyglot = require('node-polyglot'); // https://www.npmjs.com/package/node-polyglot | |
// Example of pl_pl.json file: { "Hello world":"Witaj świecie" } | |
let dictionaries = { | |
pl_pl: require('./pl_pl/pl_pl.json'), | |
en_gb: require('./en_gb/en_gb.json') | |
}; | |
@Injectable() | |
export class I18nService { | |
private polyglot = null; | |
constructor() { | |
let locale = 'pl_pl'; | |
let phrases = dictionaries[locale]; | |
this.polyglot = new Polyglot({ locale, phrases }); | |
} | |
translate(phrase) { | |
return this.polyglot.t(phrase); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment