Logo

dev-resources.site

for different kinds of informations.

🌎 Seamless Multi-Language Support in React Native

Published at
1/11/2025
Categories
reactnative
android
ios
multilanguage
Author
amitkumar13
Author
11 person written this
amitkumar13
open
🌎 Seamless Multi-Language Support in React Native

Adding multi-language support to your React Native app not only enhances the user experience but also expands your app’s reach to a global audience. In this article, we’ll show you how to integrate multi-language support into your React Native app using i18next, react-i18next, and other helpful packages.


Image description


🛠️ Required Dependencies

Run the following commands to install the necessary packages:

yarn add i18next react-i18next i18next-http-backend i18next-browser-languagedetector
yarn add @react-native-async-storage/async-storage
yarn add react-native-localize
Enter fullscreen mode Exit fullscreen mode

📂 Setting Up the Translations Folder

Create a translations folder inside your src directory. Inside this folder, create the following files:

  1. en.json - English translations.
  2. hi.json - Hindi translations.
  3. i18n.js - Configuration for i18next.
  4. translate.js - Helper function for translation.

Image description


📝 Translation Files

en.json

{
  "welcome": "Welcome",
  "hello_world": "Hello, World!"
}
Enter fullscreen mode Exit fullscreen mode

hi.json

{
  "welcome": "स्वागत है",
  "hello_world": "नमस्ते, दुनिया!",
  "change_language": "भाषा बदलें"
}
Enter fullscreen mode Exit fullscreen mode

🔧 i18n Configuration

i18n.js

import i18n from 'i18next';
import {initReactI18next} from 'react-i18next';
import * as Localization from 'react-native-localize';
import AsyncStorage from '@react-native-async-storage/async-storage';

// Translation files
import english from '../translations/en.json';
import hindi from '../translations/hi.json';

// Detect the user's language
const languageDetector = {
  type: 'languageDetector',
  async: true,
  detect: callback => {
    AsyncStorage.getItem('user-language', (err, language) => {
      if (err || !language) {
        const bestLanguage = Localization.findBestAvailableLanguage([
          'english',
          'hindi',
        ]);
        callback(bestLanguage?.languageTag || 'english');
      } else {
        callback(language);
      }
    });
  },
  init: () => {},
  cacheUserLanguage: language => {
    AsyncStorage.setItem('user-language', language);
  },
};

i18n
  .use(languageDetector)
  .use(initReactI18next)
  .init({
    fallbackLng: 'english',
    lng: 'english',
    resources: {
      english: {translation: english},
      hindi: {translation: hindi},
    },
    interpolation: {
      escapeValue: false,
    },
  });

export default i18n;

Enter fullscreen mode Exit fullscreen mode

🛠 Helper for Translations

translate.js

import i18n from 'i18next';

export const translate = (key, options = {}) => {
  return i18n.t(key, options);
};


Enter fullscreen mode Exit fullscreen mode

🚀 Integrating i18n in Your App

Update your App.js file to include the I18nextProvider:

App.js

import React from 'react';
import MainNavigator from './src/navigation/rootNavigator';
import {SafeAreaProvider} from 'react-native-safe-area-context';
import {I18nextProvider} from 'react-i18next';
import i18n from './src/translations/i18n';

const App = () => {
  return (
    <I18nextProvider i18n={i18n}>
      <SafeAreaProvider>
        {/* Your main app components */}
      </SafeAreaProvider>
    </I18nextProvider>
  );
};

export default App;
Enter fullscreen mode Exit fullscreen mode

🖥️ Example Screen for Multi-Language Support

Create a component to demonstrate the language switch feature:

LanguageComponent.js

import {View, Text, Button, StyleSheet} from 'react-native';
import React from 'react';
import {useTranslation} from 'react-i18next';
import {translate} from '../../translations/translate';

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

  const switchLanguage = language => {
    i18n.changeLanguage(language);
  };

  return (
    <View style={styles.container}>
      <Text>{translate('welcome')}</Text>
      <Text>{translate('hello_world')}</Text>
      <Button title="Switch to Hindi" onPress={() => switchLanguage('hindi')} />
      <Button
        title="Switch to English"
        onPress={() => switchLanguage('english')}
      />
    </View>
  );
};

export default LanguageComponent;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
});

Enter fullscreen mode Exit fullscreen mode

With these steps, you’ve successfully added multi-language support to your React Native app! 🌐 Happy coding! 🚀

android Article's
30 articles in total
Favicon
Three Common Pitfalls in Modern Android Development
Favicon
Using SVGs on Canvas with Compose Multiplatform
Favicon
Kotlin Generics Simplified
Favicon
Understanding Process Management in Operating Systems
Favicon
Introduction to Operating Systems
Favicon
Why Should You Develop a Native Android App Over Flutter?
Favicon
Flutter Development for Low end PCs
Favicon
Day 13 of My Android Adventure: Crafting a Custom WishList App with Sir Denis Panjuta
Favicon
How to Integrate Stack, Bottom Tab, and Drawer Navigator in React Native
Favicon
Flutter Design Pattern Bussines Logic Component (BLOC)
Favicon
🌎 Seamless Multi-Language Support in React Native
Favicon
Morphing Geometric Shapes with SDF in GLSL Fragment Shaders and Visualization in Jetpack Compose
Favicon
FMWhatsApp - Enhanced WhatsApp Experience
Favicon
[Feedback Wanted]Meet Nora: A Desktop Plant Robot Companion 🌱
Favicon
GBWhatsApp - Advanced WhatsApp Alternative
Favicon
Android TreeView(Hierarchy based) with Kotlin
Favicon
The Role of Android in IoT Development
Favicon
PicsArt MOD APK: Unlock the Power of Creativity
Favicon
Getting Started with Android Testing: Building Reliable Apps with Confidence (Part 3/3)
Favicon
How to Integrate Stack and Bottom Tab Navigator in React Native
Favicon
Day 11 Unlocking the Magic of Location Services!
Favicon
Creating M3U Playlists for xPola Player
Favicon
xPola Player: The Advanced Media Player for Android
Favicon
Getting Started with Android Testing: Building Reliable Apps with Confidence (Part 2/3)
Favicon
How I wrote this technical post with Nebo: an Android gamechanger ✍️
Favicon
Understanding Room Database in Android: A Beginner's Guide
Favicon
hiya
Favicon
Android Lock Screen Widgets: Revolutionizing Content Consumption with Glance's Smart Features
Favicon
Top 10 Best Android App Development Companies in India
Favicon
Why Modded APKs Are Gaining Popularity in 2025: Understanding the Risks and Benefits

Featured ones: