未定义不是对象(评估“ module.type”)

时间:2019-08-21 06:35:34

标签: react-native react-native-android i18next

我经历了其他类似的类型答案,但从未见过类似“(评估'module.type')”之类的错误

在我的项目中,我试图使用i18next添加阿拉伯语

这是我的错误日志

info Starting logkitty


[11:29:22] E | ReactNativeJS ▶︎ undefined is not an object (evaluating 'module.type')

[11:29:22] E | ReactNativeJS ▶︎ Module AppRegistry is not a registered callable module (calling runApplication)

enter image description here

这是我的代码

import i18n from 'i18next';
import { reactI18nextModule } from 'react-i18next';
import locale from 'react-native-locale-detector';
import { AsyncStorage } from 'react-native';


import en from './en.json';
import ar from './ar.json';

const STORAGE_KEY = '@APP:languageCode';

const languageDetector = {
    init: Function.prototype,
    type: 'languageDetector',
    async: true, 
    detect: async (callback) => {
        const savedDataJSON = await AsyncStorage.getItem(STORAGE_KEY);
        const lng = (savedDataJSON) ? savedDataJSON: null;
        const selectLanguage = lng || locale;
        console.log('detect - selectLanguage:', selectLanguage);
        callback(selectLanguage);
    },
    cacheUserLanguage: () => {}
};

i18n
    .use(languageDetector)
    .use(reactI18nextModule)
  .init({
    fallbackLng: 'en',
    resources: { en,  ar},

    ns: ['common'],
    defaultNS: 'common',

    debug: true,


    interpolation: {
      escapeValue: false, 
    }
  });


export default i18n;

参考:- I am trying to implementing this

1 个答案:

答案 0 :(得分:1)

检查此代码。

import React from 'react';
import { Text, View, TouchableOpacity } from 'react-native';
import i18next from 'i18next';
import { initReactI18next, useTranslation } from 'react-i18next';
import locale from 'react-native-locale-detector';
import { AsyncStorage } from 'react-native';

const languageDetector = {
  type: 'languageDetector',
  async: true,
  detect: async (callback) => {
      const savedDataJSON = await AsyncStorage.getItem(STORAGE_KEY);
      const lng = (savedDataJSON) ? savedDataJSON: null;
      const selectLanguage = lng || locale;
      console.log('detect - selectLanguage:', selectLanguage);
      callback(selectLanguage);
  },
  init: () => {},
  cacheUserLanguage: () => {},
};

i18next
  .use(languageDetector)
  .use(initReactI18next)
  .init({
    fallbackLng: 'en',
    debug: true,
    resources: {
      en: {
        translation: {
          hello: 'Hello world',
          change: 'Change language',
        },
      },
      sv: {
        translation: {
          hello: 'xxx',
          change: 'xxx',
        },
      },
    },
  });