Example of Localization in React Native Multi Language App

Here is an Example of Localization in React Native Multi Language App. Localization is the process of making something local in character or restricting it to a particular place. In case of application development, it is related to the development of the application according to the local language.

For the localization, we are going to use LocalizedStrings component from react-native-localization library.

How to Use

Here is the code snippet of LocalizedStrings we have used in this Example.

In this example below, we will make a home screen to select the language and will show the text content on the next screen accordingly from a common string repository where we have defined same text for each language code.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependencies

To install the dependencies open the terminal and jump into your project

1. Install react-native-localization dependency to import LocalizedStrings

2. Install react-navigation dependency to import createAppContainer

3. Install react-native-gesture-handler dependency to support gesture navigation

4. Install react-navigation-stack to import createStackNavigator

These commands will copy all the dependencies into your node_module directory.

CocoaPods Installation

After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the libraries but need to install pods.

In this example we need to install the pods for react-native-gesture-handler and react-native-localization.

Please use the following command to install CocoaPods

Project Structure

We are using the following project structure. So please make the following project structure and replace the code.

App.js

LanguageSelectionScreen.js

ContentScreen.js

stringsoflanguages.js

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Android

      

IOS

        

This is how you can make a multi-language React Native Application (Localization). If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

6 thoughts on “Example of Localization in React Native Multi Language App”

  1. Hello. How to handle the situation when one of the localization objects does not contain one of the string?
    e.g.
    const strings = new LocalizedStrings({
    hi: {
    first:”क्या हाल है ?”,
    },

    “en”:{
    first:”How are You ?”,
    second:”I am fine “,
    },
    });
    I want to show en second string if it does not exist in hi.

    Thank you.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.