运行时需求
浏览器
next-intl 的源代码是针对 Next.js 支持的浏览器 编译的。
根据你使用的功能,需要确保目标浏览器支持以下 API:
- 基本使用:
Intl.Locale(兼容性) - 日期与时间格式化:
Intl.DateTimeFormat(兼容性) - 数字格式化:
Intl.NumberFormat(兼容性) - 复数规则:
Intl.PluralRules(兼容性) - 相对时间格式化:
Intl.RelativeTimeFormat(兼容性) - 列表格式化:
Intl.ListFormat(兼容性)
如果你的目标浏览器不支持所有必需的 API,建议使用 polyfill。
Cloudflare 提供了一个 polyfill 服务,你可以用来为指定语言环境加载所需的 polyfill。
示例:
IntlPolyfills.tsx
import {useLocale} from 'next-intl';
import Script from 'next/script';
function IntlPolyfills() {
const locale = useLocale();
const polyfills = [
'Intl',
'Intl.Locale',
'Intl.DateTimeFormat',
`Intl.DateTimeFormat.~locale.${locale}`,
`Intl.NumberFormat`,
`Intl.NumberFormat.~locale.${locale}`,
'Intl.PluralRules',
`Intl.PluralRules.~locale.${locale}`,
'Intl.RelativeTimeFormat',
`Intl.RelativeTimeFormat.~locale.${locale}`,
'Intl.ListFormat',
`Intl.ListFormat.~locale.${locale}`
];
return (
<Script
strategy="beforeInteractive"
src={
'https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?features=' +
polyfills.join(',')
}
/>
);
}⚠️
注意 polyfill 服务并不支持所有语言环境。你可以在 polyfill-service仓库
中找到可用 polyfill 的列表(例如搜索 Intl.DateTimeFormat.~locale.de-AT)。
Node.js
支持所有相关 Intl API 的最低版本是 Node.js 13。从该版本开始,所有必需的 API 都已可用。