# General Translation Platform: formatList
URL: https://generaltranslation.com/zh/docs/platform/core/reference/gt-class-methods/formatting/format-list.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 在 GT 实例上将数组格式化为符合区域设置的列表字符串。formatList 的 API 参考。

General Translation 使用内置的 `Intl.ListFormat` API，根据各个区域设置的规则，使用正确的分隔符以及连接 (“and”) 或析取 (“or”) 表达来连接各项。

## 概览 [#overview]

在 [`GT`](/docs/platform/core/reference/gt-class/constructor) 实例上调用 `formatList`，传入一个数组和可选的选项对象。它会返回一个普通字符串，并根据解析后的区域设置将各项连接起来。

```typescript
const gt = new GT({ targetLocale: 'en' });

const list = gt.formatList(['apple', 'banana', 'orange']);
// "apple, banana, and orange"
```

签名：

```typescript
formatList(
  array: Array<string | number>,
  options?: { locales?: string | string[] } & Intl.ListFormatOptions
): string
```

*注意：`formatList` 返回的是扁平的 `string`。如果要保留原始项的类型 (适用于混合类型或 JSX 数组) ，请改用 [`formatListToParts`](/docs/platform/core/reference/gt-class-methods/formatting/format-list-to-parts)。如果要在没有 `GT` 实例的情况下进行格式化，请参见独立的 [`formatList`](/docs/platform/core/reference/utility-functions/formatting/format-list)。*

## 工作原理 [#how-it-works]

* **基于 Intl。** 格式化由 [`Intl.ListFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat) 处理。
* **区域设置解析。** 默认情况下，该方法会按实例的目标区域设置进行格式化；如果无法使用，则依次回退到源区域设置，再回退到库默认值 (`en`) ——而不是 `locales` 配置数组。你可以在选项中传入 `locales` 进行覆盖。
* **列表类型。** `type: '连接'` 会生成用“和”连接的列表，`'析取'` 会生成用“或”连接的列表，`'unit'` 会生成单位样式的列表。

## 参数 [#parameters]

| 参数                    | 描述                                                    | 类型                        | 可选 | 默认值 |
| --------------------- | ----------------------------------------------------- | ------------------------- | -- | --- |
| [`array`](#array)     | 要格式化的项目列表。                                            | `Array<string \| number>` | 否  | —   |
| [`options`](#options) | 格式化配置：基于 `Intl.ListFormatOptions` 扩展，并支持覆盖 `locales`。 | `object`                  | 是  | —   |

### `array` [#array]

**类型** `Array<string | number>` · **必填**

要拼接为单个字符串的项目列表。

### `options` [#options]

**类型** `{ locales?: string | string[] } & Intl.ListFormatOptions` · **可选**

格式化配置。该表列出了已发布的 Core 类型中公开的常用选项及其在 Core 中实际生效的默认值。 (有关标准及特定运行时的补充详情，请参阅 [`Intl.ListFormat` 构造函数选项](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/ListFormat#options)) 。

| Name            | Description        | Type                                       | Optional | Default                                |
| --------------- | ------------------ | ------------------------------------------ | -------- | -------------------------------------- |
| `locales`       | 覆盖格式化时使用的 locales。 | `string \| string[]`                       | Yes      | `targetLocale` → `sourceLocale` → `en` |
| `localeMatcher` | 区域设置匹配算法。          | `'lookup' \| 'best fit'`                   | Yes      | `'best fit'`                           |
| `type`          | 列表格式化类型。           | `'conjunction' \| 'disjunction' \| 'unit'` | Yes      | `'conjunction'`                        |
| `style`         | 列表格式样式。            | `'long' \| 'short' \| 'narrow'`            | Yes      | `'long'`                               |

## 返回值 [#returns]

**类型** `string`

将各项合并为一个符合区域设置的列表字符串。

## 示例 [#examples]

```typescript
import { GT } from 'generaltranslation';

const gt = new GT({ targetLocale: 'en' });

// 连接（默认）— "and" 列表
console.log(gt.formatList(['A', 'B', 'C']));
// 输出："A, B, and C"

// 析取 — "or" 列表
console.log(gt.formatList(['A', 'B', 'C'], { type: 'disjunction' }));
// 输出："A, B, or C"

// 西班牙语析取
console.log(gt.formatList(['red', 'green', 'blue'], {
  locales: ['es'],
  type: 'disjunction',
}));
// 输出："red, green o blue"
```

## 说明 [#notes]

* 返回一个普通的 `string`；如果你需要保留非字符串的项类型，请使用 [`formatListToParts`](/docs/platform/core/reference/gt-class-methods/formatting/format-list-to-parts)。
* 该方法底层使用浏览器原生的 `Intl.ListFormat`。

## Sitemap

See the full [sitemap](https://generaltranslation.com/sitemap.md) for all pages.
