Created
May 29, 2019 12:43
-
-
Save theodesp/de2eae45511ff8b107a461b4aefa7b3a to your computer and use it in GitHub Desktop.
This file contains 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 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