Example of Localization in React Native Multi Language App

React Native Localization

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 the 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 LocalizedStrings Component?

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

const strings = new LocalizedStrings({
  hi: {
    first:"क्या हाल है ?",
    second:"मैं ठीक हूँ ?",
  },
  "ma":{
    first:"तू कसा आहेस ?",
    second:"मी ठीक आहे ?",
  },
  "en":{
    first:"How are You ?",
    second:"I am fine ",
  },
"fr":{
  first:"comment allez vous",
  second:"je vais bien",
}
});

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 the 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

cd ProjectName

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

npm install react-native-localization --save

2. Install react-navigation dependency to import createAppContainer

npm install react-navigation --save

3. Other supporting libraries for react-navigation

npm install react-native-gesture-handler react-native-safe-area-context @react-native-community/masked-view react-native-screens react-native-reanimated --save

4. Install react-navigation-stack to import createStackNavigator

npm install react-navigation-stack --save

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

CocoaPods Installation

Please use the following command to install CocoaPods

cd ios && pod install && cd ..

Project Structure

We are using the following project structure. Please make the following project structure.

Code to Make a Multi-Language App

If you are done with the project structure then please replace the following code

App.js

import React from 'react';

//Import react-navigation
import { createAppContainer } from 'react-navigation';
import { createStackNavigator} from 'react-navigation-stack';
import LanguageSelectionScreen from './pages/LanguageSelectionScreen';
import ContentScreen from './pages/ContentScreen';

const App = createStackNavigator({
  LanguageSelectionScreen: { 
      screen: LanguageSelectionScreen, 
      navigationOptions: { header: null } 
  },
  ContentScreen: { screen: ContentScreen },
});
export default createAppContainer(App);

LanguageSelectionScreen.js

import React from 'react';
import { View, Text, ScrollView, Image, StyleSheet } from 'react-native';
import stringsoflanguages from './stringsoflanguages';
export default class Home extends React.Component {
  constructor(props) {
    super(props);
    const lang = [
      { shortform: 'hi', longform: 'Hindi' },
      { shortform: 'ma', longform: 'Marathi' },
      { shortform: 'en', longform: 'English' },
      { shortform: 'fr', longform: 'French' },
    ];
    global.lang = lang;
  }
  settext(value) {
    stringsoflanguages.setLanguage(value);
    this.props.navigation.navigate('ContentScreen' , {JSON_Clicked_Item:value,});
  }
  render() {
    return (
      <View style={styles.MainContainer}>
        <View style={{ marginTop: 60 }}>
          <Text style={styles.textHeading}>
            Please Select Preferred Language
          </Text>
        </View>
        <Image
          source={{
            uri:
              'https://raw.githubusercontent.com/AboutReact/sampleresource/master/language.png',
          }}
          style={styles.img}
        />
        <ScrollView style={{ marginTop: 30, width: '80%' }}>
          {global.lang.map((item, key) => (
            <View style={styles.elementContainer} key={key}>
              <Text
                ref={item.shortform}
                onPress={() => this.settext(item.shortform)}
                style={styles.text}>
                {item.longform}
              </Text>
              <View style={styles.saparator} />
            </View>
          ))}
        </ScrollView>
      </View>
    );
  }
}
const styles = StyleSheet.create({
  MainContainer: { 
    flex: 1,
    backgroundColor: 'white',
    alignItems: 'center'
  },
  textHeading: {
    color: '#191919',
    fontSize: 30,
    textAlign: 'center'
  },
  img: { 
    width: 64,
    height: 64,
    marginTop: 30 
  },
  elementContainer: {
    width: '100%',
    marginTop: 30,
    alignItems: 'center',
  },
  text: {
    color: '#191919',
    fontSize: 25
  },
  saparator: {
    height: 0.5,
    width: '60%',
    backgroundColor: '#C2C2C2',
    marginTop: 10,
  },
});

ContentScreen.js

import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import stringsoflanguages from './stringsoflanguages';
export default class second extends React.Component {
  static navigationOptions = ({ navigation }) => {
    return {
      title: navigation.getParam('Title', 'Default Title'),
    };
  };
  componentDidMount() {
    var that = this;
    var heading = '';
    if (this.props.navigation.state.params.JSON_Clicked_Item == 'hi') {
      heading = 'Selected Language Hindi';
    } else if (
      this.props.navigation.state.params.JSON_Clicked_Item == 'ma'
    ) {
      heading = 'Selected Language Marathi';
    } else if (
      this.props.navigation.state.params.JSON_Clicked_Item == 'en'
    ) {
      heading = 'Selected Language English';
    } else if (
      this.props.navigation.state.params.JSON_Clicked_Item == 'fr'
    ) {
      heading = 'Selected Language French';
    }
    that.props.navigation.setParams({
      Title: heading,
    });
  }
  render() {
    return (
      <View style={styles.MainContainer}>
        <Text style={styles.text}> {stringsoflanguages.first}</Text>
        <Text style={styles.text}> {stringsoflanguages.second} </Text>
      </View>
    );
  }
}
const styles = StyleSheet.create({
  MainContainer: {
    flex: 1,
    backgroundColor: 'white',
    alignItems: 'center'
  },
  text: {
    color: '#191919',
    fontSize: 25,
    marginTop: 15
  },
});

stringsoflanguages.js

import React from 'react';
import LocalizedStrings from 'react-native-localization';
const strings = new LocalizedStrings({
  hi: {
    first:"क्या हाल है ?",
    second:"मैं ठीक हूँ ?",
  },
  "ma":{
    first:"तू कसा आहेस ?",
    second:"मी ठीक आहे ?",
  },
  "en":{
    first:"How are You ?",
    second:"I am fine ",
  },
"fr":{
  first:"comment allez vous",
  second:"je vais bien",
}
});
export default strings;

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).

Output Screenshots

Android

      

IOS

        

This is how you can make a multi-language React Native Application (Localization). If you have any doubts 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. 🙂


Related Posts

10 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.

    Reply
  2. console.error: “Something went wrong initializing the native ReactNativeLocalization module.”Please check your configuration.Did you run ‘react-native link’?”

    This is my error.
    Please help

    Reply

Leave a Comment

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