# General Translation Platform: formatListToParts URL: https://generaltranslation.com/ja/docs/platform/core/reference/utility-functions/formatting/format-list-to-parts.mdx --- title: formatListToParts description: GT インスタンスなしで、リストをロケールに応じた部分にフォーマットします。formatListToParts の API リファレンス。 --- [`formatListToParts`](/docs/platform/core/reference/gt-class-methods/formatting/format-list-to-parts) は、General Translation のコアライブラリに含まれるスタンドアロンのユーティリティ関数で、配列をロケールに応じた部分にフォーマットします。各要素の元の型を保持したまま、項目の間に文字列の区切りを挿入し、`Array` を返します。 ## 概要 [#overview] `generaltranslation` から `formatListToParts` を直接インポートし、配列とオプションオブジェクトを指定して呼び出します。API Key や [GT](/docs/platform/core/reference/gt-class/constructor) インスタンスは必要ありません。インスタンスのロケールを継承するインスタンスベースのフォーマットを行う場合は、代わりに [`GT`](/docs/platform/core/reference/gt-class/constructor) インスタンスの [`formatListToParts`](/docs/platform/core/reference/gt-class-methods/formatting/format-list-to-parts) メソッドを使用してください。 単一の `string` を返す [`formatList`](/docs/platform/core/reference/utility-functions/formatting/format-list) とは異なり、この関数は元の項目をそのまま保持し、それらの間に文字列区切りを挿入するだけです。そのため、React などの UI フレームワークで型が混在する配列を描画する際に役立ちます。 ```typescript import { formatListToParts } from 'generaltranslation'; const parts = formatListToParts(['red', 'green', 'blue'], { locales: ['es'], type: 'disjunction', }); // 戻り値: ['red', ', ', 'green', ' o ', 'blue'] ``` シグネチャ: ```typescript formatListToParts( array: Array, options?: { locales?: string | string[] } & Intl.ListFormatOptions ): Array ``` ## 仕組み [#how-it-works] * **基盤となる API。** GT クラスメソッド と同じ [`Intl.ListFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat) を使用します。 * **型の保持。** 要素間に挿入されるのは文字列の区切りだけで、元の各項目はそれぞれの型を保つため、数値やオブジェクトは変更されずそのまま渡されます。 * **ロケールの決定。** `locales` を省略すると、library のデフォルトロケールである `en` にフォールバックします。 ## パラメーター [#parameters] | パラメーター | 説明 | 型 | 任意 | デフォルト | | --------------------- | ------------------- | ----------------------------------------------------------- | --- | ----- | | [`array`](#array) | 書式設定する項目の配列。 | `Array` | いいえ | — | | [`options`](#options) | 対象ロケールを含む書式設定オプション。 | `{ locales?: string \| string[] } & Intl.ListFormatOptions` | はい | `{}` | ### `array` [#array] **型** `Array` · **必須** フォーマットする項目の配列です。項目は任意の型に対応しており、string 以外の項目は変更せずそのまま返されます。 ### `options` [#options] **型** `{ locales?: string | string[] } & Intl.ListFormatOptions` · **任意** · **デフォルト** `{}` フォーマットの構成。表には、公開済みのコア型で提供される一般的なオプションと、その実効的なコアのデフォルト値を示します。補足的な標準およびランタイム固有の詳細については、[`Intl.ListFormat` コンストラクターオプション](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/ListFormat#options)を参照してください。 | プロパティ | 説明 | 型 | 任意 | デフォルト | | --------------- | -------------- | ------------------------------------------ | -- | --------------- | | `locales` | フォーマットに使用するロケール。 | `string \| string[]` | はい | `en` | | `localeMatcher` | ロケール照合アルゴリズム。 | `'lookup' \| 'best fit'` | はい | `'best fit'` | | `type` | リストのフォーマットの型。 | `'conjunction' \| 'disjunction' \| 'unit'` | はい | `'conjunction'` | | `style` | リストのフォーマットスタイル。 | `'long' \| 'short' \| 'narrow'` | はい | `'long'` | ## 戻り値 [#returns] **型** `Array` 元の項目の間にロケール固有の区切り文字列を挟んだ配列です。 ## 例 [#examples] ```typescript import { formatListToParts } from 'generaltranslation'; // 連言リスト(デフォルト) console.log(formatListToParts(['A', 'B', 'C'], { locales: 'en' })); // 出力: ['A', ', ', 'B', ', and ', 'C'] // 選言リスト console.log(formatListToParts(['A', 'B', 'C'], { locales: 'en', type: 'disjunction' })); // 出力: ['A', ', ', 'B', ', or ', 'C'] ``` ```typescript // 混合型配列:数値とオブジェクトはそのまま保持される console.log(formatListToParts(['apple', 42, { type: 'fruit' }], { locales: 'en' })); // 出力: ['apple', ', ', 42, ', and ', { type: 'fruit' }] ``` ```typescript // スペイン語の選言リスト console.log(formatListToParts(['red', 'green', 'blue'], { locales: 'es', type: 'disjunction', })); // Output: ['red', ', ', 'green', ' o ', 'blue'] // 短縮スタイル console.log(formatListToParts(['first', 'second'], { locales: 'en', style: 'short', })); // Output: ['first', ' & ', 'second'] ``` ## メモ [#notes] * 元の項目の型は保持され、要素間に区切り文字列が挿入されるだけです。 * これは、フラットな `string` を返す [`formatList`](/docs/platform/core/reference/utility-functions/formatting/format-list) との主な違いです。 * React のような UI フレームワークで、型が混在する配列をレンダリングする際に特に役立ちます。 * 内部的には、GT クラスメソッドと同じ `Intl.ListFormat` を使用しています。