Multi-Language support in React Native
Last Updated :
11 Jan, 2025
Multi-Language support in React Native: Internationalization of React Native app helps us to target those users who either don't speak English or are more comfortable using the app in their local language. We can give options to the users to choose from multiple languages that we will offer in our app.
For this kind of situation, we need to implement Multi-Language support in our app. This is where i18next and react-i18next come in handy. This library makes it easier to manage Translations and provides a lot of useful functions.
Approach:
- Creating a fresh React Native Project and installing i18next and react-i18next dependencies.
- Creating a JSON file for each language.
- Configuring i18next in i18n.js .
- Initializing i18next by importing it in App.js.
- Importing language functions in App.js and changing the language.
Adding multi-language support involves using libraries like i18n-js
or react-native-localize
. With globalization becoming crucial, supporting multiple languages is often essential for reaching a broader audience.
Now let’s start with the implementation.
Step 1: Create a fresh React Native Project by running the command
npx react-native init languageDemo
Step 2: Now go into your project folder i.e. language demo.
cd languageDemo
Step 3: Install i18next and react-i18next Libraries.
npm install i18next --save
npm i react-i18next
Project Structure: It will look like the following.
Step 4Project StructureStep 4: Creating JSON files. The JSON file contains the translation for each text in our app that we want to show. The file is in JSON format, so every text has a key.
The key is unique for each different text but common for the same text in different languages.
1. en.json: contains translations for English.
{
"translation": {
"hello":"Hello",
"this line is translated":"This line is translated"
}
}
2. hi.json: Contains translations for Hindi.
{
"translation": {
"hello":"नमस्ते",
"this line is translated":"यह पंक्ति अनुवादित है"
}
}
Step 5: Configuring i18next (i18n.js). We will import i18n from 'i18next' and initReactI18next from 'react-i18next'.
Then we will import our JSON files(en.json and hi.json).
In i18n.use(initReactI18next) we pass the i18n instance to react-i18next which will make it available for all the components via the context api.
The init() function takes an object of {lng , fallbacklng, resources} as parameter.
lng - default language
fallbackLng - Fallback language in case key is not found for any translation.
resources - JSON file for various languages.
Filename: i18n.js
JavaScript
import i18n from 'i18next';
import {initReactI18next} from 'react-i18next';
import en from './en.json';
import hi from './hi.json';
i18n.use(initReactI18next).init({
lng: 'en',
fallbackLng: 'en',
resources: {
en: en,
hi: hi,
},
interpolation: {
escapeValue: false // react already safes from xss
}
});
export default i18n;
Step 6: We will now import the i18n.js in our App.js file which will initialize i18next. We will then import useTranslation Hook from react-i18next which gives us t and i18n.
t - function which accepts key as parameter and returns the
appropriate text based on current language selected.
i18n - object containing function like changeLanguage
and other useful values.
In our screen we have two buttons for changing language. There is a Text which will be changed according to the current set language when we press one of the two buttons. To change the language we will use changeLanguage function which accepts language resource name as parameter and a callback function to execute after the language change is done.
We can check current language by doing console.log(i18n.language)
App.js
import React,{useState} from 'react';
import './i18n/i18n';
import {View, Text,Pressable} from 'react-native';
import {useTranslation} from 'react-i18next';
const App = () => {
const {t, i18n} = useTranslation();
const [currentLanguage,setLanguage] =useState('en');
const changeLanguage = value => {
i18n
.changeLanguage(value)
.then(() => setLanguage(value))
.catch(err => console.log(err));
};
return (
<View
style={{
flex: 1,
backgroundColor: 'white',
alignItems: 'center',
justifyContent: 'space-evenly',
}}>
<Text style={{fontWeight: 'bold', fontSize: 25, color: '#33A850'}}>
{t('hello')}{' '}
</Text>
<Text style={{fontWeight: 'bold', fontSize: 25, color: '#33A850'}}>
{t('this line is translated')}
</Text>
<Pressable
onPress={() => changeLanguage('en')}
style={{
backgroundColor:
currentLanguage === 'en' ? '#33A850' : '#d3d3d3',
padding: 20,
}}>
<Text>Select English</Text>
</Pressable>
<Pressable
onPress={() => changeLanguage('hi')}
style={{
backgroundColor:
currentLanguage === 'hi' ? '#33A850' : '#d3d3d3',
padding: 20,
}}>
<Text>हिंदी का चयन करें</Text>
</Pressable>
</View>
);
};
export default App;
Step to run the application: Start the application using the following command :
npx react-native run-android
Output:
Reference: https://react.i18next.com/guides/quick-start
Similar Reads
Multi-Language Support in ReactJS
To add multi-language support in ReactJS, use the react-i18next library. Set up translation files for each language in JSON format and configure i18next to load them with a default language. Use the useTranslation hook to access translations in components, and implement i18n.changeLanguage() to allo
4 min read
Word Guessing Game Design using React-Native
In this article, we will create a Word Guessing App using React Native. Word Guess app is a guessing game, where a hint will be given based on the word you have to guess. This app provides hints and a limited number of attempts to make the game challenging and enjoyable.Preview of final output: Let
5 min read
Build a Language Translator App Using React Native
In this article, we'll walk you through the process of creating a language translation app that allows users to translate text between different languages. The implementation involves utilizing ReÂact Native components and integrating an efficient language translation API, ensuring the app's functio
5 min read
Introduction to React Native
If you want to build mobile apps for both Android and iOS. What should you learn? The individual native languages for each app i.e, Java for Android and Swift/Objective-C for iOS?, Actually NO. Native Android and iOS development are quite different and can be expensive â first, the language itself i
3 min read
React Native Smart Components
In this article, we are going to learn about Smart components in React Native. The smart component is one of the categories of React Components so before diving into the smart components detail. A Component is one of the core building blocks of React and React-Native. The component is a block of cod
3 min read
Setting up i18n in Angular 17 for Multi-Language Support
Think of your web app as a tool that can connect with users worldwide. To ensure a welcoming and user-friendly experience for everyone, we need to speak their language. Internationalization (i18n) is the process of making your app adaptable to multiple languages and regions. Angular provides powerfu
4 min read
How to create a Surface in react-native ?
React native is a framework developed by Facebook for creating native-style apps for iOS & Android under one common language, JavaScript. Initially, Facebook only developed React Native to support iOS. However, with its recent support of the Android operating system, the library can now render m
3 min read
Getting started with React Native
React Native, also known as RN, is a widely used mobile app framework that relies on JavaScript and It enables developers to build natively-rendered apps for both iOS and Android platforms using the same codebase was introduced by Facebook as an open-source project in 2015 and quickly became one of
7 min read
React Native Switch API
In this article, we will explore how to use the Switch component in React Native. The Switch component is a controlled component, meaning it requires a callback function to update the props based on the user's actions. Let's watch a demo video of what we are going to develop.Demo VideoSyntax<Swit
6 min read
How to Upload and Preview an Image in React Native ?
Image uploading and previewing are widespread in mobile apps. With the Expo ImagePicker package, React Native makes this work easier. Uploading and previewing photographs is a common feature in Android and iOS applications, especially in the user profile section. This feature allows users to submit
4 min read