@formatjs/intl-支持值f
ECMA-402 光谱遵守
此套件执行 ECMA-402 Intl.supportedValuesOf() 规格 :
- 光谱科数字 : 8.3.2 支持的Intl.ValuesOf(钥匙)
- 回返: 国际化密钥的支持值
- 完全符合规格: 没有可选参数, 匹配本地浏览器行为
安装
npm install @formatjs/intl-supportedvaluesof
使用量
作为多填充
导入多填充器 Intl.supportedValuesOf 全球现有:
import '@formatjs/intl-supportedvaluesof/polyfill'
// Now available on the global Intl object
const calendars = Intl.supportedValuesOf('calendar')
// ['buddhist', 'chinese', 'coptic', 'dangi', 'ethioaa', 'ethiopic', 'gregory', ...]
const currencies = Intl.supportedValuesOf('currency')
// ['AED', 'AFN', 'ALL', 'AMD', 'ANG', 'AOA', 'ARS', 'AUD', ...]
const timeZones = Intl.supportedValuesOf('timeZone')
// ['Africa/Abidjan', 'Africa/Accra', ..., 'Pacific/Wallis']
直接导入
直接导入函数而不修改全局命名空间 :
import {supportedValuesOf} from '@formatjs/intl-supportedvaluesof'
const numberingSystems = supportedValuesOf('numberingSystem')
// ['adlm', 'ahom', 'arab', 'arabext', 'bali', 'beng', 'bhks', ...]
const units = supportedValuesOf('unit')
// ['acre', 'bit', 'byte', 'celsius', 'centimeter', 'day', 'degree', ...]
支持的密钥
该函数接受六个字符串密钥中的一个并返回一系列支持的值:
| 密钥 | 回返 | 示例数值 |
|---|---|---|
'calendar' | 支持的日历系统 | 'gregory', (中文(简体) ), 'islamic', (中文(简体) ), 'buddhist', (中文(简体) ), 'chinese' |
'collation' | 支持的整理算法 | 'emoji', (中文(简体) ), 'eor', (中文(简体) ), 'phonebk', (中文(简体) ), 'search' |
'currency' | 支持的 ISO 4217 货币代码 | 'USD', (中文(简体) ), 'EUR', (中文(简体) ), 'JPY', (中文(简体) ), 'GBP' |
'numberingSystem' | 支持的编号系统 | 'arab', (中文(简体) ), 'latn', (中文(简体) ), 'hanidec', (中文(简体) ), 'thai' |
'timeZone' | 支持的IANA时区 | 'America/New_York', (中文(简体) ), 'Europe/London', (中文(简体) ), 'Asia/Tokyo' |
'unit' | 支助的计量单位 | 'meter', (中文(简体) ), 'celsius', (中文(简体) ), 'liter', (中文(简体) ), 'acre' |
API 密码
supportedValuesOf(key: string): string[]
返回给定密钥的一系列支持值.
参数:
key- 一个:'calendar', (中文(简体) ),'collation', (中文(简体) ),'currency', (中文(简体) ),'numberingSystem', (中文(简体) ),'timeZone', (中文(简体) ),'unit'
回返情况:
- 排序的独有字符串值阵列
掷出 :
RangeError- 如果密钥不是支持的值之一
示例
import {supportedValuesOf} from '@formatjs/intl-supportedvaluesof'
// Valid usage
const calendars = supportedValuesOf('calendar')
console.log(calendars) // ['buddhist', 'chinese', 'coptic', ...]
// Invalid key throws RangeError
try {
supportedValuesOf('invalid')
} catch (e) {
console.error(e) // RangeError: Invalid key: invalid
}
执行细节
这种多填充 动态验证 候选人数值与实际 Intl 而不是维持静态清单。 这种办法确保:
- 准确性: 结果匹配您的 JavaScript 运行时间
- 特定运行时间: 不同的引擎( V8, JavaScriptCore, SpiderMonkey) 可能支持不同的值
- 最新数据: 引擎添加新值时不需要手动更新列表
如何运作
每一类别的执行情况:
- 装入候选值 出自QQR( Unicode 通用地块数据存储器)
- 测试每个值 试图创建适当的
Intl用于含有该值的事项 - 验证接受 检查是否实际使用请求的值
- 返回过滤列表 只有通过验证的值
示例:日历验证
// Implementation tests if a calendar is supported:
function isSupportedCalendar(calendar) {
try {
// Attempt to create DateTimeFormat with this calendar
const formatter = new Intl.DateTimeFormat(`en-u-ca-${calendar}`)
// Verify the calendar was actually accepted
const resolvedOptions = formatter.resolvedOptions()
return resolvedOptions.calendar === calendar
} catch {
return false
}
}
这只保证日历 实际支持 由您的运行时间返回,而不仅仅是一个静态列表.
类型脚本支持
此包包括了TypeScript类型定义. 当使用多填充时,全局 Intl 命名空间自动增强 :
import '@formatjs/intl-supportedvaluesof/polyfill'
// TypeScript knows about Intl.supportedValuesOf
const calendars: string[] = Intl.supportedValuesOf('calendar')
// TypeScript will error on invalid keys
Intl.supportedValuesOf('invalid') // Error: Argument of type '"invalid"' is not assignable...
浏览器支持
这种多填充只针对没有本地支持的更老浏览器 Intl.supportedValuesOf()土著支助有:
- 99克罗姆克
- 边缘99+
- Firefox 93+ (英语)
- 萨法里15.4+
混合填充需要下列条件: Intl 可用的 API :
Intl.DateTimeFormat(计:calendar, (中文(简体) ),timeZone, (中文(简体) ),numberingSystem(中文(简体) )Intl.NumberFormat(计:currency, (中文(简体) ),unit, (中文(简体) ),numberingSystem(中文(简体) )Intl.Collator(计:collation(中文(简体) )
正在突破 v3. 0 的更改
删除:非标准 locale 参数
v2.x 及更早时 有非标准选项 locale 按语言过滤结果的参数。 这是 删除 与ECMA-402规格相匹配.
// ❌ v2.x (non-standard, no longer works)
const calendars = supportedValuesOf('calendar', 'en-US')
// ✅ v3.0 (spec-compliant)
const calendars = supportedValuesOf('calendar')
函数现在总是返回 全体 支持的值,匹配本地浏览器行为.
移徙指南
如果您在 v3. 0+ 中需要指定本地端过滤, 请自行执行 :
import {supportedValuesOf} from '@formatjs/intl-supportedvaluesof'
// Get all supported calendars
const allCalendars = supportedValuesOf('calendar')
// Filter by what a specific locale actually uses/accepts
const enUSCalendars = allCalendars.filter(calendar => {
try {
const formatter = new Intl.DateTimeFormat('en-US', {calendar})
return formatter.resolvedOptions().calendar === calendar
} catch {
return false
}
})
然而,请注意,大多数使用案例不需要特定本地化过滤,因为光谱的设计返回了所有值 任意 当地人可以使用.
实例
检查是否支持日历
import {supportedValuesOf} from '@formatjs/intl-supportedvaluesof'
const calendars = supportedValuesOf('calendar')
if (calendars.includes('islamic')) {
console.log('Islamic calendar is supported')
const formatter = new Intl.DateTimeFormat('en-US', {calendar: 'islamic'})
console.log(formatter.format(new Date()))
}
显示所有支持的货币
import {supportedValuesOf} from '@formatjs/intl-supportedvaluesof'
const currencies = supportedValuesOf('currency')
console.log(`This runtime supports ${currencies.length} currencies:`)
currencies.forEach(currency => {
const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency,
})
console.log(`${currency}: ${formatter.format(100)}`)
})
验证用户输入
import {supportedValuesOf} from '@formatjs/intl-supportedvaluesof'
function isValidTimeZone(timeZone) {
const supportedTimeZones = supportedValuesOf('timeZone')
return supportedTimeZones.includes(timeZone)
}
console.log(isValidTimeZone('America/New_York')) // true
console.log(isValidTimeZone('Invalid/TimeZone')) // false
许可证
麻省理工学院
相关软件包
- @formatjs/intl (中文(简体) ) - 完整格式JS国际化套件
- @formatjs/intl-date-timeformat (中文(简体) ) -内特 日期时间格式多填充
- @formatjs/intl-numberformat (中文(简体) ) -内特 数字格式多填充
- @formatjs/intl-pluralrules (英语) -内特 多规则多填充