Skip to content

Instantly share code, notes, and snippets.

@theodesp
Created May 29, 2019 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theodesp/de2eae45511ff8b107a461b4aefa7b3a to your computer and use it in GitHub Desktop.
Save theodesp/de2eae45511ff8b107a461b4aefa7b3a to your computer and use it in GitHub Desktop.
import EventEmitter from 'https://unpkg.com/EventEmitter@1.0.0/src/index.js?module';
export class LocaleProvider {
constructor(currentLocale = 'en', availableLocales = ['en', 'el'], defaultLocale = 'en') {
this.availableLocales = availableLocales;
this.defaultLocale = defaultLocale;
this.emiter = new EventEmitter();
this.setCurrent(currentLocale);
}
getDefault() {
return this.defaultLocale;
}
getCurrent() {
return this.currentLocale;
}
setCurrent(tag) {
if (!this.availableLocales.includes(tag)) {
console.warn('Sorry ', tag, 'is not supported right now. Setting default');
this.currentLocale = this.defaultLocale;
}
this.currentLocale = tag;
this.emiter.emit('locale:changed', tag);
}
onChangeLocale(cb) {
this.emiter.on('locale:changed', cb);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment