JavaScript - Intl 国际化标准

发布于:2024-06-10 ⋅ 阅读:(87) ⋅ 点赞:(0)

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

1、Intl.Collator

比较字符串

Options 选项 描述
localeMatcher lookup , best fit 确定使用哪种算法来选择语言环境
usage sort ,search 指定比较的用途
sensitivity base, accent, case, variant 确定比较的灵敏度
ignorePunctuation Boolean 是否忽略标点符号
numeric Boolean 是否进行数字排序
caseFirst upper, lower, false 指定大写或小写字母优先

示例:

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

2、Intl.DateTimeFormat

用于格式化日期和时间

Options 选项 描述
localeMatcher lookup, best fit 确定使用哪种算法来选择语言环境
weekday narrow, short, long 指定显示星期的格式
era narrow, short, long 指定显示纪元的格式
year numeric, 2-digit 指定显示年份的格式
month numeric, 2-digit, narrow, short, long 指定显示月份的格式
day numeric, 2-digit 指定显示日期的格式
hour numeric, 2-digit 指定显示小时的格式
minute numeric, 2-digit 指定显示分钟的格式
second numeric, 2-digit 指定显示秒的格式
timeZoneName short, long 指定显示时区的格式
formatMatcher basic, best fit 用于选择最佳的格式匹配算法
hour12 Boolean 指定是否使用 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 选项 描述
localeMatcher lookup, best fit 确定使用哪种算法来选择语言环境
style decimal, currency, percent, unit 指定格式化的样式
currency USD, EUR … 指定货币代码
currencyDisplay symbol, narrowSymbol, code, name 指定货币显示的格式
useGrouping Boolean 指定是否使用分组分隔符
minimumIntegerDigits Number 指定最少整数位数
minimumFractionDigits Number 指定最少小数位数
maximumFractionDigits Number 指定最多小数位数
minimumSignificantDigits Number 指定最少有效位数
maximumSignificantDigits Number 指定最多有效位数

示例:

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 选项 描述
localeMatcher lookup, best fit 确定使用哪种算法来选择语言环境
type cardinal, 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 选项 描述
localeMatcher lookup, best fit 确定使用哪种算法来选择语言环境
numeric always, auto 指定是否总是使用数字
style long, 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 选项 描述
baseName en-US, … 基础语言环境名称
calendar gregory, buddhist 指定日历系统
caseFirst upper, lower, false 指定在排序中大写或小写字母优先
collation emoji, phonebook 指定排序规则
hourCycle h11, h12, h23, h24 指定小时循环系统
numeric Boolean 指定是否进行数字排序
numberingSystem latn, arab … 指定编号系统
region US, GB … 指定区域
script Latn, Cyrl 指定书写系统

示例:

const locale = new Intl.Locale('en-US', { numeric: true, caseFirst: 'upper' });
console.log(locale.baseName); // 输出: en-US