前言

React 做国际化,我推荐使用 React-intl , 这个库提供了 React 组件和Api两种方式来格式化日期,数字和字符串等。知道这个库了,那让我们开始使用它

组件用法

为了和React 比较融和,我们可以使用组件的方式

1.安装

npm install react-intl --save

2.添加引用

import {IntlProvider, addLocaleData} from 'react-intl';

3.添加 locale 配置文件
zh-CN.js
 const zh_CN = {
'intl.hello': "你好",
'intl.name': '我的名字是 {name}'
}
export default zh_CN;
en-US.js
 const en_US = {
'intl.hello': "hello",
'intl.name': 'my name is {name}'
}
export default en_US;
4.使用<IntlProvider />

这个组件用于设置 i18n 的上下文,它将包装应用程序的根组件,以便整个应用程序将配置在 i18n 的上下文中.

最主要的两个配置项是: loacle 当前的语言环境 messages 当前语言的内容。

我们要动态切换语言,需要动态改这两个配置。

import zhCN from './locale/zh.js';    //导入 i18n 配置文件
import enUS from './locale/en.js'; addLocaleData([...en, ...zh]); export default class Root extends Component {
static propTypes = {
store: PropTypes.object.isRequired,
history: PropTypes.object.isRequired
} render() {
const { store , history } = this.props;
return (
<IntlProvider locale='zh' messages={zhCN}>
<Provider store={store}>
<Router history={history}>
</Router>
</Provider>
</IntlProvider>
)
}
}
5.使用<FormattedMessage />
基础用法
<FormattedMessage
  id="intl.hello"
  defaultMessage={'hello'}
/>

如果当前语言环境是 中文,它会显示你好 ,如果是英文环境,会显示Hello.

动态传值
<FormattedMessage
id="intl.name"
values={{name: <b>{name}</b>}}
/>

我们定义 intl.name 的时候模板里用到了{name} ,这个代表我们可以动态传值,我们可以通过FormattedMessage中的 values 属性传一个JSON对象,这是就会动态显示我们的内容了。

6.其它组件用法

Ract-intl 为我们提供了丰富的组件,可以帮我们很好的处理字符串,时间,日期 ,大家可以自己查看 API,如有不明白的地方,我可以留言。

API用法

有时候我们可能需要在代码中动态做 国际化,这就需要动态API 了。下面我简单介绍下怎么用

1.导入 injectIntl

import { injectIntl, FormattedMessage } from 'react-intl';

2.在组件中注入

export default connect(mapStateToProps,mapActionCreators)(injectIntl(App))

我在项目中用到了Redux,注入的时候应该向上面那样,如果你没有用Redux ,只需要 export defuault injectIntl(App)

3.使用 intl 对象

我们通过第二步的注入,现在在我们在 组件的 props 上会得到一个 intl 对象,它提供的方法和咱们上边介绍的组件基本相对应,这时候我们想要显示字符串,可以使用formatMessage 方法:

const {intl} = this.props;
  
let tmp = intl.formatMessage({id: 'intl.name'},{name: 'joe'});

formatMessage的第一个参数可以传入Id, 第二个参数传入 values ,更详细的了解,请查看 API

结束语

教程的代码,我已放到github 上,大家如果需要,自行查看 React-intl

React-intl 实现多语言的更多相关文章

  1. react项目实现多语言切换

    网站的语言切换功能大家都见过不少,一般都是一个下拉框选择语言,如果让我们想一下怎么实现这个功能,我相信大家都是有哥大概思路,一个语言切换的select,将当前的选择的语言存在全局,根据这个语言的key ...

  2. [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 ...

  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. kvm下Windows激活方式小计

    使用kvm创建widnwos镜像模板,镜像模板默认是已经激活的正版系统,但是使用程序拷贝部署到不同的机器后发现已经激活的系统变成未激活状态,我们需求就是需要拷贝到不同的机器也能显示是正版系统 网上找了 ...

  2. 《javascript语言精粹》——第6章数组

    [1].数组字面量 var empty=[]; var num=[ 'zero','one','two','three','four','five','six','seven','eight','ni ...

  3. 关于ios 推送功能的终极解决

    刚刚做了一个使用推送功能的应用 遇到了一些问题整的很郁闷 搞了两天总算是弄明白了 特此分享给大家 本帖 主要是针对产品发布版本的一些问题 综合了网上一些资料根据自己实践写的 不过测试也可以看看 首先要 ...

  4. git 常用命令--Linus Torvalds

    1.git log 显示仓库的历史记录,默认显示所有记录, 1)git log -m,显示最近的几次提交,, 2)git log --pretty=oneline  显示提交hash和注释 -p 按补 ...

  5. 关于浏览器和HTTP协议

    关于浏览器 浏览器的主要功能就是向服务器发出请求,在浏览器窗口中展示想要访问的网络资源.这里资源一般是指 HTML 文档,图片等其他的类型.资源的位置由用户使用 URL(统一资源标示符)指定. 而浏览 ...

  6. 2)Java学习笔记:匿名内部类

    为什么要使用匿名内部类 ①如果以前的类有一些缺陷,只是想在某一个模块进行修复,可以在引用该类的地方使用匿名内部类,在overRide方法进行修复. ②如果一个类,需要派生出很多类,而且这些类大多只是在 ...

  7. .NET Core installation guide

      .NET Core installation guide 1.Download Visual Studio 2015 Make sure you have Visual Studio 2015 U ...

  8. robotium从入门到放弃 一 测试开发环境搭建

    1.JDK的安装及环境变量的配置    配置JAVA的运行环境,添加完环境变量后,可以打开Windows命令处理程序窗口,通过执行命令java -version验证环境变量是否添加成功.如果添加成功会 ...

  9. 关于自己封装Web前端框架的思考和探索

    一.引言 首先这些年关于前端技术层出不穷,从最早的只用js做简单验证,到现在发现好像大前端已经无所不能了的感觉.特别是为了降低前端开发复杂度,涌现了一大批 的MVC/MVVM模式的前端框架,不停了刷新 ...

  10. VMware虚拟机与宿主无法复制的解决办法

    由于工作需要,上网机器使用虚拟机,因此需要经常来回的拷贝文件,而vmware从6.5一直走来到10.0.1,总是有一个问题很让人苦恼---共享粘贴板总是会无故失效.经常实验,发现可以经过以下方法临时解 ...