格式化带有数字,日期,复数和精选占位符的ICU消息字符串以创建本地化消息。
该软件包旨在为您提供一种管理和格式化JavaScript应用程序的字符串消息的方法,以适用于使用应用程序的人的本地化字符串。您可以通过Node.js在浏览器和服务器上使用此软件包。
此实施是基于稻草人的提议,但是在某些地方,该实施方案有所不同。
注意:此IntlMessageFormat API可能会更改以保持与ECMA-402同步,但是此软件包将遵循SEMVER。
将消息作为String消息或预先放置的AST对象提供在构造函数中。
var msg = new IntlMessageFormat ( message , locales , [ formats ] ) ;将字符串message解析,然后内部存储在编译形式中,该表格已针对format()方法进行了优化,以生成用于向用户显示的格式化字符串。
var output = msg . format ( values ) ;一个非常普遍的示例是格式化带有复数标签的数字的消息。使用此软件包,您可以确保为一个人的语言环境正确格式化字符串,例如:
var MESSAGES = {
'en-US' : {
NUM_PHOTOS : 'You have {numPhotos, plural, ' +
'=0 {no photos.}' +
'=1 {one photo.}' +
'other {# photos.}}'
} ,
'es-MX' : {
NUM_PHOTOS : 'Usted {numPhotos, plural, ' +
'=0 {no tiene fotos.}' +
'=1 {tiene una foto.}' +
'other {tiene # fotos.}}'
}
} ;
var output ;
var enNumPhotos = new IntlMessageFormat ( MESSAGES [ 'en-US' ] . NUM_PHOTOS , 'en-US' ) ;
output = enNumPhotos . format ( { numPhotos : 1000 } ) ;
console . log ( output ) ; // => "You have 1,000 photos."
var esNumPhotos = new IntlMessageFormat ( MESSAGES [ 'es-MX' ] . NUM_PHOTOS , 'es-MX' ) ;
output = esNumPhotos . format ( { numPhotos : 1000 } ) ;
console . log ( output ) ; // => "Usted tiene 1,000 fotos."该软件包使用的消息语法不是专有的,实际上,这是一种常见的标准消息语法,跨编程语言起作用,并且专业翻译人员熟悉一种。该软件包使用ICU消息语法,并适用于所有具有多元化规则的CLDR语言。
使用行业标准:ICU消息语法和CLDR语言环境数据。
支持复数,选择和选择性消息参数。
分别使用Intl.NumberFormat和Intl.DateTimeFormat格式化消息中的数字和日期/时间。
针对重复调用IntlMessageFormat实例的format()方法进行了优化。
支持定义自定义格式样式/选项。
支持消息语法字符的逃脱序列,例如: "\{foo\}"将输出: "{foo}"在格式化的输出中,而不是将其解释为foo参数。
Intl依赖性该软件包假定Intl全局对象存在于运行时。 Intl都存在于所有现代浏览器(IE11+)和节点(带有完整ICU)中。我们依赖的Intl方法是:
Intl.NumberFormat (可以使用intl.js进行多填充)Intl.DateTimeFormat用于日期时间格式化(可以使用intl.js进行多填充)Intl.PluralRules (可以使用intl-luralrules进行多填充) < script src =" intl-messageformat/intl-messageformat.min.js " > </ script > require()此软件包:
var IntlMessageFormat = require ( 'intl-messageformat' ) ;注意:您的节点必须包括完整的ICU
IntlMessageFormat构造函数要创建以格式化的消息,请使用IntlMessageFormat构造函数。构造函数采用三个参数:
消息- {字符串| AST} - 字符串消息(或预先放置的AST)用作格式模式。
LOCERES- {字符串|字符串[]} - 具有BCP 47语言标签的字符串或此类字符串数组。如果您不提供语言环境,则将使用默认场所。提供一系列位置时,将检查每个项目及其祖先地点,并返回带有注册语言环境数据的第一个。请参阅:本地分辨率有关更多详细信息。
[格式] - {Object} - 具有用户定义的格式样式选项的可选对象。
var msg = new IntlMessageFormat ( 'My name is {name}.' , 'en-US' ) ; IntlMessageFormat使用Intl.NumberFormat.supportedLocalesOf()来确定基于传递给构造函数的locales值使用的语言环境数据。该分辨率过程的结果可以通过调用resolvedOptions()原型方法来确定。
resolvedOptions()方法此方法返回一个具有实例创建期间已解决的选项值的对象。目前,它仅包含一个locale属性;这是一个例子:
var msg = new IntlMessageFormat ( '' , 'en-us' ) ;
console . log ( msg . resolvedOptions ( ) . locale ) ; // => "en-US"请注意,指定的语言环境是所有低案例值: "en-us" ,但已解决并归一化为"en-US" 。
format(values)方法创建消息后,通过在实例上调用format()方法并传递values集合来完成格式化消息:
var output = msg . format ( { name : "Eric" } ) ;
console . log ( output ) ; // => "My name is Eric."注意:必须为实例构建的消息模式中的每个参数提供一个值。
定义自定义格式样式很有用,您需要为基础格式化器提供一组选项;例如,在美元中输出一个数字:
var msg = new IntlMessageFormat ( 'The price is: {price, number, USD}' , 'en-US' , {
number : {
USD : {
style : 'currency' ,
currency : 'USD'
}
}
} ) ;
var output = msg . format ( { price : 100 } ) ;
console . log ( output ) ; // => "The price is: $100.00"在此示例中,我们将定义USD数字格式样式,该格式将传递给基础Intl.NumberFormat实例作为其选项。
此示例显示了如何使用ICU消息语法定义具有复数标签的消息;例如, "You have 10 photos" :
You have {numPhotos, plural,
=0 {no photos.}
=1 {one photo.}
other {# photos.}
}
var MESSAGES = {
photos : '...' , // String from code block above.
...
} ;
var msg = new IntlMessageFormat ( MESSAGES . photos , 'en-US' ) ;
console . log ( msg . format ( { numPhotos : 0 } ) ) ; // => "You have no photos."
console . log ( msg . format ( { numPhotos : 1 } ) ) ; // => "You have one photo."
console . log ( msg . format ( { numPhotos : 1000 } ) ) ; // => "You have 1,000 photos."注意:当numPhotos是1000时,该数字是用正确的千分隔符格式化的。
该软件可以在Yahoo!下免费使用。 Inc. BSD许可证。有关许可文本和版权信息,请参见许可证文件。