格式化帶有數字,日期,複數和精選佔位符的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許可證。有關許可文本和版權信息,請參見許可證文件。