使用您最喜欢的AI助手总结文档,并引用此页面和AI提供商
此文档已过期,基础版本已在以下日期更新 2026年1月20日.
前往英文文档版本历史
- "添加了 vue 格式化器"v5.8.02025/8/20
- "添加了格式化器文档"v5.8.02025/8/18
- "添加列表格式化器文档"v5.8.02025/8/20
- "添加额外的 Intl 工具(DisplayNames、Collator、PluralRules)"v5.8.02025/8/20
- "添加语言环境工具(getLocaleName、getLocaleLang、getLocaleFromPath 等)"v5.8.02025/8/20
- "添加内容处理工具(getContent、getTranslation、getIntlayer 等)"v5.8.02025/8/20
此页面的内容已使用 AI 翻译。
查看英文原文的最新版本If you have an idea for improving this documentation, please feel free to contribute by submitting a pull request on GitHub.
GitHub link to the documentationCopy doc Markdown to clipboard
Intlayer 格式化工具
概述
Intlayer 提供了一组基于原生 Intl API 构建的轻量级辅助工具,以及一个缓存的 Intl 包装器,避免重复构建重量级的格式化器。这些工具完全支持本地化,可以直接从主 intlayer 包中使用。
缓存的 Intl
导出的 Intl 是对全局 Intl 的一个轻量级缓存包装器。它会缓存 NumberFormat、DateTimeFormat、RelativeTimeFormat、ListFormat、DisplayNames、Collator 和 PluralRules 的实例,从而避免重复构建相同的格式化器。
由于格式化器的构建相对昂贵,这种缓存机制在不改变行为的情况下提升了性能。该包装器暴露了与原生 Intl 相同的 API,因此用法完全一致。
- 缓存是按进程进行的,对调用者透明。
如果环境中不支持 Intl.DisplayNames,则只会打印一次仅限开发者的警告(建议使用 polyfill)。
示例:
复制代码到剪贴板
import { Intl } from "intlayer";// 数字格式化const numberFormat = new Intl.NumberFormat("en-GB", { style: "currency", currency: "GBP",});numberFormat.format(1234.5); // "£1,234.50"// 语言、地区等的显示名称const displayNames = new Intl.DisplayNames("fr", { type: "language" });displayNames.of("en"); // "anglais"// 用于排序的比较器const collator = new Intl.Collator("fr", { sensitivity: "base" });collator.compare("é", "e"); // 0(相等)// 复数规则const pluralRules = new Intl.PluralRules("fr");pluralRules.select(1); // "one"pluralRules.select(2); // "other"区域设置工具
getLocaleLang(locale?)
从语言环境字符串中提取语言代码:
复制代码到剪贴板
import { getLocaleLang } from "intlayer";getLocaleLang("en-US"); // "en"getLocaleLang("fr-CA"); // "fr"getLocaleLang("de"); // "de"- locale: 要提取语言代码的语言环境(默认为当前语言环境)
getLocaleFromPath(inputUrl)
从 URL 或路径名中提取语言环境段:
复制代码到剪贴板
import { getLocaleFromPath } from "intlayer";getLocaleFromPath("/en/dashboard"); // "en"getLocaleFromPath("/fr/dashboard"); // "fr"getLocaleFromPath("/dashboard"); // "en"(默认语言环境)getLocaleFromPath("https://example.com/es/about"); // "es"- inputUrl:要处理的完整 URL 字符串或路径名
- returns:检测到的语言环境,如果未找到语言环境则返回默认语言环境
getLocalizedUrl(url, currentLocale, locales?, defaultLocale?, prefixDefault?)
为当前语言生成本地化 URL:
复制代码到剪贴板
import { getLocalizedUrl } from "intlayer";getLocalizedUrl("/about", "fr", ["en", "fr"], "en", false); // "/fr/about"getLocalizedUrl("/about", "en", ["en", "fr"], "en", false); // "/about"getLocalizedUrl("https://example.com/about", "fr", ["en", "fr"], "en", true); // "https://example.com/fr/about"- url:要本地化的原始 URL
- currentLocale:当前语言
- locales:可选的支持语言数组(默认为配置的语言)
- defaultLocale:可选的默认语言环境(默认为配置的默认语言环境)
- prefixDefault:是否为默认语言环境添加前缀(默认为配置值)
getHTMLTextDir(locale?)
返回指定语言环境的文本方向:
复制代码到剪贴板
import { getHTMLTextDir } from "intlayer";getHTMLTextDir("en-US"); // "ltr"getHTMLTextDir("ar"); // "rtl"getHTMLTextDir("he"); // "rtl"- locale:要获取文本方向的语言环境(默认为当前语言环境)
- returns:返回
"ltr"、"rtl"或"auto"
内容处理工具
getContent(node, nodeProps, locale?)
使用所有可用插件(翻译、枚举、插入等)转换内容节点:
复制代码到剪贴板
import { getContent } from "intlayer";const content = getContent( contentNode, { dictionaryKey: "common", dictionaryPath: "/path/to/dict" }, "fr");- node:要转换的内容节点
- nodeProps:转换上下文的属性
- locale:可选的语言环境(默认为配置的默认语言环境)
getTranslation(languageContent, locale?, fallback?)
从语言内容对象中提取特定语言环境的内容:
复制代码到剪贴板
import { getTranslation } from "intlayer";const content = getTranslation( { en: "Hello", fr: "Bonjour", de: "Hallo", }, "fr", true); // "Bonjour"- languageContent:映射语言环境到内容的对象
- locale:目标语言环境(默认为配置的默认语言环境)
- fallback:是否回退到默认语言环境(默认为 true)
getIntlayer(dictionaryKey, locale?, plugins?)
通过键检索并转换字典中的内容:
复制代码到剪贴板
import { getIntlayer } from "intlayer";const content = getIntlayer("common", "fr");const nestedContent = getIntlayer("common", "fr", customPlugins);- dictionaryKey:要检索的字典键
- locale:可选的语言环境(默认为配置的默认语言环境)
- plugins:可选的自定义转换插件数组
getIntlayerAsync(dictionaryKey, locale?, plugins?)
异步从远程字典中检索内容:
复制代码到剪贴板
import { getIntlayerAsync } from "intlayer";const content = await getIntlayerAsync("common", "fr");- dictionaryKey:要检索的字典键
- locale:可选的语言环境(默认为配置的默认语言环境)
- plugins:可选的自定义转换插件数组
格式化工具
以下所有辅助函数均从 intlayer 导出。
percentage(value, options?)
将数字格式化为百分比字符串。
行为:大于 1 的值被解释为完整的百分比并进行归一化(例如,25 → 25%,0.25 → 25%)。
- value:
number | string - options:
Intl.NumberFormatOptions & { locale?: LocalesValues }
示例:
复制代码到剪贴板
import { percentage } from "intlayer";percentage(0.25); // "25%"percentage(25); // "25%"percentage(0.237, { minimumFractionDigits: 1 }); // "23.7%"格式化器函数
number(value, options?)
使用locale感知的分组和小数格式化数值。
- value:
number | string - options:
Intl.NumberFormatOptions & { locale?: LocalesValues }
复制代码到剪贴板
number(123456.789); // "123,456.789" (in en-US)number("1000000", { locale: "fr" }); // "1 000 000"number(1234.5, { minimumFractionDigits: 2 }); // "1,234.50"percentage(value, options?)
将数字格式化为百分比字符串。大于 1 的值会被规范化(例如,25 → 25%,0.25 → 25%)。
- value:
number | string - options:
Intl.NumberFormatOptions & { locale?: LocalesValues }
复制代码到剪贴板
percentage(0.25); // "25%"percentage(25); // "25%"percentage(0.237, { minimumFractionDigits: 1 }); // "23.7%"currency(value, options?)
将值格式化为本地化货币。默认为 USD。
- value:
number | string - options:
Intl.NumberFormatOptions & { locale?: LocalesValues }- 常见:
currency,currencyDisplay("symbol" | "code" | "name")
- 常见:
复制代码到剪贴板
currency(1234.5, { currency: "EUR" }); // "€1,234.50"currency("5000", { locale: "fr", currency: "CAD", currencyDisplay: "code" }); // "5 000,00 CAD"date(date, optionsOrPreset?)
格式化日期/时间值。
- date:
Date | string | number - optionsOrPreset:
Intl.DateTimeFormatOptions & { locale?: LocalesValues }或预设:"short" | "long" | "dateOnly" | "timeOnly" | "full"
复制代码到剪贴板
date(new Date(), "short"); // 例如 "08/02/25, 14:30"date("2025-08-02T14:30:00Z", { locale: "fr", month: "long", day: "numeric" }); // "2 août"relativeTime(from, to?, options?)
格式化两个时刻之间的相对时间。
- from:
Date | string | number - to:
Date | string | number(默认值为new Date()) - options:
{ locale?, unit?, numeric?, style? }
复制代码到剪贴板
const now = new Date();const in3Days = new Date(now.getTime() + 3 * 864e5);relativeTime(now, in3Days, { unit: "day" }); // "in 3 days"const twoHoursAgo = new Date(now.getTime() - 2 * 3600e3);relativeTime(now, twoHoursAgo, { unit: "hour", numeric: "auto" }); // "2 hours ago"units(value, options?)
使用单位格式化数值。
- value:
number | string - options:
Intl.NumberFormatOptions & { locale?: LocalesValues }- 常见:
unit(例如,"kilometer","byte"),unitDisplay("short" | "narrow" | "long")
- 常见:
复制代码到剪贴板
units(5, { unit: "kilometer", unitDisplay: "long", locale: "en-GB" }); // "5 kilometers"units(1024, { unit: "byte", unitDisplay: "narrow" }); // "1,024B"compact(value, options?)
使用紧凑记号法格式化数字。
- value:
number | string - options:
Intl.NumberFormatOptions & { locale?: LocalesValues }
复制代码到剪贴板
compact(1200); // "1.2K"compact("1000000", { locale: "fr", compactDisplay: "long" }); // "1 million"list(values, options?)
将数组格式化为本地化列表字符串。
- values:
(string | number)[] - options:
Intl.ListFormatOptions & { locale?: LocalesValues }- 常见:
type("conjunction" | "disjunction" | "unit"),style("long" | "short" | "narrow")
- 常见:
复制代码到剪贴板
list(["apple", "banana", "orange"]); // "apple, banana, and orange"list(["red", "green", "blue"], { locale: "fr", type: "disjunction" }); // "rouge, vert ou bleu"缓存的 Intl
从 intlayer 导出的 Intl 是围绕全局 Intl 的缓存包装器。它会缓存格式化程序实例(NumberFormat、DateTimeFormat 等),以避免重复构造它们,从而提高性能。
复制代码到剪贴板
import { Intl } from "intlayer";// 数字格式化const numberFormat = new Intl.NumberFormat("en-GB", { style: "currency", currency: "GBP",});numberFormat.format(1234.5); // "£1,234.50"// 语言、地区等的显示名称const displayNames = new Intl.DisplayNames("fr", { type: "language" });displayNames.of("en"); // "anglais"// 用于排序的排序规则const collator = new Intl.Collator("fr", { sensitivity: "base" });collator.compare("é", "e"); // 0 (相等)// 复数规则const pluralRules = new Intl.PluralRules("fr");pluralRules.select(1); // "one"pluralRules.select(2); // "other"额外的 Intl 功能
Intl.DisplayNames
用于获取本地化的语言、地区、货币和脚本名称:
复制代码到剪贴板
import { Intl } from "intlayer";const languageNames = new Intl.DisplayNames("en", { type: "language" });languageNames.of("fr"); // "French"const regionNames = new Intl.DisplayNames("fr", { type: "region" });regionNames.of("US"); // "États-Unis"Intl.Collator
用于区域感知的字符串比较和排序:
复制代码到剪贴板
import { Intl } from "intlayer";const collator = new Intl.Collator("de", { sensitivity: "base", numeric: true,});const words = ["äpfel", "zebra", "100", "20"];words.sort(collator.compare); // ["20", "100", "äpfel", "zebra"]Intl.PluralRules
用于在不同的 locales 中确定复数形式:
复制代码到剪贴板
import { Intl } from "intlayer";const pluralRules = new Intl.PluralRules("ar");pluralRules.select(0); // "zero"pluralRules.select(1); // "one"pluralRules.select(2); // "two"pluralRules.select(3); // "few"pluralRules.select(11); // "many"语言环境工具函数
date(date, optionsOrPreset?)
使用 Intl.DateTimeFormat 格式化日期/时间值。
- date:
Date | string | number - optionsOrPreset:
Intl.DateTimeFormatOptions & { locale?: LocalesValues }或者以下预设之一:- 预设值:
"short" | "long" | "dateOnly" | "timeOnly" | "full"
- 预设值:
示例:
复制代码到剪贴板
import { date } from "intlayer";date(new Date(), "short"); // 例如,"08/02/25, 14:30"date("2025-08-02T14:30:00Z", { locale: "fr", month: "long", day: "numeric" }); // "2 août"units(value, options?)
使用 Intl.NumberFormat 的 style: 'unit' 将数值格式化为本地化的单位字符串。
- value:
number | string - options:
Intl.NumberFormatOptions & { locale?: LocalesValues }- 常用字段:
unit(例如"kilometer","byte"),unitDisplay("short" | "narrow" | "long") - 默认值:
unit: 'day',unitDisplay: 'short',useGrouping: false
- 常用字段:
示例:
复制代码到剪贴板
import { units } from "intlayer";units(5, { unit: "kilometer", unitDisplay: "long", locale: "en-GB" }); // "5 kilometers"units(1024, { unit: "byte", unitDisplay: "narrow" }); // "1,024B"(依赖于区域设置)compact(value, options?)
使用紧凑表示法格式化数字(例如,1.2K,1M)。
- value:
number | string - options:
Intl.NumberFormatOptions & { locale?: LocalesValues }(内部使用notation: 'compact')
示例:
复制代码到剪贴板
import { compact } from "intlayer";compact(1200); // "1.2K"compact("1000000", { locale: "fr", compactDisplay: "long" }); // "1 million"list(values, options?)
使用 Intl.ListFormat 将值数组格式化为本地化的列表字符串。
- values:
(string | number)[] - options:
Intl.ListFormatOptions & { locale?: LocalesValues }- 常用字段:
type("conjunction" | "disjunction" | "unit"),style("long" | "short" | "narrow") - 默认值:
type: 'conjunction',style: 'long'
- 常用字段:
示例:
复制代码到剪贴板
import { list } from "intlayer";list(["apple", "banana", "orange"]); // "apple, banana, and orange"list(["red", "green", "blue"], { locale: "fr", type: "disjunction" }); // "rouge, vert ou bleu"list([1, 2, 3], { type: "unit" }); // "1, 2, 3"getPathWithoutLocale(inputUrl, locales?)
从 URL 中移除语言区域段:
复制代码到剪贴板
import { getPathWithoutLocale } from "intlayer";getPathWithoutLocale("/en/dashboard"); // "/dashboard"getPathWithoutLocale("/fr/dashboard"); // "/dashboard"getHTMLTextDir(locale?)
返回一个 locale 的文本方向:
复制代码到剪贴板
import { getHTMLTextDir } from "intlayer";getHTMLTextDir("en-US"); // "ltr"getHTMLTextDir("ar"); // "rtl"getHTMLTextDir("he"); // "rtl"内容处理工具
React
客户端组件:
复制代码到剪贴板
import { useNumber, useCurrency, useDate, usePercentage, useCompact, useList, useRelativeTime, useUnit,} from "react-intlayer/format";// 或在 Next.js 应用中import { useNumber, useCurrency, useDate, usePercentage, useCompact, useList, useRelativeTime, useUnit,} from "next-intlayer/client/format";const MyComponent = () => { const number = useNumber(); const currency = useCurrency(); const date = useDate(); const percentage = usePercentage(); const compact = useCompact(); const list = useList(); const relativeTime = useRelativeTime(); const unit = useUnit(); return ( <div> <p>{number(123456.789)}</p> <p>{currency(1234.5, { currency: "EUR" })}</p> <p>{date(new Date(), "short")}</p> <p>{percentage(0.25)}</p> <p>{compact(1200)}</p> <p>{list(["apple", "banana", "orange"])}</p> <p>{relativeTime(new Date(), new Date() + 1000)}</p> <p>{unit(123456.789, { unit: "kilometer" })}</p> </div> );};服务器组件(或 React 服务器运行时):
复制代码到剪贴板
import { useNumber, useCurrency, useDate, usePercentage, useCompact, useList, useRelativeTime, useUnit,} from "react-intlayer/server/format";// 或在 Next.js 应用中import { useNumber, useCurrency, useDate, usePercentage, useCompact, useList, useRelativeTime, useUnit,} from "next-intlayer/server/format";这些钩子将会从IntlayerProvider或IntlayerServerProvider中获取语言环境
getTranslation(languageContent, locale?, fallback?)
为特定语言环境提取内容:
复制代码到剪贴板
import { getTranslation } from "intlayer";const content = getTranslation( { zh: "你好", en: "Hello", fr: "Bonjour", de: "Hallo" }, "fr", true); // "Bonjour"Vue
客户端组件:
复制代码到剪贴板
import { useNumber, useCurrency, useDate, usePercentage, useCompact, useList, useRelativeTime, useUnit,} from "vue-intlayer/format";这些组合式函数将会使用注入的 IntlayerProvider 中的语言环境
注意事项
- 所有辅助函数接受
string输入;它们在内部被强制转换为数字或日期。 - 如果未提供 locale,则默认为你配置的
internationalization.defaultLocale。 - 这些实用程序是薄包装器;对于高级格式化,请传递标准
Intl选项。