Created
June 13, 2020 14:38
-
-
Save LauraBeatris/8ceeff4587c136e9505488be61917c58 to your computer and use it in GitHub Desktop.
Internacionalization setup for React with i18n and date-fns
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 * 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; |
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
{ | |
"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" | |
} | |
} |
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 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