JavaScript - Intl 国际化标准

JavaScript 的 Intl 对象可以方便地进行各种国际化处理,包括货币、日期、时间和数字格式化

1、Intl.Collator

比较字符串

Options 选项描述
localeMatcherlookup , best fit确定使用哪种算法来选择语言环境
usagesort ,search指定比较的用途
sensitivitybase, accent, case, variant确定比较的灵敏度
ignorePunctuationBoolean是否忽略标点符号
numericBoolean是否进行数字排序
caseFirstupper, lower, false指定大写或小写字母优先

示例:

const collator = new Intl.Collator('en', { sensitivity: 'base' });
console.log(collator.compare('a', 'A')); // 输出: 0

2、Intl.DateTimeFormat

用于格式化日期和时间

Options 选项描述
localeMatcherlookup, best fit确定使用哪种算法来选择语言环境
weekdaynarrow, short, long指定显示星期的格式
eranarrow, short, long指定显示纪元的格式
yearnumeric, 2-digit指定显示年份的格式
monthnumeric, 2-digit, narrow, short, long指定显示月份的格式
daynumeric, 2-digit指定显示日期的格式
hournumeric, 2-digit指定显示小时的格式
minutenumeric, 2-digit指定显示分钟的格式
secondnumeric, 2-digit指定显示秒的格式
timeZoneNameshort, long指定显示时区的格式
formatMatcherbasic, best fit用于选择最佳的格式匹配算法
hour12Boolean指定是否使用 12 小时制
timeZone指定时区的 IANA 时区名称

示例:

const date = new Date();
const formatter = new Intl.DateTimeFormat('en-US', {
  year: 'numeric',
  month: 'long',
  day: '2-digit'
});
console.log(formatter.format(date)); // 输出: June 07, 2024

3、Intl.NumberFormat

用于格式化数字,包括货币和百分比

options 选项描述
localeMatcherlookup, best fit确定使用哪种算法来选择语言环境
styledecimal, currency, percent, unit指定格式化的样式
currencyUSD, EUR …指定货币代码
currencyDisplaysymbol, narrowSymbol, code, name指定货币显示的格式
useGroupingBoolean指定是否使用分组分隔符
minimumIntegerDigitsNumber指定最少整数位数
minimumFractionDigitsNumber指定最少小数位数
maximumFractionDigitsNumber指定最多小数位数
minimumSignificantDigitsNumber指定最少有效位数
maximumSignificantDigitsNumber指定最多有效位数

示例:

const number = 123456.789;
const formatter = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD'
});
console.log(formatter.format(number)); // 输出: $123,456.79

4、Intl.PluralRules

用于处理复数规则

options 选项描述
localeMatcherlookup, best fit确定使用哪种算法来选择语言环境
typecardinal, ordinal指定复数规则的类型

示例:

const pluralRules = new Intl.PluralRules('en-US');
console.log(pluralRules.select(0)); // 输出: other
console.log(pluralRules.select(1)); // 输出: one
console.log(pluralRules.select(2)); // 输出: other

5、Intl.RelativeTimeFormat

用于格式化相对时间

options 选项描述
localeMatcherlookup, best fit确定使用哪种算法来选择语言环境
numericalways, auto指定是否总是使用数字
stylelong, short, narrow指定相对时间的格式样式

示例:

const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
console.log(rtf.format(-1, 'day')); // 输出: yesterday
console.log(rtf.format(1, 'day'));  // 输出: tomorrow

6、Intl.Locale

用于处理和解析 BCP 47 语言标记

options 选项描述
baseNameen-US, …基础语言环境名称
calendargregory, buddhist指定日历系统
caseFirstupper, lower, false指定在排序中大写或小写字母优先
collationemoji, phonebook指定排序规则
hourCycleh11, h12, h23, h24指定小时循环系统
numericBoolean指定是否进行数字排序
numberingSystemlatn, arab …指定编号系统
regionUS, GB …指定区域
scriptLatn, Cyrl指定书写系统

示例:

const locale = new Intl.Locale('en-US', { numeric: true, caseFirst: 'upper' });
console.log(locale.baseName); // 输出: en-US
  • 20
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值