Intl API:你没有在使用的最好的浏览器 API

Chances are you've used Moment.js, date-fns, Luxon, or numeral.js at some point. Developers have relied on these libraries for years to format dates, numbers, and currencies. Those are all very useful libraries, but they also come with a cost: they add kilobytes to your download size and they require client-side code parsing:

你很可能已经使用过 Moment.jsdate-fnsLuxonnumeral.js。开发者多年来一直依赖这些库来格式化日期、数字和货币。这些都是非常有用的库,但它们也有代价:它们会增加下载大小(千字节),并且需要客户端代码解析:

Library Size
Moment.js 295 kB
date-fns 77 kB
luxon.js 82 kB
numeral.js 11 kB
大小
Moment.js 295 kB
date-fns 77 kB
luxon.js 82 kB
numeral.js 11 kB

The Intl API is baseline widely available (except for Intl.DurationFormat. It works in all current evergreen browsers, but hasn't been around long enough to qualify for "widely available".) and can handle almost all of your formatting requirements directly in the browser, with zero kB of download and no JS bundle parsing required. It also knows your users' locale preferences so you can format dates and numbers in a way that feels natural to them without any extra work.

Intl API 在基线广泛可用(除了 Intl.DurationFormat。它在所有当前的常青浏览器中工作,但存在时间还不够长,还不足以符合「广泛可用」的标准。)并且可以直接在浏览器中处理几乎所有您的格式化需求,下载零 kB 且无需 JS 包解析。它还知道用户们的区域设置偏好,因此您可以以对他们自然的方式格式化日期和数字,而无需额外工作。

Prefer video?

更喜欢视频?

This article is a written version of a talk by Kilian Valkhof, Polypane's creator. You can watch the video here:

这篇文章是 Kilian Valkhof(Polypane 的创建者)的一次演讲的书面版本。您可以在这里观看视频:

Interested in having this talk at your conference or meetup? Get in touch!

有兴趣在你的会议或聚会上做这个演讲吗?联系我!

Why you need an internationalization API

为什么您需要国际化 API

You might be thinking: "My site is only in one language. Why would I need an internationalization API?"

你可能在想:\"我的网站只有一种语言。为什么我需要国际化 API?\"

That's the most common reason developers skip over Intl. But Intl is less about translating your UI and more about formatting data correctly for your users.

这是开发者跳过 Intl 的最常见原因。但是 Intl 较少涉及翻译您的 UI,而更多是关于为您的用户正确格式化数据

Consider a date like 09/12/2025. If you're from the US, you'd read it as Sept...

开通本站会员,查看完整译文。

Accueil - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.1. UTC+08:00, 2026-04-18 20:04
浙ICP备14020137号-1 $Carte des visiteurs$