Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current language setting in the phone with react native

I would like my app to support multiple languages, for example English and Chinese. Using react native, how do I find the language settings on the phone?

like image 728
Feiyang Lei Avatar asked Sep 25 '17 06:09

Feiyang Lei


People also ask

How do I change the language in React Native?

Using a handler method called setLanguage , you can allow the functionality to switch between different languages from the LANGUAGES array defined above this function component. This function component uses Pressable from React Native to change the language.

How do I change dynamic language in React Native?

translations = { en, nl }; i18n. locale = lang; }; export const changeLanguage = async lang => { setI18nConfig(lang); // AsyncStorage. setItem('language', lang); }; export const isRTL = () => { return translate('lang') === 'ar' ? true : false; };


3 Answers

import { NativeModules, Platform } from 'react-native';

const deviceLanguage =
          Platform.OS === 'ios'
            ? NativeModules.SettingsManager.settings.AppleLocale ||
              NativeModules.SettingsManager.settings.AppleLanguages[0] // iOS 13
            : NativeModules.I18nManager.localeIdentifier;

console.log(deviceLanguage); //en_US
like image 182
Firoz Ahmed Avatar answered Oct 18 '22 11:10

Firoz Ahmed


Try using this library -> https://github.com/AlexanderZaytsev/react-native-i18n

import I18n from 'react-native-i18n';
deviceLocale = I18n.currentLocale()
like image 23
Apurva jain Avatar answered Oct 18 '22 12:10

Apurva jain


import { NativeModules, Platform } from 'react-native';

let deviceLanguage = Platform.OS === 'ios' ? 
    NativeModules.SettingsManager.settings.AppleLocale:
    NativeModules.I18nManager.localeIdentifier;

if (deviceLanguage === undefined) {
    // iOS 13 workaround, take first of AppleLanguages array 
    deviceLanguage = NativeModules.SettingsManager.settings.AppleLanguages[0]
    if (deviceLanguage == undefined) {
        return defaultLocalization // default language
    }
}
like image 3
Leonid Veremchuk Avatar answered Oct 18 '22 11:10

Leonid Veremchuk