NodeJs-Internationalization

 后端   小卒子   2024-09-04 21:34   213
  nodejs

Node.js 的国际化(Internationalization,简称 i18n)模块允许应用程序在多语言环境中正确处理文本、日期、时间和货币格式。Node.js 提供了 Intl 对象,这是 ECMAScript 国际化 API 的一部分,主要通过各种构造函数和方法提供国际化支持。

以下是关于 Node.js 国际化(Intl)的详细介绍,包括它的属性、方法以及使用示例。

1. Intl 对象

Intl 是 JavaScript 的全局对象之一,提供了多种构造函数和方法来支持不同的国际化需求,如格式化日期、时间、数字和货币,以及处理字符串的比较。

1.1 Intl 构造函数

  • Intl.Collator: 用于比较字符串。
  • Intl.DateTimeFormat: 用于格式化日期和时间。
  • Intl.NumberFormat: 用于格式化数字(包括货币和百分比)。
  • Intl.PluralRules: 用于确定复数规则。
  • Intl.RelativeTimeFormat: 用于格式化相对时间。
  • Intl.ListFormat: 用于格式化列表。
  • Intl.DisplayNames: 用于显示各种语言、区域、脚本和货币名称。

1.2 Intl 方法

  • Intl.getCanonicalLocales(locales): 返回语言环境的规范化名称数组。
const canonicalLocales = Intl.getCanonicalLocales('EN-us');
console.log(canonicalLocales); // ["en-US"]

2. Intl.Collator

Intl.Collator 用于字符串比较,例如在不同语言环境下对字符串进行排序。

2.1 构造函数

  • new Intl.Collator([locales[, options]]): 创建一个新的字符串比较对象。
    • locales: 可选,表示一个或多个语言环境的字符串或数组。
    • options: 可选,包含影响比较行为的配置。
      • usage: sortsearch,默认值为 sort
      • sensitivity: base, accent, casevariant
      • ignorePunctuation: 是否忽略标点符号。

2.2 方法

  • collator.compare(string1, string2): 比较两个字符串,返回一个数字,指示字符串的排序顺序。
const collator = new Intl.Collator('en', { sensitivity: 'base' });
console.log(collator.compare('a', 'A')); // 0,表示相等
  • collator.resolvedOptions(): 返回一个对象,表示实例的配置选项。
console.log(collator.resolvedOptions()); 
// {locale: "en", usage: "sort", sensitivity: "base", ignorePunctuation: false, ...}

3. Intl.DateTimeFormat

Intl.DateTimeFormat 用于格式化日期和时间,根据语言环境以不同格式展示日期和时间信息。

3.1 构造函数

  • new Intl.DateTimeFormat([locales[, options]]): 创建一个日期时间格式化对象。
    • locales: 可选,表示一个或多个语言环境的字符串或数组。
    • options: 可选,包含影响日期和时间格式的配置。
      • weekday, era, year, month, day, hour, minute, second: 用于指定时间的各个部分应该如何显示。
      • timeZone: 用于指定时区(如 'UTC')。

3.2 方法

  • dateTimeFormat.format(date): 格式化指定的日期对象。
const dateFormat = new Intl.DateTimeFormat('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
console.log(dateFormat.format(new Date())); // September 4, 2024
  • dateTimeFormat.formatToParts(date): 将日期对象格式化为不同部分的数组。
console.log(dateFormat.formatToParts(new Date())); 
// [{type: "month", value: "September"}, {type: "literal", value: " "}, ...]
  • dateTimeFormat.resolvedOptions(): 返回一个对象,表示实例的配置选项。
console.log(dateFormat.resolvedOptions()); 
// {locale: "en-US", calendar: "gregory", numberingSystem: "latn", ...}

4. Intl.NumberFormat

Intl.NumberFormat 用于根据语言环境格式化数字,支持货币、百分比等格式。

4.1 构造函数

  • new Intl.NumberFormat([locales[, options]]): 创建一个数字格式化对象。
    • locales: 可选,表示一个或多个语言环境的字符串或数组。
    • options: 可选,包含影响数字格式的配置。
      • style: decimal, currency, percent,指定格式类型。
      • currency: 指定货币代码(如 'USD')。
      • minimumFractionDigits, maximumFractionDigits: 小数位数。

4.2 方法

  • numberFormat.format(number): 格式化数字。
const currencyFormat = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' });
console.log(currencyFormat.format(1234.56)); // $1,234.56
  • numberFormat.formatToParts(number): 将数字格式化为不同部分的数组。
console.log(currencyFormat.formatToParts(1234.56));
// [{type: "currency", value: "$"}, {type: "integer", value: "1"}, ...]
  • numberFormat.resolvedOptions(): 返回一个对象,表示实例的配置选项。
console.log(currencyFormat.resolvedOptions());
// {locale: "en-US", numberingSystem: "latn", style: "currency", currency: "USD", ...}

5. Intl.PluralRules

Intl.PluralRules 用于根据语言环境的复数规则,确定数值的复数形式。

5.1 构造函数

  • new Intl.PluralRules([locales[, options]]): 创建一个复数规则对象。
    • locales: 可选,表示一个或多个语言环境的字符串或数组。
    • options: 可选,影响复数规则的配置。
      • type: cardinalordinal,表示基数或序数。

5.2 方法

  • pluralRules.select(number): 返回给定数字的复数形式。
const pluralRules = new Intl.PluralRules('en-US');
console.log(pluralRules.select(1)); // "one"
console.log(pluralRules.select(2)); // "other"
  • pluralRules.resolvedOptions(): 返回一个对象,表示实例的配置选项。
console.log(pluralRules.resolvedOptions());
// {locale: "en-US", type: "cardinal", minimumIntegerDigits: 1, ...}

6. Intl.RelativeTimeFormat

Intl.RelativeTimeFormat 用于格式化相对时间,如“2 天前”或“明天”。

6.1 构造函数

  • new Intl.RelativeTimeFormat([locales[, options]]): 创建一个相对时间格式化对象。
    • locales: 可选,表示一个或多个语言环境的字符串或数组。
    • options: 可选,包含影响相对时间格式的配置。
      • numeric: alwaysauto,指示是否总是使用数字。
      • style: long, short, narrow

6.2 方法

  • relativeTimeFormat.format(value, unit): 格式化相对时间。
const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
console.log(rtf.format(-1, 'day')); // "yesterday"
console.log(rtf.format(1, 'day')); // "tomorrow"
  • relativeTimeFormat.formatToParts(value, unit): 将相对时间格式化为不同部分的数组。
console.log(rtf.formatToParts(-1, 'day'));
// [{type: "literal", value: "yesterday"}]
  • relativeTimeFormat.resolvedOptions(): 返回一个对象,表示实例的配置选项。
console.log(rtf.resolvedOptions());
// {locale: "en", style: "long", numeric: "auto", ...}

7. Intl.ListFormat

Intl.ListFormat 用于格式化项目列表,例如将数组转换为“苹果、香蕉和橙子”这样的字符串。

7.1 构造函数

  • new Intl.ListFormat([locales[, options]]): 创建一个列表格式化对象。
    • locales: 可选

,表示一个或多个语言环境的字符串或数组。

  • options: 可选,影响列表格式的配置。
    • type: conjunction(和), disjunction(或), unit(单位)。
    • style: long, short, narrow

7.2 方法

  • listFormat.format(list): 格式化列表。
const listFormat = new Intl.ListFormat('en', { style: 'short', type: 'conjunction' });
console.log(listFormat.format(['Apple', 'Banana', 'Orange'])); // "Apple, Banana, and Orange"
  • listFormat.formatToParts(list): 将列表格式化为不同部分的数组。
console.log(listFormat.formatToParts(['Apple', 'Banana', 'Orange']));
// [{type: "element", value: "Apple"}, {type: "literal", value: ", "}, ...]
  • listFormat.resolvedOptions(): 返回一个对象,表示实例的配置选项。
console.log(listFormat.resolvedOptions());
// {locale: "en", style: "short", type: "conjunction", ...}

8. Intl.DisplayNames

Intl.DisplayNames 用于显示语言、区域、脚本和货币的名称。

8.1 构造函数

  • new Intl.DisplayNames(locales, options): 创建一个显示名称对象。
    • locales: 一个或多个语言环境的字符串或数组。
    • options: 配置显示名称的选项。
      • type: language, region, script, currency
      • style: long, short, narrow
      • fallback: code, none

8.2 方法

  • displayNames.of(code): 获取给定代码的显示名称。
const displayNames = new Intl.DisplayNames(['en'], { type: 'currency' });
console.log(displayNames.of('USD')); // "US Dollar"
  • displayNames.resolvedOptions(): 返回一个对象,表示实例的配置选项。
console.log(displayNames.resolvedOptions());
// {locale: "en", style: "long", type: "currency", ...}

9. 总结

Node.js 的国际化支持通过 Intl 对象及其构造函数和方法提供了强大的工具,帮助开发者在全球化应用中处理不同语言环境下的日期、时间、数字、货币和文本格式。利用这些工具,开发者可以确保应用程序在多语言环境中的一致性和正确性,从而更好地服务于全球用户。