react-intl
internationalization standard
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl
The
Intl
object 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
Intl
built-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
match
is 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
output
of 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 ...
随机推荐
- WPF软件开发系统之五——展会展厅触摸屏企业产品宣传展示系统
本系统开发背景:上海展会多点触摸大屏(60寸以上)上互动展示. 功能包括:企业背景.产品.合作伙伴.所获荣誉等以图片.文字.视频多媒体的方式呈块状显示,亮点功能为支持多点操作去旋转.缩放.拖拽呈现各种 ...
- U盘制作启动盘
https://jingyan.baidu.com/article/15622f24322f52fdfcbea58b.html UltraISO v9.65.3237 官方版及注册码 保存为uikey ...
- RocketMQ4.3.X关于设置useEpollNativeSelector = true报错问题
前一阵子刚整理完RocketMQ4.3.x版本的相关配置的工作,接下来就来测试一下改变参数会带来什么好的结果 首先我就选中了useEpollNativeSelector 这个参数 默认这个参数是 fa ...
- P1551 亲戚题解
标准并查集板子题 没啥好说的,分明是白书上的(除了输入方式外一点都没改动) #include<cstdio> #include<iostream> using namespac ...
- 身边有个漂亮的java女程序员是什么体验?
程序员都是一些追求完美的人.女程序员细致认真,不仅能写代码而且注释详尽清晰.能做好单元测试BUG最少.能写标准规范的设计文件不会对不上模块编号也不会少了类或接口说明,提交代码不会忘记写LOG,不会和测 ...
- Educational Codeforces Round 61 (Rated for Div. 2)-C. Painting the Fence 前缀和优化
题意就是给出多个区间,要求去掉两个区间,使得剩下的区间覆盖范围最大. 当然比赛的时候还是没能做出来,不得不佩服大佬的各种姿势. 当时我想的是用线段树维护区间和,然后用单点判0,维护区间间断个数.然后打 ...
- 一些矩阵范数的subgradients
目录 引 正交不变范数 定理1 定理2 例子:谱范数 例子:核范数 算子范数 定理3 定理4 例子 \(\ell_2\) <Subgradients> Subderivate-wiki S ...
- 项目总结-timerTask的使用
关于使用timerTask来进行定时任务的研究 业务说明:每天的0点执行一次 调用说明:com.hzmd.itest.db.ItestDbUtil中的startPermitTimer()方法进行最终的 ...
- js面向对象学习
纯属笔记,加强记忆,不是教程,欢迎纠错,没有逻辑,不太适合学习使用. -------------- 继承多态等太多概念难以理解,还是从实践中慢慢学吧!争取能大致看懂网上的开源的代码. -------- ...
- redis session 共享 测试案列
下载 spring redis session demo 2.分别在不同的服务器上启动 3.nginx 安装 测试