https://medium.com/how-to-react/setup-multilingual-in-react-js-using-i18n-module-33b1bfbb57cd
-
Create project
-
Install dependencies
npm install react-i18next i18next
-
Configure i18n. Create
i18n.js
file in /src -
create locales dir
mkdir src/locales
Create a folder for each translationmkdir src/locales/en src/locales/es
Create a json for each language translationtouch src/locales/en/translation.json src/locales/es/translation.json
-
create translation files as json
es/translation.json
en/translation.json
{
"language": "English",
"selectLanguage":"Select your language",
"articles": [
{
"heading": "Proven facts. Is the economy doing well or badly? ",
"subheading": "Apocalyptic forecasts about"
}
]
}
- Import i18n en
main.jsx
import './i18n';
- Import useTranslation
import { useTranslation } from "react-i18next";
const { t, i18n } = useTranslation();
<p>{t('name')}</p>
- Use select to choose language
//https://react.i18next.com/latest/usetranslation-hook
//TODO ADD CONTEXT