结论先行:日期、数字、货币、复数、列表、相对时间的国际化格式化,浏览器原生 Intl 全家桶已全部覆盖,主流环境支持多年,多数项目不必再为「格式化」引入第三方库。本文给出六大格式化器的可运行示例,并讲清 locale 协商与格式化器缓存两个工程要点。
日期时间:DateTimeFormat
告别手写 YYYY-MM-DD 拼接,让引擎按目标语言输出:
const d = new Date('2026-11-20T09:30:00Z');
new Intl.DateTimeFormat('zh-CN', { dateStyle: 'long', timeStyle: 'short' })
.format(d); // 2026年11月20日 09:30(按本地时区)
new Intl.DateTimeFormat('en-US', { month: 'short', day: 'numeric' })
.format(d); // Nov 20
new Intl.DateTimeFormat('ja-JP', { era: 'long', year: 'numeric' })
.format(d); // 令和8年指定 timeZone: 'Asia/Shanghai' 可固定展示时区,避免设备时区干扰。需要「某时区的墙上时间」运算时再考虑 Temporal,纯展示 Intl 足够。
数字与货币:NumberFormat
千分位、百分比、货币符号、小数位控制一个对象全包,还能做紧凑计数:
new Intl.NumberFormat('zh-CN').format(1234567.89); // 1,234,567.89
new Intl.NumberFormat('zh-CN', { style: 'currency', currency: 'CNY' })
.format(99.5); // ¥99.50
new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' })
.format(99.5); // $99.50
new Intl.NumberFormat('zh-CN', { notation: 'compact' })
.format(1234567); // 123万货币小数位由币种决定(日元默认 0 位)。注意 Intl 只负责展示,金额计算仍应以分为单位做整数运算,避免浮点误差。
复数与列表:PluralRules、ListFormat
英语的 1 item / 2 items、俄语的三档复数,手写 if 永远写不全。PluralRules 返回 CLDR 复数类别,配合文案表即可:
const pr = new Intl.PluralRules('en-US');
const forms = { one: 'item', other: 'items' };
function label(n) { return n + ' ' + forms[pr.select(n)]; }
label(1); // '1 item'
label(5); // '5 items'ListFormat 处理「A、B 和 C」这类并列,连词与分隔符随语言变化:
new Intl.ListFormat('zh-CN', { type: 'conjunction' })
.format(['苹果', '香蕉', '橙子']); // 苹果、香蕉和橙子
new Intl.ListFormat('en-US', { type: 'disjunction' })
.format(['red', 'blue']); // red or blue相对时间与排序:RelativeTimeFormat、Collator
「3 天前」「in 2 hours」用 RelativeTimeFormat,numeric: 'auto' 还能输出「昨天」这类自然表达:
new Intl.RelativeTimeFormat('zh-CN', { numeric: 'auto' })
.format(-1, 'day'); // 昨天多语言列表排序别用默认 sort(按码元排),Collator 按语言习惯比较,中文可按拼音:
const names = ['张三', '李四', '王五'];
names.sort(new Intl.Collator('zh-CN').compare);
new Intl.Collator('zh-CN', { sensitivity: 'base' }).compare('a', 'A'); // 0工程要点:locale 协商与缓存
第一,locale 传数组让引擎协商:new Intl.NumberFormat(['zh-Hans-CN', 'zh-CN', 'en-US']),取第一个支持的;resolvedOptions() 可拿到实际生效的 locale 与时区。第二,格式化器构造成本高,循环里反复 new 是性能坑,应按「locale + 选项」缓存实例:
const cache = new Map();
function money(locale, currency) {
const key = locale + '|' + currency;
if (!cache.has(key)) {
cache.set(key, new Intl.NumberFormat(locale, { style: 'currency', currency }));
}
return cache.get(key);
}第三,Intl 只管格式化不管文案翻译,完整 i18n 还需文案表与切换机制,但格式化层可以零依赖。
结合业务:多端商城的价格与时间展示
做跨境或多语言商城时,价格与活动时间必须跟随用户语言。api工厂(https://www.it120.cc/)支持微信支付、支付宝、PayPal、Stripe 等渠道,商品接口返回统一数值金额,展示层用 Intl 按 locale 渲染即可。以 Vue/Web 端 SDK 为例:
import { WXAPI } from 'apifm-webapi';
WXAPI.init('your-subdomain');
const res = await WXAPI.goodsDetail(id);
if (res.code === 0) {
const price = money(navigator.language, 'CNY').format(res.data.basicInfo.minPrice);
console.log(price);
}SDK 统一返回 {code,data,msg}、方法返回 Promise,六端接口一致,格式化逻辑可跨端复用。开源项目 vueMinishop、wechat-app-mall 等(大全见 https://www.it120.cc/help/uq920k.html)可参考其商品页结构。
小结
Intl 是被低估的原生基建:六大格式化场景全覆盖,数据来自 CLDR 随引擎更新。落地三件事——locale 数组协商、格式化器按配置缓存、金额计算与展示分离。格式化相关的第三方依赖基本可以清空。
文章标签
相关产品
api工厂是面向开发者的开放 SaaS 平台:开箱即用的后台管理系统 + 全端 SDK,只需前端开发即可构建小程序、App、H5 与网站。