反应原生版本 0.62。我在 React Native 中有点菜鸟,我正在尝试按照本指南设置 react-native-localize https://heartbeat.fritz.ai/how-to-use-react-native-localize-in-react- native-apps-3bb3d510f801但是当我更改语言并回到我的应用程序时,我正在努力解决这个错误:
下面你可以看到我的 App.js
import React, { Component } from 'react';
import { StatusBar, Text } from 'react-native';
import * as RNLocalize from 'react-native-localize'
import {Router, Scene, Tabs} from 'react-native-router-flux';
import Search from '@screens/Search';
import MyAppointments from '@screens/MyAppointments';
import MyAccount from '@screens/MyAccount';
import CustomTabBar from '@components/CustomTabBar';
import theme from '@styles/theme';
import memoize from 'lodash.memoize';
import i18n from 'i18n-js';
const translationGetters = {
en: () => require('./translations/en.json'),
it: () => require('./translations/it.json')
};
const translate = memoize(
(key, config) => i18n.t(key, config),
(key, config) => (config ? key + JSON.stringify(config) : key)
);
const setI18nConfig = () => {
const fallback = { languageTag: 'en' };
const { languageTag } =
RNLocalize.findBestAvailableLanguage(Object.keys(translationGetters)) ||
fallback;
translate.cache.clear();
i18n.translations = { [languageTag]: translationGetters[languageTag]() };
i18n.locale = languageTag;
};
export default class App extends Component {
constructor(props) {
super(props);
setI18nConfig()
}
componentDidMount() {
RNLocalize.addEventListener('change', this.handleLocalizationChange)
}
componentWillUnmount() {
RNLocalize.removeEventListener('change', this.handleLocalizationChange)
}
handleLocalizationChange = () => {
setI18nConfig()
.then(() => this.forceUpdate())
.catch(error => {
console.error(error)
})
};
render() {
return (
... render code not important for the issue
);
}
}
非常感谢谁会帮助我。