Skip to content

Instantly share code, notes, and snippets.

@LauraBeatris
Created June 13, 2020 14:38
Show Gist options
  • Save LauraBeatris/8ceeff4587c136e9505488be61917c58 to your computer and use it in GitHub Desktop.
Save LauraBeatris/8ceeff4587c136e9505488be61917c58 to your computer and use it in GitHub Desktop.
Internacionalization setup for React with i18n and date-fns
import * as dateFnsLocales from 'date-fns/locale';
import { Locale } from 'date-fns';
import i18n from './i18n';
interface Locales {
[key: string]: Locale;
}
const getDateFnsLocale = (): Locale => {
const locales: Locales = {
pt: dateFnsLocales.ptBR,
en: dateFnsLocales.enUS,
de: dateFnsLocales.de,
};
return locales[i18n.language];
};
export default getDateFnsLocale;
{
"account_form": {
"email": "E-mail",
"password": "Password",
"full_name": "Nome completo",
"forgot_my_password": "Esqueci minha senha"
},
"signin": {
"welcome_to_the_platform": "Bem vindo a plataforma",
"create_an_account": "Cria uma conta"
},
"signup": {
"create_an_account": "Cria uma conta",
"already_have_an_account": "Já possui uma conta? Faça seu login"
},
"buttons": {
"confirm": "Confirm"
}
}
import i18n from 'i18next';
import detector from 'i18next-browser-languagedetector';
import { initReactI18next } from 'react-i18next';
import translationEN from './en/common.json';
import translationPT from './pt/common.json';
const resources = {
en: {
translation: translationEN,
},
pt: {
translation: translationPT,
},
};
i18n
.use(detector)
.use(initReactI18next)
.init({
resources,
lng: 'en',
fallbackLng: 'en',
interpolation: {
escapeValue: false,
},
});
export default i18n;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment