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?
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.
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; };
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
Try using this library -> https://github.com/AlexanderZaytsev/react-native-i18n
import I18n from 'react-native-i18n';
deviceLocale = I18n.currentLocale()
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With