# generaltranslation: General Translation Core SDK: formatListToParts URL: https://generaltranslation.com/ja/docs/core/functions/formatting/format-list-to-parts.mdx --- title: formatListToParts description: 元の型を保持したまま、リストを各部分にフォーマットするスタンドアロン関数 --- ## 概要 スタンドアロンの `formatListToParts` 関数は、GT インスタンスを作成しなくても、配列をロケールに応じた各部分にフォーマットします。 各要素の元の型を保持したまま、項目の間に区切り文字列を挿入し、`Array` を返します。 ```typescript import { formatListToParts } from 'generaltranslation'; const parts = formatListToParts(['red', 'green', 'blue'], { locales: ['es'], type: 'disjunction' }); // 戻り値: ['red', ', ', 'green', ' o ', 'blue'] ``` ## リファレンス ### パラメータ | 名前 | 型 | 説明 | | --------- | ---------------------------------------------------------- | -------------------------- | | `array` | `Array` | フォーマットする項目の配列 | | `options` | `ListFormatPartsOptions & { locales: string \| string[] }` | 必須の `locales` を含むフォーマットオプション | ### ListFormatPartsOptions | 名前 | 型 | 説明 | | --------- | ------------------------------------------ | -------------------------------------- | | `locales` | `string \| string[]` | **必須** - フォーマットに使用するロケール | | `type?` | `'conjunction' \| 'disjunction' \| 'unit'` | リストのフォーマット種別 (デフォルト: `'conjunction'`) | | `style?` | `'long' \| 'short' \| 'narrow'` | リストのフォーマットスタイル (デフォルト: `'long'`) | ### 戻り値 `Array` - 元の項目の間にロケール固有の区切り文字列が挟まれた配列です。 *** ## 例 ### 基本的な使い方 ```typescript copy 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 copy // 数値とオブジェクトは保持される console.log(formatListToParts(['apple', 42, { type: 'fruit' }], { locales: 'en' })); // 出力: ['apple', ', ', 42, ', and ', { type: 'fruit' }] ``` ### ロケールに応じたフォーマット ```typescript copy // スペイン語の選言リスト console.log(formatListToParts(['red', 'green', 'blue'], { locales: 'es', type: 'disjunction' })); // 出力: ['red', ', ', 'green', ' o ', 'blue'] // 短縮スタイル console.log(formatListToParts(['first', 'second'], { locales: 'en', style: 'short' })); // 出力: ['first', ' & ', 'second'] ``` *** ## 注意事項 * GT クラスメソッドとは異なり、`locales` パラメータは必須です * 元の項目の型は保持され、要素間には区切り文字列のみが挿入されます * これは、フラットな `string` を返す `formatList` との主な違いです * React のような UI フレームワークで、型が混在する配列をレンダリングする場合に特に便利です * GT クラスメソッドと同じ `Intl.ListFormat` を内部的に使用します ## 次のステップ * その他のオプションについては、[`Intl.ListFormat` のドキュメント](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat)を参照してください * スタンドアロンの数値フォーマットについては [`formatNum`](/docs/core/functions/formatting/format-num) を参照してください * スタンドアロンの日付フォーマットについては [`formatDateTime`](/docs/core/functions/formatting/format-date-time) を参照してください * インスタンスベースの使用方法については、GT クラスの [`formatListToParts`](/docs/core/class/methods/formatting/format-list-to-parts) を参照してください