Building a Multi-Language Website with React and i18next
Learn how to create a website that supports multiple languages using the popular internationalization library i18next and React
Building a website that supports multiple languages can be a challenging task, but with the right tools, it can also be a lot of fun. In this blog post, we will explore how to build a multi-language website with React and i18next, a popular internationalization (i18n) library.
First, let's start by understanding what i18next is and what it can do. i18next is an open-source library that provides a set of tools for internationalizing your website or application. It makes it easy to add support for multiple languages by providing a simple and flexible API for handling translations, formatting dates and numbers, and more.
The first step in building a multi-language website with React and i18next is to install the library. You can do this by running the following command in your terminal:
npm install i18next
Once i18next is installed, you can start configuring it to work with your React application. One of the most important things to do is to set up the i18next instance, which is the main object that you will use to interact with the library. You can do this by creating a new file called i18n.js
and adding the following code:
import i18next from 'i18next';
import { initReactI18next } from 'react-i18next';
i18next
.use(initReactI18next)
.init({
lng: 'en',
fallbackLng: 'en',
resources: {
en: {
translation: {
"key": "Hello world"
}
},
fr: {
translation: {
"key": "Bonjour le monde"
}
}
}
});
export default i18next;
In this code, we're importing i18next and the initReactI18next
function from the react-i18next
package. Then, we're initializing the i18next instance by calling the init()
method and passing in some configuration options. The most important option is the resources
option, which is where you define your translations. In this example, we're defining translations for the en
and fr
languages.
Now that we have set up the i18next instance, we can start using it in our React components. The easiest way to do this is to use the useTranslation
hook, which is provided by the react-i18next
package. You can use this hook in any component that needs to display translated text. For example, the following component uses the useTranslation
hook to display a translated text:
import { useTranslation } from 'react-i18next';
function MyComponent() {
const { t } = useTranslation();
return <h1>{t('key')}</h1>;
}
In this example, the useTranslation
hook returns an object with a t
property, which is a function that can be used to get translations. The t
function takes a single argument, which is the key of the translation that you want to get. In this case, we're passing in the key key
, which corresponds to the translation "Hello world" in English and "Bonjour le monde" in French.
Another way to use translations in your React components is to use the Trans
component provided by the react-i18next
package. The Trans
component can be used to wrap any text that needs to be translated and it automatically detects the current language and provides the correct translation. For example, the following component uses the Trans
component to display a translated text:
import { Trans } from 'react-i18next';
function MyComponent() {
return <h1><Trans>key</Trans></h1>;
}
You can also pass the translation key as a prop to the Trans component, which makes it more reusable.
import { Trans } from 'react-i18next';
function MyComponent({key}) {
return <h1><Trans>{key}</Trans></h1>;
}
Another important aspect of building a multi-language website is to provide a way for users to switch between languages. i18next provides a changeLanguage
method that can be used to change the current language. For example, the following component uses the changeLanguage
method to allow the user to switch between English and French:
import { useTranslation } from 'react-i18next';
function LanguageSwitcher() {
const { i18n } = useTranslation();
return (
<>
<button onClick={() => i18n.changeLanguage('en')}>English</button>
<button onClick={() => i18n.changeLanguage('fr')}>French</button>
</>
);
}
In this example, we're using the useTranslation
hook to get the i18n
object and then using the changeLanguage
method to change the current language when the user clicks on one of the buttons.
In conclusion, building a multi-language website with React and i18next is a great way to provide a better user experience for your international audience. With i18next, you can easily add support for multiple languages, manage translations, and switch between languages. With the useTranslation
hook or the Trans
component, you can easily use the translations in your React components. Remember that, building a multi-language website is a process that takes time and effort but it's worth it to provide a better user experience and reach a broader audience.