What's the best way to get device locale in react native (iOS)?

后端 未结 14 1607
清歌不尽
清歌不尽 2020-12-13 03:35

Something similar to [NSLocale currentLocale] in Objective-C.

相关标签:
14条回答
  • 2020-12-13 04:30

    You can install react-native-i18n and use this function:

    import React, { NativeModules } from 'react-native'
    ...
    function getLocale () {
      if (React.Platform.OS === 'android') {
        return NativeModules.RNI18n.getCurrentLocale(locale => locale.replace(/_/, '-'))
      } else {
        return NativeModules.RNI18n.locale.replace(/_/, '-')
      }
    }
    

    Works both under Android and iOS.

    0 讨论(0)
  • 2020-12-13 04:34

    iOS 13 workaround here:

    locale = NativeModules.SettingsManager.settings.AppleLocale // "fr_FR"
    console.log(" ==> Current settings: ", NativeModules.SettingsManager.settings)
    if (locale === undefined) {
        // iOS 13 workaround, take first of AppleLanguages array 
        locale = NativeModules.SettingsManager.settings.AppleLanguages[0]
        if (locale == undefined) {
              return "en" // default language
        }
    }

    0 讨论(0)
提交回复
热议问题