前言

之前写过一篇关于React的国际化文章,主要是用react-intl库,雅虎开源的。react-intl是用高阶组件包装一层来做国际化。

基于组件化会有一些问题,比如在一些工具方法中需要国际化,就需要自己去实现了。在umi-react快速开发框架我们采用

react-intl-universal库来实现,不仅支持组件化调用,也支持动态调用,实现国际化。

react-intl-universal 用法

  1. 安装
```
npm install react-intl-universal --save
```
  1. 初始化

    在以下示例中,我们intl使用app locale data(locales)初始化并确定动态使用哪个区域设置(currentLocale).

    然后intl.get(...)用来获取国际化的消息

      import intl from 'react-intl-universal';
    const locales = {
    "en-US": require('./locales/en-US.js'),
    "zh-CN": require('./locales/zh-CN.js'),
    }; class App extends Component { state = {initDone: false} componentDidMount() {
    this.loadLocales();
    } loadLocales() {
    // init method will load CLDR locale data according to currentLocale
    // react-intl-universal is singleton, so you should init it only once in your app
    intl.init({
    currentLocale: 'en-US', // TODO: determine locale here
    locales,
    })
    .then(() => {
    // After loading CLDR locale data, start to render
    this.setState({initDone: true});
    });
    } render() {
    return (
    this.state.initDone &&
    <div>
    {intl.get('SIMPLE')}
    </div>
    );
    } }
  2. 调用

    • HTML Message (HTML 消息)

      locale data:

      { "TIP": "This is <span style='color:red'>HTML</span>" }

      js code:

      intl.getHTML('TIP');
    • Default Message (设置默认消息)

      js code:

      intl.get('not-exist-key').defaultMessage('default message')

      也可以用简写设置默认值

      intl.get('not-exist-key').d('default message')

      getHTML 也支持默认值

      intl.getHTML('not-exist-key').d(<div>hello</div>)
    • Message With Variables (消息添加变量)

      locale data:

      { "HELLO": "Hello, {name}. Welcome to {where}!" }

      js code:

      intl.get('HELLO', {name:'Tony', where:'Alibaba'})

在umi-react项目中加入国际化

  1. 在根目录新建locales文件夹, 添加locale描述文件

    en_US.js 文件

      export default {
    user: {
    login: {
    loginBtn: 'login',
    placeholderName: 'Please input user name',
    placeholderPws: 'Please input password',
    forgetPwd: 'Forget password',
    remember: 'Remember me',
    },
    logout: 'logout'
    }
    }

zh_CN.JS 文件

  ```
export default {
user: {
login: {
loginBtn: '登录',
placeholderName: '请输入用户名',
placeholderPws: '请输入密码',
forgetPwd: '忘记密码',
remember: '记住我',
},
logout: '退出登录'
}
}
```
  1. 在global modle中添加初始化state和Effect(changeLocale)和reducers(setLocale)

      import intl from 'react-intl-universal';
    import locales from '../locales';
    import storage from 'utils/localStorage'; const defaultState = {
    currLocale: storage.get('locale') || 'zh_CN',
    localeLoad: false,
    } export default {
    namespace: 'global', state: defaultState, effects: {
    *changeLocale({ payload }, { call, put }) {
    const params = {
    currentLocale: payload,
    locales
    };
    // 初始化国际化
    yield intl.init(params); yield put({
    type: 'setLocale',
    payload: {
    currLocale: payload,
    localeLoad: true,
    }
    });
    // 把当前国际化持久化到 localstorage 中
    storage.add('locale', payload);
    },
    }, reducers: {
    setLocale(state, { payload }) {
    return {
    ...state,
    ...payload,
    };
    },
    },
    };
  2. 在layouts index.js 中掉用changeLocale初始化国际化和antd组件国际化

    import React, { Component } from 'react'
    import BaseLayout from './baseLayout';
    import { LocaleProvider } from 'antd';
    import { connect } from 'dva';
    import zh_CN from 'antd/lib/locale-provider/zh_CN';
    import en_US from 'antd/lib/locale-provider/en_US';
    import { init } from './init'; @connect(({global}) => {
    return {
    currLocale: global.currLocale,
    localeLoad: global.localeLoad,
    }
    })
    class Index extends Component {
    constructor() {
    super();
    init();
    this.state = {
    initDone: false,
    }
    } componentDidMount() {
    const {dispatch, currLocale} = this.props;
    // 更改国际化
    dispatch({
    type: 'global/changeLocale',
    payload: currLocale,
    });
    }
    /**
    * 初始intl国际化和antd组件国际化
    */
    renderBody = () => {
    const {location: {pathname}, children, currLocale, localeLoad } = this.props;
    if (pathname === '/login') {
    return localeLoad && <React.Fragment>
    {children}
    </React.Fragment>;
    }
    return (
    localeLoad && (<LocaleProvider locale={ currLocale === 'zh_CN' ? zh_CN : en_US }>
    <BaseLayout {...this.props} />
    </LocaleProvider>)
    );
    } render() {
    return (
    <React.Fragment>
    {this.renderBody()}
    </React.Fragment>
    )
    }
    } export default Index;
  3. 在 src/baseLayout/header.js 添加更改国际化的 select

    import React, { Component } from 'react';
    import { Avatar, Dropdown, Menu, Icon, Select } from 'antd';
    import { connect } from 'dva';
    import intl from 'react-intl-universal';
    import styles from './baseLayout.less'; const Option = Select.Option; @connect(({user}) => {
    return {
    user: user.user
    }
    })
    class Header extends Component {
    /**
    * 切换语言
    */
    onLocaleChange = (value) => {
    this.props.dispatch({
    type: 'global/changeLocale',
    payload: value,
    })
    } render() {
    const {currLocale, user} = this.props;
    return (
    <div className={styles.header}>
    <div className={styles.headerButton}>
    <Select
    defaultValue={currLocale}
    style={{ width: 100 }}
    onChange={this.onLocaleChange}>
    <Option value='zh_CN'>中文</Option>
    <Option value='en_US'>English</Option>
    </Select>
    </div>
    </div>
    )
    }
    } export default Header;
  4. 到此我们的系统国际化就可以用了。我们把登陆页面国际化完善起来。这要调用intl.get方法

    import intl from 'react-intl-universal';
    @connect(({user}) => ({
    loginErr: user.loginErr,
    }))
    @Form.create()
    class Login extends React.Component {
    render() {
    const { loginErr, form:{ getFieldDecorator } } = this.props;
    const intlLogin = intl.get('user.login.loginBtn');
    const intlUsername = intl.get('user.login.placeholderName');
    const intlPwd = intl.get('user.login.placeholderPws');
    const intlforgetPwd = intl.get('user.login.forgetPwd');
    const intlRemember = intl.get('user.login.remember');
    return (
    <div className={styles.login}>
    { loginErr && <Alert style={{ marginBottom: '20px' }} message='用户名密码错误' type='error' showIcon />}
    <Form onSubmit={this.handleSubmit} className='login-form'>
    <FormItem>
    {getFieldDecorator('name', {
    rules: [
    FormValid.require(intlUsername),
    ],
    })(
    <Input
    prefix={<Icon type='user' style={{ color: 'rgba(0,0,0,.25)' }} />}
    placeholder={intlUsername}
    />
    )}
    </FormItem>
    <FormItem>
    {getFieldDecorator('password', {
    rules: [
    FormValid.require(intlPwd),
    ],
    })(
    <Input
    prefix={<Icon type='lock' style={{ color: 'rgba(0,0,0,.25)' }} />}
    type='password'
    placeholder={intlPwd}
    />
    )}
    </FormItem>
    <FormItem>
    {getFieldDecorator('remember', {
    valuePropName: 'checked',
    initialValue: true,
    })(
    <Checkbox>{intlRemember}</Checkbox>
    )}
    <a className='login-form-forgot' href=''>{intlforgetPwd}</a>
    <Button type='primary' htmlType='submit' className='login-form-button'>
    {intlLogin}
    </Button>
    </FormItem>
    </Form>
    </div>
    );
    }
    }
    export default Login;





结束语

国际化已经完成, 代码已放到github上,大家可以自行查看umi-react。如果觉得不错,请 start 一下

我建了一个QQ群,大家加进来,可以一起交流。群号 787846148

02 基于umi搭建React快速开发框架(国际化)的更多相关文章

  1. 01 基于umi搭建React快速开发框架

    介绍 基于umi搭建一个快速开发框架,react 应用框架.umi 以路由为基础的,支持类 next.js 的约定式路由,以及各种进阶的路由功能,并以此进行功能扩展,比如支持路由级的按需加载. 我们会 ...

  2. 03 基于umi搭建React快速开发框架(封装列表增删改查)

    前言 大家在做业务系统的时候,很多地方都是列表增删改查,做这些功能占据了大家很长时间,如果我们有类似的业务,半个小时就能做出一套那是不是很爽呢. 这样我们就可以有更多的时间学习一些新的东西.我们这套框 ...

  3. ABP开发框架前后端开发系列---(14)基于Winform的ABP快速开发框架

    前面介绍了很多ABP系列的文章,一步一步的把我们日常开发中涉及到的Web API服务构建.登录日志和操作审计日志.字典管理模块.省份城市的信息维护.权限管理模块中的组织机构.用户.角色.权限.菜单等内 ...

  4. Mario是一个基于.NETCore的简单快速开发框架

    Mario .NET Core简单快速开发框架 Mario是一个基于.NET Core的简单快速开发框架 GitHub:https://github.com/deeround/Mario 技术特点 基 ...

  5. SpringCloud微服务实战——搭建企业级开发框架(四十六):【移动开发】整合uni-app搭建移动端快速开发框架-环境搭建

      近年来uni-app发展势头迅猛,只要会vue.js,就可以开发一套代码,发布移动应用到iOS.Android.Web(响应式).以及各种小程序(微信/支付宝/百度/头条/飞书/QQ/快手/钉钉/ ...

  6. 【玩转 WordPress】基于 Serverless 搭建个人博客图文教程,学生党首选!

    以下内容来自「玩转腾讯云」用户原创文章,已获得授权. 01. 什么是 Serverless? 1. Serverless 官方定义 Serverless 中的 Server是服务器的意思,less 是 ...

  7. 基于SpringBoot+Mybatis+AntDesign快速开发平台,Jeecg-Boot 1.1 版本发布

    Jeecg-Boot 1.1 版本发布,初成长稳定版本 导读     平台首页UI升级,精美的首页支持多模式 提供4套代码生成器模板(支持单表.一对多) 集成Excel简易工具类,支持单表.一对多导入 ...

  8. Known快速开发框架

    Known是一个基于.NET开发的快速开发框架,前后端分离,使用极少的第三方组件,开发简单快速,大部分代码可通过代码生成工具自动生成,非常适合单兵作战和小团队开发.前端UI是一个基于JQuery开发的 ...

  9. Reactjs-generator-cli 一款基于Ink构建用于快速搭建React应用的CLI scaffolding工具

    Reactjs-generator-cli 一款基于Ink构建用于快速搭建React应用的CLI scaffolding工具 A simple CLI for scaffolding React.js ...

随机推荐

  1. Photoshop 基础四 填充(渐变、油漆桶)

    一 渐变 二 油漆桶 一 渐变 技术点:选框工具(矩形.圆形.选区.反选).变换(透视).渐变 1)新建图层——>矩形选框工具,画一个矩形 2)渐变工具,定义渐变颜色 3)编辑——>变换— ...

  2. harbor Configuring Harbor with HTTPS Access

    首先,下载fq (fanqiang) harbor-offline-installer-v1.2.0-rc5.tgz tar xvf harbor-offline-installer-<vers ...

  3. 10-51单片机ESP8266学习-AT指令(ESP8266连接路由器,建立TCP服务器,分别和C#TCP客户端和AndroidTCP客户端通信+花生壳远程通信)

    http://www.cnblogs.com/yangfengwu/p/8871464.html 先把源码和资料链接放到这里 源码链接:https://pan.baidu.com/s/1wT8KAOI ...

  4. [01] JSP的基本认识

    1.什么是JSP JSP,全称JavaServer Pages,是由Sun Microsystems公司倡导和许多公司参与共同建立的一种使软件开发者可以响应客户端请求,而动态生成HTML.XML或其他 ...

  5. pycharm 取消 rebase 操作

    291/5000 取消rebase操作从主菜单中选择VCS | Git | 中止重新定位(abrot rebasing)如果rebase在两个或多个本地存储库中启动,则会显示“中止重新排序”对话框. ...

  6. TensorFlow框架下的RNN实践小结

    截至目前,TensorFlow的RNN APIs还处于Draft阶段.不过据官方解释,RNN的相关API已经出现在Tutorials里了,大幅度的改动应该是不大可能,现在入手TF的RNN APIs风险 ...

  7. 爬虫学习--http请求详解

    上篇博客里面写了,爬虫就是发http请求(浏览器里面打开发送的都是http请求),然后获取到response,咱们再从response里面找到想要的数据,存储到本地. 咱们本章就来说一下什么是http ...

  8. 学习ML.NET(2): 使用模型进行预测

    训练模型 在上一篇文章中,我们已经通过LearningPipeline训练好了一个“鸢尾花瓣预测”模型, var model = pipeline.Train<IrisData, IrisPre ...

  9. CSharp 案例:用 Dynamic 来解决 DataTable 数值累加问题

    需求说明 给定一个 DataTable,如果从中取出数值类型列的值并对其累加? 限制:不知该列是何种数值类型. 解决方案 1.将表转换为 IEnumerable<dynamic>,而后获取 ...

  10. Vue2.0 搭配 axios

    1.安装axios $ npm install axios 2.Demo (1)Get // 为给定 ID 的 user 创建请求 axios.get('/user?ID=12345') .then( ...