类似于 Objective-C 中的 [NSLocale currentLocale]。
最佳答案
不需要外部库。您可以在 React 的 Native Modules 中找到您要查找的内容。
import { NativeModules } from 'react-native'
// iOS:
const locale = NativeModules.SettingsManager.settings.AppleLocale ||
NativeModules.SettingsManager.settings.AppleLanguages[0] // "fr_FR"
// Android:
const locale = NativeModules.I18nManager.localeIdentifier // "fr_FR"
为了测试这一点,我将设备上的语言更改为法语。以下是您在 NativeModules.SettingsManager.settings
对象中与区域设置相关的内容的示例:
{
...
AppleKeyboards: [
"fr_FR@hw=US;sw=QWERTY",
"en_US@sw=QWERTY;hw=Automatic",
"es_419@sw=QWERTY-Spanish;hw=Automatic",
"emoji@sw=Emoji"
]
AppleLanguages: ["fr-US", "en-US", "es-US", "en"]
AppleLanguagesDidMigrate: "9.2"
AppleLocale: "fr_FR"
NSLanguages: ["fr-US", "en-US", "es-US", "en"]
...
}
关于react-native - 在 React Native (iOS) 中获取设备区域设置的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33468746/