AskHandle Blog
How to Use i18next with React Like a Pro

How to Use i18next with React Like a Pro
Are you facing challenges with internationalization and localization using i18next in your React projects? This guide will help you implement multilingual features seamlessly in your applications.
Getting Started with i18next in React
Begin by installing the necessary packages. Run the following command:
1npm install i18next react-i18nextAfter installation, set up i18next in your React application. Create a file named i18n.js in your project's root directory and add the following code:
1import i18n from 'i18next';
2import { initReactI18next } from 'react-i18next';
3
4i18n.use(initReactI18next).init({
5 resources: {
6 en: {
7 translation: {
8 greeting: 'Hello, World!'
9 }
10 },
11 fr: {
12 translation: {
13 greeting: 'Bonjour le monde!'
14 }
15 }
16 },
17 lng: 'en',
18 fallbackLng: 'en',
19
20 interpolation: {
21 escapeValue: false
22 }
23});
24
25export default i18n;Replace the sample translations with your desired language resources. The lng key indicates the default language, while the fallbackLng key specifies the fallback language.
Integrating i18next with React Components
Next, wrap your root component with the I18nextProvider to share translations across your React tree. Here's how to do this:
1import React from 'react';
2import ReactDOM from 'react-dom';
3import { I18nextProvider } from 'react-i18next';
4import i18n from './i18n';
5import App from './App';
6
7ReactDOM.render(
8 <I18nextProvider i18n={i18n}>
9 <App />
10 </I18nextProvider>,
11 document.getElementById('root')
12);With this configuration, you can access the i18n instance in your components using the useTranslation hook. Below is an example of using translations in a functional component:
1import React from 'react';
2import { useTranslation } from 'react-i18next';
3
4const MyComponent = () => {
5 const { t } = useTranslation();
6
7 return <h1>{t('greeting')}</h1>;
8};
9
10export default MyComponent;The t function allows you to retrieve translations based on the keys in your language resources.
Dynamic Language Switching
Switching languages dynamically is a powerful feature of i18next. You can implement this by defining functions that update the i18n instance with the selected language. Here’s a simple example:
1import React from 'react';
2import { useTranslation } from 'react-i18next';
3
4const LanguageSelector = () => {
5 const { i18n } = useTranslation();
6
7 const changeLanguage = (lng) => {
8 i18n.changeLanguage(lng);
9 };
10
11 return (
12 <div>
13 <button onClick={() => changeLanguage('en')}>English</button>
14 <button onClick={() => changeLanguage('fr')}>French</button>
15 </div>
16 );
17};
18
19export default LanguageSelector;With i18n.changeLanguage(lng), you can switch between different languages in your React application.
Pluralization and Interpolation
Handling pluralization and interpolation is important in multilingual applications. i18next offers robust support for these features. Below are examples of how to use pluralization and interpolation in i18next.
Pluralization
Define plural translation keys in your language resources. Here’s an example:
1{
2 "apple": "An apple",
3 "apple_plural": "{{count}} apples"
4}In your component, you can access the pluralized translation using the count parameter:
1import React from 'react';
2import { useTranslation } from 'react-i18next';
3
4const PluralExample = () => {
5 const { t } = useTranslation();
6 const appleCount = 5;
7
8 return <p>{t('apple', { count: appleCount })}</p>;
9};
10
11export default PluralExample;Interpolation
Use interpolation to inject dynamic values into your translations. Define placeholders in your translation keys. Here’s an example:
1{
2 "welcome": "Welcome, {{name}}!"
3}In your component, pass the name when accessing the translation:
1const InterpolationExample = () => {
2 const { t } = useTranslation();
3 const username = 'John';
4
5 return <p>{t('welcome', { name: username })}</p>;
6};With pluralization and interpolation, your multilingual React application can have adaptive content.
You have now learned how to use i18next in your React projects effectively. Following these steps will help you implement internationalization and localization smoothly. Start integrating i18next into your applications and enhance your multilingual capabilities.