Skip to content

Instantly share code, notes, and snippets.

@zrod
Created March 3, 2018 03:58
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 zrod/0b83cbf8d29e2a9b93f3854abe798fb6 to your computer and use it in GitHub Desktop.
Save zrod/0b83cbf8d29e2a9b93f3854abe798fb6 to your computer and use it in GitHub Desktop.
// @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