JavaScript中如何使用IntlAPI?

来源:IPIPP.com作者:头衔:全栈工程师
导读:本期聚焦于小伙伴创作的《JavaScript中如何使用IntlAPI?》,敬请观看详情,探索知识的价值。以下视频、文章将为您系统阐述其核心内容与价值。如果您觉得《JavaScript中如何使用IntlAPI?》有用,将其分享出去将是对创作者最好的鼓励。

JavaScript的IntlAPI是ECMAScript标准内置的国际化对象,提供了一系列用于字符串比较、数字格式化、日期时间格式化的构造函数,能够帮助开发者快速实现应用的多地区适配,无需依赖第三方国际化库。

JavaScript中如何使用IntlAPI?

Intl.DateTimeFormat:日期时间格式化

Intl.DateTimeFormat用于处理日期和时间的本地化格式化,第一个参数是语言标签字符串或数组,第二个参数是配置选项对象。

// 基础用法:格式化当前时间为中文简体格式
const now = new Date();
const cnDateFormatter = new Intl.DateTimeFormat('zh-CN');
console.log(cnDateFormatter.format(now)); // 输出类似 2024/5/20

// 自定义配置:展示完整日期和时间
const fullDateFormatter = new Intl.DateTimeFormat('zh-CN', {
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour: '2-digit',
  minute: '2-digit',
  second: '2-digit',
  hour12: false
});
console.log(fullDateFormatter.format(now)); // 输出类似 2024年5月20日 14:30:00

// 适配不同地区格式
const usDateFormatter = new Intl.DateTimeFormat('en-US');
console.log(usDateFormatter.format(now)); // 输出类似 5/20/2024

Intl.NumberFormat:数字与货币格式化

Intl.NumberFormat可以处理普通数字、百分比、货币的本地化展示,支持配置小数位数、分组分隔符等规则。

const num = 1234567.89;

// 普通数字格式化,中文环境下使用逗号作为千分位分隔符
const cnNumFormatter = new Intl.NumberFormat('zh-CN');
console.log(cnNumFormatter.format(num)); // 输出 1,234,567.89

// 货币格式化,人民币显示为¥符号
const rmbFormatter = new Intl.NumberFormat('zh-CN', {
  style: 'currency',
  currency: 'CNY'
});
console.log(rmbFormatter.format(num)); // 输出 ¥1,234,567.89

// 百分比格式化
const percentFormatter = new Intl.NumberFormat('zh-CN', {
  style: 'percent',
  minimumFractionDigits: 2
});
console.log(percentFormatter.format(0.1234)); // 输出 12.34%

// 美元货币适配
const usdFormatter = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD'
});
console.log(usdFormatter.format(num)); // 输出 $1,234,567.89

Intl.Collator:字符串比较与排序

Intl.Collator用于按照本地化规则进行字符串比较和排序,支持配置大小写敏感度、重音符号处理等规则。

const words = ['apple', 'Banana', 'cherry', 'Date'];

// 默认排序,区分大小写
const defaultCollator = new Intl.Collator('en-US');
console.log([...words].sort(defaultCollator.compare)); 
// 输出 ["Banana", "Date", "apple", "cherry"]

// 不区分大小写排序
const caseInsensitiveCollator = new Intl.Collator('en-US', {
  sensitivity: 'base'
});
console.log([...words].sort(caseInsensitiveCollator.compare));
// 输出 ["apple", "Banana", "cherry", "Date"]

// 中文排序,按拼音顺序
const cnWords = ['张三', '李四', '王五', '赵六'];
const cnCollator = new Intl.Collator('zh-CN');
console.log([...cnWords].sort(cnCollator.compare));
// 输出 ["李四", "王五", "张三", "赵六"]

其他常用Intl接口

除了上述三个核心接口,Intl还提供了其他实用接口:

  • Intl.ListFormat:用于格式化列表,比如将数组转为自然语言的列表字符串,支持"and""or"等连接词配置
  • Intl.RelativeTimeFormat:用于格式化相对时间,比如"3天前""2小时后"这类表述
  • Intl.PluralRules:用于根据数量选择对应的复数形式,适配不同语言的复数规则
// Intl.RelativeTimeFormat示例
const relativeFormatter = new Intl.RelativeTimeFormat('zh-CN', {
  numeric: 'auto'
});
console.log(relativeFormatter.format(-3, 'day')); // 输出 前天
console.log(relativeFormatter.format(2, 'hour')); // 输出 2小时后

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

使用注意事项

在使用IntlAPI时需要注意以下几点:

  • IntlAPI是ECMAScript标准内置对象,现代浏览器和Node.js环境都原生支持,无需额外安装依赖
  • 语言标签需要符合BCP 47规范,比如zh-CN代表中文简体,en-US代表美国英语
  • 如果传入的语言标签不被环境支持,Intl会使用最接近的可用语言作为 fallback,不会直接报错
  • 对于性能敏感的场景,可以缓存创建好的Intl实例,避免重复初始化带来的开销

Intl_APIJavaScript国际化格式化本地化修改时间:2026-06-03 00:45:46

免责声明:​ 已尽一切努力确保本网站所含信息的准确性。网站内容多为原创整理与精心编撰,观点力求客观中立。本站旨在免费分享,内容仅供个人学习、研究或参考使用。若引用了第三方作品,版权归原作者所有。如内容涉及您的权益,请联系我们处理。
内容垂直聚焦
专注技术核心技术栏目,确保每篇文章深度聚焦于实用技能。从代码技巧到架构设计,为用户提供无干扰的纯技术知识沉淀,精准满足专业提升需求。
知识结构清晰
覆盖从开发到部署的全链路。AI、前端、编程、数据库、服务器、建站、系统层层递进,构建清晰学习路径,帮助用户系统化掌握开发与运维所需的核心技术。
深度技术解析
拒绝泛泛而谈,深入技术细节与实践难点。无论是数据库优化还是服务器配置,均结合真实场景与代码示例进行剖析,致力于提供可直接应用于工作的解决方案。
专业领域覆盖
精准对应开发生命周期。从前端界面到后端编程,从数据库操作到服务器运维,形成完整闭环,一站式满足全栈工程师和运维人员的技术需求。
即学即用高效
内容强调实操性,步骤清晰、代码完整。用户可根据教程直接复现和应用于自身项目,显著缩短从学习到实践的距离,快速解决开发中的具体问题。
持续更新保障
专注既定技术方向进行长期、稳定的内容输出。确保各栏目技术文章持续更新迭代,紧跟主流技术发展趋势,为用户提供经久不衰的学习价值。