0

反应原生版本 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
    );
  }
}

非常感谢谁会帮助我。

4

1 回答 1

0

seti18nConfig函数不会返回您使用 a的承诺.then。它是一个同步函数。你可以简单地写

handleLocalizationChange = () => {
    setI18nConfig()
    this.forceUpdate()
};
于 2020-05-15T09:06:45.763 回答