first Add the dependencies
npm install --save react-i18next i18next
If you are following along, start the app with npm start so you can give the two pages a look.
Create the file structure
In the src folder, create an i18n folder to hold all the information related to translating the app. Here’s the structure I used that is pretty common:
src
+-- i18n
+-- locales
| +-- en
| +-- translations.json
| +-- es
| +-- translations.json
+-- config.js
The config.js file contains the set up for the i18n instance. The two JSON files will hold the text that goes in the app. en for English and es for Spanish. If you are following along, create the file structure in the boilerplate.
Configure the i18n instance
The i18n
instance will hold all of your translations, the current language, and other info and methods needed. You can configure it in the config.js
file like this:
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
i18n.use(initReactI18next).init({
fallbackLng: 'en',
lng: 'en',
resources: {
en: {
translations: require('./locales/en/translations.json')
},
es: {
translations: require('./locales/es/translations.json')
}
},
ns: ['translations'],
defaultNS: 'translations'
});
i18n.languages = ['en', 'es'];
export default i18n;
At the top, import the necessary dependencies. Next, the use(initReactI18next)
will bind react-i18next
to the i18n
instance.
The first two properties of the init object parameter are a fallback
language (fallbackLng
) and default language (lng
). I set both of these to English (en
).
The resources
are the JSON files with the translations that you created in the last step. The namespaces (ns
) and default namespace (defaultNS
) are the keys from the resources object.
You can split your translations into multiple files (namespaces) if you have a large app to simplify the files. In which case, you would add more than just the one file in the resources
object and add it to the namespaces array. This app is small, so all the translations can go in one file.
Add the i18n instance to your app
In the index.js
file of the boilerplate, which contains the entire app, import the i18n
instance you created like this:
import './i18n/config';
Next, add an empty object {}
to your two translations.json
files so you don’t get any errors. You will fill in the keys later.
Translate a functional component with the useTranslation hook
The Nav.js
file is the first component to translate. It’s a functional component, so you can use the useTranslation
hook. Import it at the top with import { useTranslation } from 'react-i18next'.
Next, instead of using the implicit return with the component, add curly brackets and the return statement so you can declare a variable. Get the t function from the hook with const { t } = useTranslation();
.
All together, it should look like something this:
import { useTranslation } from 'react-i18next';
const Nav = () => {
const { t } = useTranslation();
return (
…
);
}
The text you want to translate in this component is Home
and Page 2
. In your English translations.json
file, add two properties to the object:
{
"home": "Home",
"page2": "Page 2"
}
Now, you can pass those keys to the t function to get the text. In the Nav.js
component, use the t function to translate Home
and Page 2
to the English text like this: t('key-from-json-file')
.
Here’s how those lines should look:
import { useTranslation } from ‘react-i18next;
const Nav = () => {
const { t } = useTranslation();
return (
…
<Link to="/">{t('home')}</Link>
<Link to="/page2">{t('page2')}</Link>
);
}
Preview in Spanish
The two nav buttons should be working for English. Add the same two keys to the Spanish JSON file so you can see what it will look like in Spanish. Here’s the translations:
{
"home": "Casa",
"page2": "Página 2"
}
After that, go to the config.js file, set the lng
(default language) to es
and reload the page. The nav links should show up in Spanish. When you are done, you can set the default language back to en
.
change languages with tab
All the desired text should be available in both English and Spanish now. The last thing to do is to make it easy to switch between languages.
The Footer.js
file has a few buttons and a changeLanguage function. Import the useTranslation
hook from react-i18next
at the top of the file. At the top of the Footer component, get the i18n
instance from it like this: const { i18n } = useTranslation();
.
Finally, in the changeLanguage function, use the i18n.changeLanguage()
method, passing e.target.value
to it, to change the langauge.
All together, it looks like this:
import { useTranslation } from 'react-i18next';
const Footer = () => {
const { i18n } = useTranslation();
function changeLanguage(e) {
i18n.changeLanguage(e.target.value);
}
return(
<div className='footer'>
<button onClick={changeLanguage} value='en'>English</button>
<button onClick={changeLanguage} value='es'>Español</button>
</div>
)
}
Now, you should be able to switch languages by clicking the buttons on each page.
I found this article here. :LINK: