Skip to content

Instantly share code, notes, and snippets.

@rendfall
Last active October 12, 2017 15:58
Show Gist options
  • Save rendfall/8473a75e6b96bcec8c68290dc362d1d0 to your computer and use it in GitHub Desktop.
Save rendfall/8473a75e6b96bcec8c68290dc362d1d0 to your computer and use it in GitHub Desktop.
Angular 2+ - internationalization with Polyglot.js
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