react-intl
internationalization standard
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl
The
Intlobject is the namespace for the ECMAScript Internationalization API, which provides language sensitive string comparison, number formatting, and date and time formatting. The INTL object provides access to several constructors as well as functionality common to the internationalization constructors and other language sensitive functions.
Intl.Collator- Constructor for collators, which are objects that enable language-sensitive string comparison.
Intl.DateTimeFormat- Constructor for objects that enable language-sensitive date and time formatting.
Intl.ListFormat- Constructor for objects that enable language-sensitive list formatting.
Intl.NumberFormat- Constructor for objects that enable language-sensitive number formatting.
Intl.PluralRules- Constructor for objects that enable plural-sensitive formatting and language-specific rules for plurals.
Intl.RelativeTimeFormat- Constructor for objects that enable language-sensitive relative time formatting.
library FormatJS
https://formatjs.io/
FormatJS is a modular collection of JavaScript libraries for internationalization that are focused on formatting numbers, dates, and strings for displaying to people. It includes a set of core libraries that build on the JavaScript
Intlbuilt-ins and industry-wide i18n standards, plus a set of integrations for common template and component libraries.
react-intl
https://github.com/yahoo/react-intl
Internationalize React apps. This library provides React components and an API to format dates, numbers, and strings, including pluralization and handling translations.
React Intl is part of FormatJS. It provides bindings to React via its components and API.
Features
- Display numbers with separators.
- Display dates and times correctly.
- Display dates relative to "now".
- Pluralize labels in strings.
- Support for 150+ languages.
- Runs in the browser and Node.js.
- Built on standards.
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')
);
单复数格式解释:
https://formatjs.io/guides/message-syntax/#plural-format
{plural}FormatThe
{key, plural, matches}is used to choose output based on the pluralization rules of the current locale. It is very similar to the{select}format above except that the value is expected to be a number and is mapped to a plural category.The
matchis a literal value and is matched to one of these plural categories. Not all languages use all plural categories.
zero- This category is used for languages that have grammar specialized specifically for zero number of items. (Examples are Arabic and Latvian.)
one- This category is used for languages that have grammar specialized specifically for one item. Many languages, but not all, use this plural category. (Many popular Asian languages, such as Chinese and Japanese, do not use this category.)
two- This category is used for languages that have grammar specialized specifically for two items. (Examples are Arabic and Welsh.)
few- This category is used for languages that have grammar specialized specifically for a small number of items. For some languages this is used for 2-4 items, for some 3-10 items, and other languages have even more complex rules.
many- This category is used for languages that have grammar specialized specifically for a larger number of items. (Examples are Arabic, Polish, and Russian.)
other- This category is used if the value doesn't match one of the other plural categories. Note that this is used for "plural" for languages (such as English) that have a simple "singular" versus "plural" dichotomy.
=value- This is used to match a specific value regardless of the plural categories of the current locale.
Cart: {itemCount} {itemCount, plural,
one {item}
other {items}
}You have {itemCount, plural,
=0 {no items}
one {1 item}
other {{itemCount} items}
}.In the
outputof the match, the#special token can be used as a placeholder for the numeric value and will be formatted as if it were{key, number}.You have {itemCount, plural,
=0 {no items}
one {# item}
other {# items}
}.
例子:
https://github.com/yahoo/react-intl/tree/master/examples
https://github.com/jiechud/georgy-react
API
https://github.com/yahoo/react-intl/wiki/API#injection-api
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 ...
- [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 ...
- [React Intl] Render Content with Placeholders using react-intl FormattedMessage
Learn how to use react-intl to set dynamic values into your language messages. We’ll also learn how ...
随机推荐
- 阿里云OCR图片转换成文字识别调用
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Drawing; using S ...
- SSH服务与tcp wrappers实验
SSH服务与tcp wrappers实验 实验环境: 一台linux(ssh client) 一台linux(ssh server) 实验步骤: 1.配置IP,测试连通性 2.在客户端创建用户yuzl ...
- Linux Mint如何安装“微信、QQ、迅雷、WPS办公软件”等国内上瘾软件
很多小伙伴都用ubuntu或者Linux Mint,但由于已经习惯了让人成瘾的国产软件,比如迅雷,qq,微信等,其实我们应该培养更为健康的上网习惯,这些软件不是非用不可,但如果你不用不行, 那么也是有 ...
- 【原创】谈谈线上CPU100%排查套路
引言 不知道在大家面试中,有没有遇到这个问题 生产服务器上部署了几个java程序,突然出现了CPU100%的异常告警,你如何定位出问题呢? 这个问题分为两版回答! 高调版 对不起,我是做研发的,这个问 ...
- 面试7家,收到5个offer,我的Python就业经验总结 !
*---------------------------------------人生处处有惊喜,背后却是无尽的辛酸苦辣. Python找工作并不容易,老表面试了很多企业,总结了些宝贵经验! 一周转 ...
- 02-Django框架介绍
02-Django框架介绍 1.MVC框架介绍 MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式 具有耦合性低.重用性高.生 ...
- WiFi攻击中“核武器”
3·15晚会上,央视曝光了WiFi探针盒子通过手机MAC地址.大数据匹配获取手机用户个人信息的典型案例. 其中,曝光的“声牙科技有限公司”号称有全国6亿手机用户的个人信息,包括手机号,只要将获取到的手 ...
- java基础-jdk工具包
1. 标准工具 这些工具都是JDK提供的,通常都是长期支持的工具,JDK承诺这些工具比较好用.不同系统.不同版本之间可能会有差异,但是不会突然就有一个工具消失. 1.1 基础包 (extcheck, ...
- Virtual DOM 系列三:Diff算法
DOM操作是昂贵的,为了减少DOM操作,才有了Virtual DOM.而Virtual DOM的关键就是通过对比新旧vnode,找出差异部分来更新节点.对比的关键算法就是Diff算法. 历史由来: d ...
- React Native & iframe & WebView
React Native & iframe & WebView React Native 怎么渲染 iframe 页面 WebView & source html https: ...