# generaltranslation: General Translation Core SDK: formatMessage URL: https://generaltranslation.com/ja/docs/core/class/methods/formatting/format-message.mdx --- title: formatMessage description: GT の formatMessage メソッドの API リファレンス --- ## 概要 `formatMessage` メソッドは、変数の置換とロケールに応じた書式設定を行いながら、メッセージをフォーマットします。 Format.JS の [`intl-messageformat`](https://formatjs.github.io/docs/intl-messageformat/) ライブラリをベースにしており、ICU メッセージフォーマットのパターンをサポートしています。 このメソッドは、変数の補間や複数形処理に不可欠です。 また、数値や日付の書式設定などの機能にも対応しています。 ```typescript const gt = new GT({ sourceLocale: 'en', targetLocale: 'fr' }); const formatted = gt.formatMessage('Hello {name}, you have {count} messages', { variables: { name: 'Alice', count: 5 } }); // 戻り値: "Hello Alice, you have 5 messages" ``` *** ## リファレンス ### パラメータ ### オプションオブジェクト | プロパティ | 型 | 任意 | 説明 | | ------------ | -------------------- | -- | ---------------------------------------------------- | | `locales` | `string \| string[]` | ✓ | 書式設定に使用するロケール (インスタンスのデフォルトを上書きします) | | `variables` | `FormatVariables` | ✓ | メッセージ補間に使用する変数を含むオブジェクト | | `dataFormat` | `StringFormat` | ✓ | メッセージ文字列のデータ形式 (例: `'ICU'`, `'I18NEXT'`, `'STRING'`) | ### FormatVariables 型 ```typescript type FormatVariables = Record; ``` ### 戻り値 `string` - 変数が置換され、ロケール固有の書式が適用された整形されたメッセージ。 *** ## 挙動 ### 変数の置換 * 単純な変数: `{variableName}` → 文字列の値に置き換えられます * ICU パターン: `{count, plural, ...}` → ICU の書式ルールに従って処理されます * 変数が見つからない場合: エラーになります * 二重中かっこ: 中かっこの特別な意味をエスケープし、1 つの中かっことして表示されます ### メッセージフォーマットのサポート * **シンプルな補間**: `{variable}` * **数値の書式設定**: `{price, number, ::currency/USD}`, `{discount, number, percent}`, `{num, number, integer}` * **日付の書式設定**: `{date, date, short}`, `{time, time, short}` * **複数形処理**: `{count, plural, =0 {none} =1 {one} other {many}}` * **選択**: `{gender, select, male {he} female {she} other {they}}` * **序数の選択**: `{place, selectordinal, =1 {#st} =2 {#nd} =3 {#rd} other {#th}}` *** ## 例 ### 基本的な変数の置換 ```typescript const gt = new GT({ targetLocale: 'en' }); const message = gt.formatMessage('Welcome {name}!', { variables: { name: 'John' } }); console.log(message); // "Welcome John!" ``` ### ICU 形式の複数形処理 ```typescript const message = gt.formatMessage( 'You have {count, plural, =0 {no items} =1 {one item} other {# items}} in your cart', { variables: { count: 3 } } ); console.log(message); // "You have 3 items in your cart"(カートに3つのアイテムがあります) ``` ### 数値と通貨の書式設定 ```typescript const gt = new GT({ targetLocale: 'en' }); const message = gt.formatMessage( 'Your total is {price, number, ::currency/USD} with {discount, number, percent} off', { variables: { price: 99.99, discount: 0.15 } } ); console.log(message); // "合計は$99.99で、15%割引が適用されます" ``` ### 複雑なメッセージテンプレート ```typescript const orderStatusMessage = gt.formatMessage(` Order #{orderId} status update: - Items: {itemCount, plural, =0 {no items} =1 {one item} other {# items}} - Total: {total, number, ::currency/USD} - Status: {status, select, pending {Pending} shipped {Shipped} delivered {Delivered} other {Unknown}} - Delivery: {deliveryDate, date, short} `, { variables: { orderId: 'ORD-12345', itemCount: 3, total: 149.97, status: 'shipped', deliveryDate: new Date('2024-03-20') } }); ``` *** ## 注意事項 * このメソッドは、高度な書式設定のために、Format.JS の [`Intl.MessageFormat`](https://formatjs.github.io/docs/intl-messageformat/) を使用して ICU メッセージフォーマット構文を処理します。 * 必要な変数が不足している場合は、エラーがスローされます。 * ロケール固有の数値、日付、通貨の書式設定が自動的に適用されます ## 次のステップ * その他のオプションについては、[`Intl.MessageFormat` のドキュメント](https://formatjs.github.io/docs/intl-messageformat/)を参照してください * 数値の書式設定には [formatNum](/docs/core/class/methods/formatting/format-num) を使用します