✨ New update: Automation 2.0 is live — smarter workflows, faster results.

Building a Multi-Language App with React JS

Introduction React JS is a popular JavaScript library used for building user interfaces. It provides a component-based approach to web development, making it easy to create reusable and modular UI components. In this article, we will explore how to build a multi-language app using React JS. By allowing users to switch between different languages, you …

Introduction

React JS is a popular JavaScript library used for building user interfaces. It provides a component-based approach to web development, making it easy to create reusable and modular UI components. In this article, we will explore how to build a multi-language app using React JS. By allowing users to switch between different languages, you can enhance the accessibility and usability of your application.

Setting up the React App

To get started, make sure you have Node.js installed on your system. You can check your Node.js version by running the command node -v in your terminal. Once you have Node.js installed, you can create a new React app using the create-react-app command. Open your terminal and run the following command:

npx create-react-app multi-language-app

This command will create a new directory called multi-language-app and set up a basic React app inside it. Navigate into the app directory using the command cd multi-language-app.

Adding Language Support

To add language support to our React app, we will use the i18next library along with its React bindings, react-i18next. i18next is a powerful internationalization library that supports multiple languages and provides features like translation, interpolation, and pluralization.

Start by installing the required dependencies. In your terminal, run the following command:

npm install i18next react-i18next i18next-http-backend

Once the dependencies are installed, create a new directory called locales in the src directory. Inside the locales directory, create a new file named en.json to hold the English translations. Add the following content to the en.json file:

{
  "greeting": "Hello, World!"
}

Similarly, create a file named fr.json inside the locales directory for French translations. Add the following content to the fr.json file:

{
  "greeting": "Bonjour, le monde!"
}

Now, let’s create a new component called LanguageSelector that allows the user to switch between languages. In the src directory, create a new file named LanguageSelector.js and add the following code:

English
); }; export default LanguageSelector; ” style=”color:#d8dee9ff;display:none” aria-label=”Copy” class=”code-block-pro-copy-button”>
import React from 'react';
import { useTranslation } from 'react-i18next';

const LanguageSelector = () => {
  const { i18n } = useTranslation();

  const handleChangeLanguage = (event) => {
    i18n.changeLanguage(event.target.value);
  };

  return (
    <div>
      <select onChange={handleChangeLanguage}>
        <option value="en">Englishoption>
        <option value="fr">Frenchoption>
      select>
    div>
  );
};

export default LanguageSelector;

The LanguageSelector component uses the useTranslation hook from react-i18next to access the translation functionality. It renders a