Objective-C中与[NSLocale currentLocale]类似的东西。在反应本机(iOS)中获取设备区域设置的最佳方式是什么?
回答
您可能需要扩展自己的反应以获取此信息。但是,根据您的使用情况,此本地化组件(stefalda/ReactNativeLocalization)可能适合您。
我正在使用i18n软件包(react-native-i18n)。 然后它只是:
I18n = require('react-native-i18n')
locale = I18n.currentLocale()
您可以使用此功能:Android和iOS的下
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(/_/, '-')
}
}
作品。
'NativeModules'没有'RNI18n'(至少默认)。我想你添加了一个lib。 – antoine129
我想你安装了https://github.com/AlexanderZaytsev/react-native-i18n – antoine129
你会发现你所需要的已建成无需外部库阵营母语:
import {NativeModules} from 'react-native'
locale = NativeModules.SettingsManager.settings.AppleLocale
为了测试这个,我改变了应用程序语言和应用区的方案编辑器在Xcode法国/法国&我注意到一个变化。这可能是也可能不是你想要的,但NativeModules.SettingsManager.settings
对象可能有些用处。
这里是什么,你会在设置中找到一个例子对象:
{
AddingEmojiKeybordHandled: true
AppleITunesStoreItemKinds: Array[20]
AppleKeyboards: [
"[email protected]=US;sw=QWERTY",
"[email protected]=Emoji",
"[email protected]=US;sw=QWERTY"
]
AppleKeyboardsExpanded: 1
AppleLanguages: ["en-US"]
AppleLanguagesDidMigrate: "9.2"
AppleLocale: "fr_FR"
ApplePasscodeKeyboards: Array[3]
MSVLoggingMasterSwitchEnabledKey: false
NSInterfaceStyle: "macintosh"
NSLanguages: ["en-US", "en"]
RCTDevMenu: Object
}
它不适用于Android。 –
@DanielSteigerwald Android上尚不支持以这种方式访问设置。请参阅[Settings.android.js](https://github.com/facebook/react-native/blob/94666f16c7da37e649316d40ea23d7a8054a04f9/Libraries/Settings/Settings.android.js) –
Android版使用'NativeModules.I18nManager.localeIdentifier'' –
我发现这个解决方案为Android和iOS(RN 0.35)
import React, {NativeModules} from 'react-native';
if (NativeModules.I18nManager) {
const {localeIdentifier, isRTL} = NativeModules.I18nManager;
}
可能这将有助于有人,但iOS不显示区域设置属性。它现在只显示rtl支持。
以上都不适用于我,但this组件。
console.log("Device Locale", DeviceInfo.getDeviceLocale()); // e.g en-US
function getLocale() {
if (React.Platform.OS === 'android') {
return I18n.locale;
} else {
return NativeModules.SettingsManager.settings.AppleLocale.replace(/_/, '-');
}
}
如果你开发了世博会......你可以使用:
console.log(await NativeModules.ExponentUtil.getCurrentLocaleAsync());
console.log(await NativeModules.ExponentUtil.getCurrentDeviceCountryAsync());
console.log(await NativeModules.ExponentUtil.getCurrentTimeZoneAsync());
NativeModules解决方案can be changed随着时间的推移由Facebook开发。
因为这个原因,我准备了a component。 (它仅适用于Android现在)
使用范例:
import SystemSettings from 'react-native-system-settings'
SystemSettings.get(
settings => console.log('settings: ', settings)
)
并可保证则可以使用:
SystemSettings.get().then(settings => console.log('settings: ', settings)).done()
也可用于ES7异步等待的方法!
class App extends React.Component {
componentWillMount() {
this._loadInitialState()
}
_loadInitialState = async() => {
try {
let settings = await SystemSettings.get()
// Now settings variable would be filled and can be used!
} catch (error) {}
};
}
样本结果:
{
densityDpi: 320,
fontScale: 1,
hardKeyboardHidden: "no",
keyboard: "qwerty",
keyboardHidden: "no",
localization: {
country: "US",
displayCountry: "United States",
displayLanguage: "English",
displayName: "English (United States)",
is24HourFormat: false,
language: "en",
locale: "en_US",
networkCountry: "US",
simCountry: "US",
timeZone: {
ID: "Europe/Amsterdam",
displayName: {
long: "Amsterdam Standard Time",
short: "GMT+01:00",
},
offset: 3600000
}
},
orientation: "portrait",
screenHeightDp: 615,
screenLayout: "normal",
screenWidthDp: 360,
smallestScreenWidthDp: 360,
uiModeType: "normal"
}
以下函数会返回一个2字母语言代码。例如:en
function getLanguageCode() {
let systemLanguage = 'en';
if (Platform.OS === 'android') {
systemLanguage = NativeModules.I18nManager.localeIdentifier;
} else {
systemLanguage = NativeModules.SettingsManager.settings.AppleLocale;
}
const languageCode = systemLanguage.substring(0, 2);
return languageCode;
}
我个人更喜欢使用react-native-i18n。 那么你可以使用这样的文档里面..
import { getLanguages } from 'react-native-i18n'
getLanguages().then(languages => {
console.log(languages) // ['en-US', 'en']
})
- 1. 什么是显示货币符号区域设置相关的最佳方式?
- 2. 在Android设备上获取用户货币的最佳方式是什么?
- 3. 设置java应用程序的最佳方式是什么?
- 4. 什么是在Android中设置通知的最佳方式
- 5. 在QuoteRequest消息中设置SenderSubID的最佳方式是什么?
- 6. 什么是在局域网上实现设备发现的最佳方式
- 7. 在两台iOS设备之间发送视频的最佳方式是什么?
- 8. 反应 - 什么是保持文本格式的最佳方式
- 9. 什么组件可以在反应本机中设置样式?
- 10. 将数据推送到设备的最佳方式是什么?
- 11. 设置“深度”配置选项的最佳方式是什么?
- 12. 什么是当前设置第三方cookie的最佳方式?
- 13. 从iOS设备上传文件/照片的最佳方式是什么?
- 14. 什么是使用JavaScript获取和设置单个cookie值的“最佳”方式
- 15. 设置表单以获取临时变量的最佳方式是什么?
- 16. 以编程方式获取在区域设置中设置的位置
- 17. 在swift中存储本地设置的最佳方式是什么?
- 18. 获取Windows的区域设置格式
- 19. 将margin-top设置为与高度相反的最佳方式是什么?
- 20. 以反应本机存储私人数据的最佳方式是什么?
- 21. 在NumPy中获得随机数的最佳方式是什么?
- 22. 阅读/设置IE选项的最佳方式是什么?
- 23. 将DateTime设置为UTC的最佳方式是什么?
- 24. 用CakePHP 3.x设置ReactJS的最佳方式是什么?
- 25. 使用cocos2d设置菜单的最佳方式是什么?
- 26. 什么是为Sitecore设置开发环境的最佳方式
- 27. 处理设置更改(iPhone/iPad)的最佳方式是什么?
- 28. 设置和测试Lambda API的最佳方式是什么?
- 29. 设置jQuery超时间隔的最佳方式是什么?
- 30. 什么是设置未知代码限制的最佳方式?
错误的反应FYI任何人的新版本。 –