2015-11-02 10 views

答えて

0

この情報を取得するには、自分自身で対応する必要がある場合があります。しかし、あなたのユースケースに応じて、このローカリゼーションコンポーネント(stefalda/ReactNativeLocalization)が役に立つかもしれません。

9

私はi18nパッケージ(react-native-i18n)を使用しています。 そして、それだけだ。

I18n = require('react-native-i18n') 
locale = I18n.currentLocale() 
+0

エラー: あなたは...ドキュメント内でこのよう

import { getLanguages } from 'react-native-i18n' getLanguages().then(languages => { console.log(languages) // ['en-US', 'en'] }) 

リンクを使用することができます。 –

1

あなたはこの機能を使用することができます両方の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(/_/, '-') 
    } 
} 

作品。あなたがに組み込むすでに必要なものを見つけることが

+0

'NativeModules'は' RNI18n'を持っていません(少なくともデフォルトでは)。私はあなたがlibを追加したと思います。 – antoine129

+0

私はあなたがhttps://github.com/AlexanderZaytsev/react-native-i18n – antoine129

5

は外部ライブラリなしでネイティブリアクト:

import {NativeModules} from 'react-native' 
locale = NativeModules.SettingsManager.settings.AppleLocale 

はこれをテストするために、私はフランス/フランス語&にXcodeでのスキームエディタでアプリケーション言語やアプリケーション地域を変更私は変化に気づいた。これは、あなたが後になっているかもしれないかもしれませんが、NativeModules.SettingsManager.settingsオブジェクトは、何らかの使用の可能性があります。

import React, {NativeModules} from 'react-native'; 

if (NativeModules.I18nManager) { 
    const {localeIdentifier, isRTL} = NativeModules.I18nManager; 
} 

私はAndroidとiOSのため、この解決策を見つけた

{ 
    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 
} 
+1

をインストールしたと思います。Androidでは動作しません。 –

+1

@DanielSteigerwaldこの方法での設定へのアクセスは、Androidではまだサポートされていません。 [Settings.android.js](https://github.com/facebook/react-native/blob/94666f16c7da37e649316d40ea23d7a8054a04f9/Libraries/Settings/Settings.android.js) –

+4

Android用に「NativeModules.I18nManager.localeIdentifier」を使用してください –

0

(RN 0.35)、これはに役立つことがある:ここで

は、オブジェクトの設定で見つけるものの一例です誰かがiOSにロケールのプロパティは表示されません。今はrtlサポートのみを表示しています。

3

上記のどれも私のために働いたのではなく、thisコンポーネントです。

console.log("Device Locale", DeviceInfo.getDeviceLocale()); // e.g en-US 
3
function getLocale() { 
    if (React.Platform.OS === 'android') { 
    return I18n.locale; 
    } else { 
    return NativeModules.SettingsManager.settings.AppleLocale.replace(/_/, '-'); 
    } 
} 
1

あなたは万博上で開発する場合...あなたが使用することができます:Facebookの開発者が時間をかけて

console.log(await NativeModules.ExponentUtil.getCurrentLocaleAsync());
console.log(await NativeModules.ExponentUtil.getCurrentDeviceCountryAsync());
console.log(await NativeModules.ExponentUtil.getCurrentTimeZoneAsync());

0

NativeModulesソリューションcan be changed
この理由のため私は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" 
} 
0

は、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; 
} 
関連する問題