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

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

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

14条回答
  •  被撕碎了的回忆
    2020-12-13 04:25

    NativeModules solution can be changed over time by Facebook developers.
    Because of this reason I had prepared a component. (It works only on Android for now)

    Sample usage:

    import SystemSettings from 'react-native-system-settings'
    
    SystemSettings.get(
        settings => console.log('settings: ', settings)
    )
    

    Also promise-then can be used:

    SystemSettings.get().then(settings => console.log('settings: ', settings)).done()
    

    Also ES7 async-await method can be used!

    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) {}
        };
    }
    

    A sample result:

    {
        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"
    }
    

提交回复
热议问题