eva*_*van 36
不需要外部库.您可以在React的Native Modules中找到
import { NativeModules } from 'react-native'
// iOS:
const locale = NativeModules.SettingsManager.settings.AppleLocale // "fr_FR"
// Android:
const locale = NativeModules.I18nManager.localeIdentifier // "fr_FR"
Run Code Online (Sandbox Code Playgroud)
为了测试这一点,我将设备上的语言更改为法语.以下是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"]
...
}
Run Code Online (Sandbox Code Playgroud)
Fir*_*med 20
下面的函数将返回一个2个字母的语言代码.例如:en
import { Platform, NativeModules } 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
Run Code Online (Sandbox Code Playgroud)
eya*_*l83 13
我正在使用i18n软件包(react-native-i18n).然后就是:
I18n = require('react-native-i18n')
locale = I18n.currentLocale()
Run Code Online (Sandbox Code Playgroud)
为我工作上面的什么都没有,但这个组件。
console.log("Device Locale", DeviceInfo.getDeviceLocale()); // e.g en-US
Run Code Online (Sandbox Code Playgroud)
function getLocale() {
if (React.Platform.OS === 'android') {
return I18n.locale;
} else {
return NativeModules.SettingsManager.settings.AppleLocale.replace(/_/, '-');
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33361 次 |
| 最近记录: |