# generaltranslation: General Translation Core SDK: formatListToParts URL: https://generaltranslation.com/ja/docs/core/class/methods/formatting/format-list-to-parts.mdx --- title: formatListToParts description: 元の型を保持したまま、リストを各部分に分けて整形する formatListToParts メソッドの API リファレンス --- ## 概要 `formatListToParts` メソッドは、配列をロケールに応じたパーツに整形し、各要素の元の型を保持したまま、項目の間に文字列の区切りを挿入します。 フラットな文字列を返す `formatList` とは異なり、このメソッドは `Array` を返すため、文字列、数値、オブジェクト、JSX 要素が混在する配列に適しています。 ```typescript const gt = new GT({ locales: ['en'] }); const parts = gt.formatListToParts(['apple', 42, { type: 'fruit' }]); // 戻り値: ['apple', ', ', 42, ', and ', { type: 'fruit' }] ``` ## リファレンス ### パラメータ | 名前 | 型 | 説明 | | ---------- | ------------------------ | ------------ | | `array` | `Array` | 書式設定する項目の配列 | | `options?` | `ListFormatPartsOptions` | 任意の書式設定オプション | ### ListFormatPartsOptions 追加のロケール指定をサポートする `Intl.ListFormatOptions` の拡張です: | 名前 | 型 | 説明 | | ---------- | ------------------------------------------ | ------------------------------------------- | | `locales?` | `string \| string[]` | フォーマットに使用するロケールを上書きします (デフォルトはインスタンスのロケール) | | `type?` | `'conjunction' \| 'disjunction' \| 'unit'` | リストのフォーマット種別 (デフォルト: `'conjunction'`) | | `style?` | `'long' \| 'short' \| 'narrow'` | リストのフォーマットスタイル (デフォルト: `'long'`) | ### 戻り値 `Array` - 元の項目の間にロケール固有の区切り文字列が挿入された配列です。 *** ## 例 ### 基本的なリストの書式設定 ```typescript copy import { GT } from 'generaltranslation'; const gt = new GT({ locales: ['en'] }); // 連言(デフォルト)— "and" リスト console.log(gt.formatListToParts(['A', 'B', 'C'])); // Output: ['A', ', ', 'B', ', and ', 'C'] // 選言 — "or" リスト console.log(gt.formatListToParts(['A', 'B', 'C'], { type: 'disjunction' })); // Output: ['A', ', ', 'B', ', or ', 'C'] ``` ### 複数の型が混在する配列 ```typescript copy // 数値とオブジェクトは保持される console.log(gt.formatListToParts(['apple', 42, { type: 'fruit' }])); // 出力: ['apple', ', ', 42, ', and ', { type: 'fruit' }] ``` ### スタイルの違い ```typescript copy // 短いスタイル console.log(gt.formatListToParts(['first', 'second'], { style: 'short' })); // 出力: ['first', ' & ', 'second'] // 単位スタイル console.log(gt.formatListToParts(['1km', '2mi'], { type: 'unit' })); // 出力: ['1km', ', ', '2mi'] ``` ### ロケールごとの書式設定 ```typescript copy // スペイン語の選言リスト console.log(gt.formatListToParts(['red', 'green', 'blue'], { locales: ['es'], type: 'disjunction' })); // 出力: ['red', ', ', 'green', ' o ', 'blue'] ``` *** ## 注記 * 元の項目の型は保持され、要素間には区切り用の文字列だけが挿入されます * これは、フラットな`string`を返す`formatList`との主な違いです * ReactのようなUIフレームワークで、型が混在する配列をレンダリングする際に特に便利です * このメソッドは内部でブラウザネイティブの`Intl.ListFormat`を使用します * `type: 'conjunction'`は「and」形式のリスト、`'disjunction'`は「or」形式のリスト、`'unit'`は単位形式のリストを生成します ## 次のステップ * その他のオプションについては、[`Intl.ListFormat` のドキュメント](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat)を参照してください * 数値の書式設定については、[`format-num`](/docs/core/class/methods/formatting/format-num)を参照してください * GT インスタンスを使わずに使用する場合は、スタンドアロンの [`format-list-to-parts`](/docs/core/functions/formatting/format-list-to-parts)を参照してください * リスト補間を使ったメッセージの書式設定については、[`format-message`](/docs/core/class/methods/formatting/format-message)を参照してください