我在做一个项目时有国际化的要求,查询后发现可以使用阿里的 react-intl-universal
先安装到项目里
yarn add react-intl-universal
在项目中使用时发现 将国际化内容放在 react-redux的Provider里会导致bug,组件延迟变化,所以我在最外层又套了一个antd框架提供的 ConfigProvider 具体使用如下
在app.js入口文件中
。
。
。
// 国际化
import { ConfigProvider } from 'antd';
import intl from 'react-intl-universal';
import { emit } from "./common/utils/emit";
import locales from './locales/locales';
class App extends Component {
constructor(props) {
super(props);
this.state = {
antdLang: locales.en_US, // 语言状态
}
}
componentDidMount() {
emit.on('change_language', lang => this.loadLocales(lang)); // 监听语言改变事件
this.loadLocales(); // 初始化语言
}
loadLocales(lang = 'en-US') {
intl.init({
currentLocale: lang, // 设置初始语音
locales,
}).then(() =>