Step by step guide (v9)
Initial situation based on quick start
npm install react-i18next@legacy i18next --save{
"Welcome to React": "Welcome to React and react-i18next"
}import i18n from "i18next";
import { reactI18nextModule } from "react-i18next";
import translationEN from '../public/locales/en/translation.json';
// the translations
const resources = {
en: {
translation: translationEN
}
};
i18n
.use(reactI18nextModule) // passes i18n down to react-i18next
.init({
resources,
lng: "en",
keySeparator: false, // we do not use keys in form messages.welcome
interpolation: {
escapeValue: false // react already safes from xss
}
});
export default i18n;1) Adding more languages
a) Add an additional language file
b) Add the additional translations on init
c) Auto detect the user language
d) Let the user toggle the language
2) Lazy loading translations
a) Adding lazy loading for translations
b) Loading multiple translation files
Use the withNamespaces hoc
c) Handle rendering while translations are not yet loaded
Using the prop tReady
Set the global wait option
3) Sidequest: natural vs. keybased catalog
a) natural keys
b) keybased catalog
4) HOCs are dead - long lives the render props
5) Translate JSX nodes as one string
Using the Trans component
Last updated