react-intl

Submitted by Lizhe on Thu, 06/14/2018 - 15:36

Internationalize React apps. This library provides React components and an API to format dates, numbers, and strings, including pluralization and handling translations.

react intl 用于格式化日期, 数字, 字符串多元化和翻译(实际上可以用做国际化)

安装

$ npm install react-intl --save

 

 

import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import {IntlProvider, FormattedMessage} from 'react-intl';
 
class App extends Component {
    constructor(props) {
        super(props);
        this.state = {
            name       : 'Eric',
            unreadCount: 1000,
        };
    }
 
    render() {
        const {name, unreadCount} = this.state;
 
        return (
            <p>
                <FormattedMessage
                    id="welcome"
                    defaultMessage={`Hello {name}, you have {unreadCount, number} {unreadCount, plural,
                      one {message}
                      other {messages}
                    }`}
                    values={{name: <b>{name}</b>, unreadCount}}
                />
            </p>
        );
    }
}
 
ReactDOM.render(
    <IntlProvider locale="en">
        <App />
    </IntlProvider>,
    document.getElementById('container')
);