Internationalization (i18n) is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes. Our application uses the i18next library along with the
To add a new language to the application, follow these steps:
In the
Within this file, export a constant object containing key-value pairs of translations. The key will be the token used in the application, and the value will be the translated string.
Import the new translation file into
In the resources object of the i18n configuration, add a new key for the language and set its value to
1export const it = {
2 welcome: "Benvenuto",
3 // ... other translations
4};
1import { it } from "./translations/it";
2
3// ... existing imports and configuration
4
5i18n.use(initReactI18next).init({
6 resources: {
7 // ... existing resources
8 it: { translation: it },
9 },
10 // ... remaining configuration
11});
To use the translations in your React components, you can use the
1import React from "react";
2import { useTranslation } from "react-i18next";
3
4const MyComponent = () => {
5 const { t } = useTranslation();
6
7 return <h1>{t("welcome")}</h1>; // 'welcome' is the key in your translation files
8};
The fallbackLng option in the i18n configuration specifies a default language to use if the current language does not have a given translation. By default, this is set to English (en).