Created
March 3, 2018 03:58
-
-
Save zrod/0b83cbf8d29e2a9b93f3854abe798fb6 to your computer and use it in GitHub Desktop.
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
// @flow | |
export default class I18n { | |
translations: { | |
messages: {}, | |
routes: {} | |
} = { | |
messages: {}, | |
routes: {} | |
}; | |
locale: string = 'pt_BR'; | |
constructor(): ?this { | |
const language: string = window.navigator.userLanguage || window.navigator.language; | |
this.setLocale(language); | |
/** @todo fix this */ | |
const messages: {} = { | |
'pt_BR': require('../translations/messages.pt_BR.js').default, | |
'en': require('../translations/messages.en.js').default | |
}; | |
const routes: {} = { | |
'pt_BR': require('../translations/routes.pt_BR.js').default, | |
'en': require('../translations/routes.en.js').default | |
}; | |
this.translations.messages = messages; | |
this.translations.routes = routes; | |
return this; | |
} | |
setLocale(newLocale: string) { | |
let localeCode: string = 'en'; | |
switch (newLocale) { | |
case 'pt-BR': | |
case 'pt_BR': | |
localeCode = 'pt_BR'; | |
break; | |
} | |
this.locale = localeCode; | |
} | |
getTranslations(): {} { | |
return { | |
...this.translations.messages[this.locale], | |
route: this.translations.routes[this.locale] | |
}; | |
} | |
} | |
const I18nClass = new I18n(); | |
export const i18nHelper = I18nClass; | |
export const i18n = i18nHelper.getTranslations(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment