React native 如何使用React Native和i18n js获取当前区域设置?

React native 如何使用React Native和i18n js获取当前区域设置?,react-native,React Native,我对react native还不熟悉,我正在尝试从用户的设备获取当前语言环境。 现在,我有两个版本的应用程序:法语和英语。 当我更改回退语言时,它工作正常。我有两个版本的应用程序。 但我无法直接从设备上更改语言 我使用RNlocalize,正如在许多页面中看到的那样,但它不适合我。。。我的App.js: import React, { Component } from "react"; import I18n from "./src/i18n/I18n"; import * as RNLoc

我对react native还不熟悉,我正在尝试从用户的设备获取当前语言环境。 现在,我有两个版本的应用程序:法语和英语。 当我更改回退语言时,它工作正常。我有两个版本的应用程序。 但我无法直接从设备上更改语言

我使用RNlocalize,正如在许多页面中看到的那样,但它不适合我。。。我的App.js:

  import React, { Component } from "react";
import I18n from "./src/i18n/I18n";
import * as RNLocalize from "react-native-localize";

async handleLocales() {
  this.locales = RNLocalize.getLocales();
}

getLocale() {
  if (this.locales) {
    if (Array.isArray(this.locales)) {
      return this.locales[0];
    }
  }
  return null;
}
class App extends Component {
  render() {
    return (
      <View>
        <Text>{I18n.t("hello")}</Text>
      </View>
    );
  }
}
export default App;
我在一个测试文件夹里试过。我犯的错误是

我希望你能帮助我。而且,作为一个新手,我需要解释,因为它是新的,我没有很多经验

无论如何,谢谢你阅读我的文章。

试试这个:

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

const locale =
  Platform.OS === 'ios'
    ? NativeModules.SettingsManager.settings.AppleLocale
    : NativeModules.I18nManager.localeIdentifier;

我想我的地方出了点问题。 以这种方式纠正一切问题:

    import i18n from "i18next";
import detector from "i18next-browser-languagedetector";
import { reactI18nextModule } from "react-i18next";
import { NativeModules, Platform } from 'react-native';

import en from '../public/locales/en/translation.js';
import fr from '../public/locales/fr/translation.js';

// the translations
const resources = {
  en: {
    translation: en
  },
  fr: {
    translation: fr
  }
};
const locale =
  Platform.OS === 'ios'
    ? NativeModules.SettingsManager.settings.AppleLocale
    : NativeModules.I18nManager.localeIdentifier;
console.log(locale.substring(0, 2));
      i18n.locale = locale

i18n
  .use(detector)
  .use(reactI18nextModule) // passes i18n down to react-i18next
  .init({
    resources,
    lng: locale.substring(0, 2),
    fallbackLng: "en", // use en if detected lng is not available

    keySeparator: false, // we do not use keys in form messages.welcome

    interpolation: {
      escapeValue: false // react already safes from xss
    }
  });

export default i18n;

非常感谢

你好,谢谢你抽出时间回答我。我要把这个放在我的I18n文件中吗?或者在组件中?@Kimako您需要获取用户的默认区域设置,如示例中所示,并将其设置为您的
I18n
I18n.locale=locale
…嘿@HendEl Sahli您可以检查问题吗?@HendEl Sahli您好,请帮助我,我有一个非常类似的问题。我可以在I18n js中使用keySeparator(react native)吗?需要使用平面json(“a.b.c”)作为键
import { NativeModules, Platform } from 'react-native';

const locale =
  Platform.OS === 'ios'
    ? NativeModules.SettingsManager.settings.AppleLocale
    : NativeModules.I18nManager.localeIdentifier;
    import i18n from "i18next";
import detector from "i18next-browser-languagedetector";
import { reactI18nextModule } from "react-i18next";
import { NativeModules, Platform } from 'react-native';

import en from '../public/locales/en/translation.js';
import fr from '../public/locales/fr/translation.js';

// the translations
const resources = {
  en: {
    translation: en
  },
  fr: {
    translation: fr
  }
};
const locale =
  Platform.OS === 'ios'
    ? NativeModules.SettingsManager.settings.AppleLocale
    : NativeModules.I18nManager.localeIdentifier;
console.log(locale.substring(0, 2));
      i18n.locale = locale

i18n
  .use(detector)
  .use(reactI18nextModule) // passes i18n down to react-i18next
  .init({
    resources,
    lng: locale.substring(0, 2),
    fallbackLng: "en", // use en if detected lng is not available

    keySeparator: false, // we do not use keys in form messages.welcome

    interpolation: {
      escapeValue: false // react already safes from xss
    }
  });

export default i18n;