react intl 国际化
方案描述:由于采用单页面,所以按钮切换时会刷新页面
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"
},
<script src="/build/en-US.js"></script>
<script src="/build/index.js"></script>
index-zh.html同理
en.json
{
"Home.Title": "username",
"Home.BUtton": "login"
}
zh.json
{
"Home.Title": "用户名",
"Home.BUtton": "登录"
}
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,
};
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')
);
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} />:
<DatePicker />
</div> </div>);
}
} export default App;
react intl 国际化的更多相关文章
- [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 ...
- mac centos linux 安装PHP扩展 INTL(国际化) ———— error: 'ext/standard/php_smart_str.h'
PHP简单源码安装扩展 五个步骤: 详细说明下: cd /fujieace/php7.0/ext/intl:#进入INTL扩展目录? 在编译扩展时候需要phpize准备环境,准备程序需要获取这个目录的 ...
- [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 ...
- [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 ...
- [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- ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- 直播软件源码,uniapp滚动条置顶实现
直播软件源码,uniapp滚动条置顶实现 实现功能: uniapp置顶滚动条.自定义页面滚动条的位置 实现代码: uni.pageScrollTo({ scrollTop: 0, dura ...
- npm查询所有可以安装的包
npm view 包名 versions
- DBeaver导入SQL脚本数据
DBeaver导入SQL脚本数据 起因 Navicat Premium还原备份的导致数据库中文乱码 看Navicat Premium中看中文数据,是正常显示了,但是在IEDA查询和Web页面上显示,均 ...
- 【服务器数据恢复】Linux服务器分区不能挂载的数据恢复案例
服务器数据恢复环境:某品牌PowerEdge系列服务器,磁盘阵列存储型号为该品牌MD3200系列存储,分配lun:linux centos 7操作系统,EXT4文件系统. 服务器故障:服务器在工作中由 ...
- [笔记] Android开发中的gradle是什么?
gradle是什么? 先看下文档中的介绍 https://docs.gradle.org/current/userguide/what_is_gradle.html Gradle is an op ...
- sql 行转列分析 以后再也不用记了
表scores 请转成的横表是这样子的: // ::::我们来分析下:首先 姓名这一列肯定是不重复的姓名[重复也没办法了 这需求只能这样了] 因此 我们用group by [姓名] 然后姓名这一列就有 ...
- mysql开启root用户远程管理权限
来源:https://blog.csdn.net/qq_29670375/article/details/120590041 1.使用"mysql -uroot -proot"命令 ...
- Java反射解析注解
package com.jeeplus.config; import javax.validation.constraints.Size; import java.lang.annotation.An ...
- Django Rest Frame work 如何使用serializers序列化函数新手教程
Django Rest Frame work 如何使用serializers序列化 Django Rest Framework提供了serializers模块,用于序列化和反序列化模型实例 ...
- centos7 双网卡同网段双网关配置
需求: #1.服务器为双网卡: #2.网卡1为互联网 172.16.137.99/24/254 #3.网卡2为旅游专网 172.16.137.97/24/1 #4.互联网路由器为172.16.137. ...