方案描述:由于采用单页面,所以按钮切换时会刷新页面

1、安装 react-intl  babel-plugin-react-intl json-loader

npm i  react-intl  babel-plugin-react-intl json-loader --save-dev

2、修改webpack.config.js 与 webpack.production.config.js

entry: {
  en_US: path.resolve(__dirname, './en-US.js'),
  zh_Hans_CN: path.resolve(__dirname, './zh-CN.js'),
  index: path.resolve(__dirname, 'react/inxex.js')
},
output: {
  path: __dirname + '/build',
  publicPath: '/build',
  filename: '[name].js',
  chunkFilename: "[id].[name].js"
},
3、新建 index-en.html/ index-zh.html
index-en.html  引入 
    <script src="/build/en-US.js"></script>
<script src="/build/index.js"></script>

index-zh.html同理

4、新建文件夹locals   创建 en.json/zh.json
en.json
{
"Home.Title": "username",
"Home.BUtton": "login"
}
zh.json
{
"Home.Title": "用户名",
"Home.BUtton": "登录"
}
5、在入口文件目录下新建 en-US.js、zh-CN.js
import antdEn from 'antd/lib/locale-provider/en_US';
import appLocaleData from 'react-intl/locale-data/en';
import MSGS from './locals/en.json';
import MSGS1 from './M/en.json' window.appLocale = {
messages: {
...MSGS,
...MSGS1
},
antd: antdEn,
locale: 'en-US',
data: appLocaleData,
};
6、入口文件index.js
import ReactDOM from 'react-dom';
import React from 'react';
import App from '../component/App';
import { LocaleProvider } from 'antd'; import { addLocaleData, IntlProvider } from 'react-intl'; const appLocale = window.appLocale; addLocaleData(appLocale.data); ReactDOM.render(
<LocaleProvider locale={appLocale.antd}>
<IntlProvider locale={appLocale.locale} messages={appLocale.messages}>
<App />
</IntlProvider>
</LocaleProvider>,
document.getElementById('react-content')
);
7、app中使用
import React from 'react';
import { DatePicker, Pagination, Table, Icon } from 'antd';
import { FormattedMessage, defineMessages } from 'react-intl';
import InjectExample from './InjectExample'; const messages = defineMessages({
datePicker: {
id: 'App.datePicker.title',
defaultMessage: '日期选择',
},
name: {
id: 'App.talbe.name',
defaultMessage: '姓名',
}
}); class App extends React.Component {
componentDidMount() {
console.log('componentDidMount')
}
render() {return (<div style={{ margin: 20 }}>
<div style={{ margin: 10 }}>
<p><a href="index.html">中文</a></p>
<p><a href="index-en.html">english</a></p>
</div>
<div style={{ margin: 10 }}>
<FormattedMessage {...messages.datePicker} />: &nbsp;
<DatePicker />
</div> </div>);
}
} export default App;

参考demo: https://github.com/yangstar/intl-example

react intl 国际化的更多相关文章

  1. [React Intl] Use a react-intl Higher Order Component to format messages

    In some cases, you might need to pass a string from your intl messages.js file as a prop to a compon ...

  2. mac centos linux 安装PHP扩展 INTL(国际化) ———— error: 'ext/standard/php_smart_str.h'

    PHP简单源码安装扩展 五个步骤: 详细说明下: cd /fujieace/php7.0/ext/intl:#进入INTL扩展目录? 在编译扩展时候需要phpize准备环境,准备程序需要获取这个目录的 ...

  3. [React Intl] Use Webpack to Conditionally Include an Intl Polyfill for Older Browsers

    Some browsers, such as Safari < 10 & IE < 11, do not support the JavaScript Internationali ...

  4. [React Intl] Get locale value from intl injector

    Get 'injectIntl' from  'react-intl', it is a high order componet. We need to wrap our component into ...

  5. [React Intl] Install and Configure the Entry Point of react-intl

    We’ll install react-intl, then add it to the mounting point of our React app. Then, we’ll use react- ...

  6. [React Intl] Render Content Based on a Number using react-intl FormattedMessage (plural)

    Using the react-intl FormattedMessage component, we’ll learn how to render content conditionally in ...

  7. [React Intl] Format Numbers with Separators and Currency Symbols using react-intl FormattedNumber

    Using a react-intl FormattedNumber component, we'll pass a Number and a few additional props in orde ...

  8. [React Intl] Format a Date Relative to the Current Date Using react-intl FormattedRelative

    Given a date, we’ll use the react-intl FormattedRelative component to render a date in a human reada ...

  9. [React Intl] Format Date and Time Using react-intl FormattedDate and FormattedTime

    Using the react-intl FormattedDate and FormattedTime components, we’ll render a JavaScript Date into ...

  10. [React Intl] Render Content with Markup Using react-intl FormattedHTMLMessage

    In this lesson, we’ll use the react-intl FormattedHTMLMessage component to display text with dynamic ...

随机推荐

  1. 关于之前ecplices没法正常导入使用fx包的笔记

    办法一:在java build path里的libraries里导入跟工作环境版本(或者ecplices的jdk版本)相配的jar包. 注:如果导入的包是要用fx的,可以不直接改整个程序的工作环境,可 ...

  2. Spring Boot 配置 Swagger(3.0.0 版本)

    添加 Swagger 依赖 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter --> ...

  3. [Oracle19C 数据库管理] 加载和传输数据库

    移动数据的通用架构 数据泵data pump(impdp, expdp),借助DBMS_DATAPUMP存储过程,可以进行表的导出导入,行记录的导出导入,表空间的导出导入或者整个schema的导出导入 ...

  4. corkscrew在ssh中发生错误

    使用绝对路径 ProxyCommand="corkscrew 127.0.0.1 12345 jump.123.com 2020" abc@efg.com 改为 ProxyComm ...

  5. Maven3.6.1 (转载大佬文章)

    (30条消息) Maven3.6.1安装及配置_浅若清风过的博客-CSDN博客 1.下载https://archive.apache.org/dist/maven/maven-3/3.6.1/bina ...

  6. Python发送飞书消息

    #!/usr/bin/python3.8 # -*- coding:UTF-8 -*- import os, sys sys.path.append(os.path.dirname(os.path.a ...

  7. 使用moment获取本周、前n周、后n周开始结束日期以及动态计算周数

    原文地址 https://blog.csdn.net/qq_43432158/article/details/124200343 项目中有一个需求:需要根据学期时间动态的计算出该学期有多少周 通过上网 ...

  8. 一个新App上线应该注意什么?

    一个新App上线应该关注哪些地方: 功能使用上无大问题遗留 用户使用体验尚佳 (前两条属于普通app更新也需要关注的点) 用户需要有反馈的入口,比如app内的反馈.用户可以直接联系到对应负责人进行反馈 ...

  9. 如何使用cmd(dos命令)关闭IIS中某个站点

    在 目录  C:\Windows\System32\inetsrv 下面有一个 appcmd 程序,定位到 该目录下 appcmd site /?  #管理站点 appcmd /? #管理整个IIS ...

  10. 【C++复习】5.7 多文件结构与编译预处理命令

    1.C++项目结构 C++程序的一般组织架构 类声明文件(.h文件) 类实现文件(.cpp文件) 类的使用文件(main()所在的.cpp文件) 用工程组合各文件 2.编译链接 编译链接过程 3.外部 ...