react-i8n

1. 在项目中安装

  npm install react-intl --save

2.兼容Safari各个版本需要安装intl

  npm install intl --save

3.编写语言包

  1.)新建 en_US.js

    const = en_US = {

      hello: "Hello world !",

      superHello: "Hello, { someone } ! "

    }

  2.)  新建 zh_CN.js

    const = en_US = {

      hello: "你好 世界!",

      superHello: "你好, { someone } ! "

    }

4.在最外层的 index.js中修改

  1.)引入文件  

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

 

    import zh_CN from './translations/zh_CN'

    import en_US from './translations/en_US'

  2.)在render里添加标签

    ReactDOM.render(
      <IntlProvider
        locale={'en'}
        messages={'en_US'}
      >
        <App/>
      </IntlProvider>, document.getElementById('root')
    );

5.在任何组件内使用

  1.)引入

    import { FormattedMessage, FormattedDate, FormattedNumber } from 'react-intl';

  2.)使用组件

    <h3><FormattedNumber value={1000}/></h3>

    <FormattedMessage id='hello' values={{someone:<b>{'增加的中文'}</b>}} description='say hello to Howard' defaultMessage='你好, Howard!' />







    




 

posted @ 2018-01-11 14:28  大耳朵小虎  阅读(244)  评论(0编辑  收藏  举报