# generaltranslation: General Translation Core SDK: formatMessage URL: https://generaltranslation.com/ja/docs/core/functions/formatting/format-message.mdx --- title: formatMessage description: スタンドアロンの formatMessage 関数の API リファレンス --- ## 概要 `formatMessage` メソッドは、変数の埋め込みとロケールに応じた書式設定を行い、メッセージを整形します。 Format.JS の [`intl-messageformat`](https://formatjs.github.io/docs/intl-messageformat/) ライブラリをベースにしており、ICU メッセージフォーマットのパターンに対応しています。 このメソッドは、変数補間や複数形の処理に不可欠です。 また、数値や日付の書式設定など、さまざまな機能もサポートしています。 ```typescript import { formatMessage } from 'generaltranslation'; const formatted = formatMessage('Hello {name}, you have {count} messages', { locales: ['en'], variables: { name: 'Alice', count: 5 } }); // 戻り値: "Hello Alice, you have 5 messages" ``` *** ## リファレンス ### パラメーター ### オプションオブジェクト | プロパティ | 型 | 任意 | デフォルト | 説明 | | ----------- | -------------------- | -- | -------- | ------------------ | | `locales` | `string \| string[]` | ✓ | `['en']` | フォーマットに使用するロケール | | `variables` | `FormatVariables` | ✓ | `{}` | 補間に使用する変数を含むオブジェクト | ### 戻り値 `string` - 変数が埋め込まれ、ロケール固有の書式が適用された、フォーマット済みのメッセージ。 *** ## 挙動 ### ロケール処理 * フォーマットには、指定された`locales`パラメータを使用します * ロケールが指定されていない場合は、`['en']`にフォールバックします * 配列によるロケールのフォールバックチェーンに対応しています ### 変数の処理 * 単純な置換: `{variable}` → 値に置換 * ICU メッセージフォーマット: 複数形、select、書式設定を完全にサポート * 変数が不足している場合: 出力内で変更されずそのまま残る ### メッセージフォーマットのサポート GT クラスのメソッドと同じ ICU メッセージフォーマット機能: * 数値の書式設定: `{price, number, currency}` * 日付の書式設定: `{date, date, short}` * 複数形処理: `{count, plural, ...}` * 選択: `{gender, select, ...}` *** ## 例 ### 基本的な使い方 ```typescript import { formatMessage } from 'generaltranslation'; const greeting = formatMessage('Hello {name}!', { locales: ['en'], variables: { name: 'World' } }); console.log(greeting); // "Hello World!" ``` ### GT インスタンスを使わない場合 ```typescript // クラスのインスタンス化なしでフォーマットするユーティリティ関数 function quickFormat(template: string, variables: any, locale = 'en') { return formatMessage(template, { locales: [locale], variables }); } const notification = quickFormat( 'You have {count, plural, =0 {no messages} =1 {one message} other {# messages}}', { count: 3 }, 'en' ); console.log(notification); // "You have 3 messages" ``` ### 通貨と数値の書式設定 ```typescript // ドイツ語ロケールのフォーマット const germanPrice = formatMessage('Preis: {price, number, currency}', { locales: ['de'], variables: { price: 1234.56 } }); console.log(germanPrice); // "Preis: 1.234,56 €" // パーセント表示のフォーマット const progress = formatMessage('Progress: {percent, number, percent}', { locales: ['en'], variables: { percent: 0.85 } }); console.log(progress); // "Progress: 85%" ``` ### テンプレートライブラリ ```typescript import { formatMessage } from 'generaltranslation'; class MessageTemplates { private locale: string; constructor(locale: string = 'en') { this.locale = locale; } welcome(name: string) { return formatMessage('Welcome back, {name}!', { locales: [this.locale], variables: { name } }); } itemCount(count: number) { return formatMessage( '{count, plural, =0 {No items} =1 {One item} other {# items}}', { locales: [this.locale], variables: { count } } ); } } const templates = new MessageTemplates('fr'); console.log(templates.welcome('Marie')); // "Welcome back, Marie!" (フランス語) console.log(templates.itemCount(5)); // "5 éléments" ``` *** ## 注意事項 * オプションでロケールを明示的に指定する必要があります * GT クラスのメソッドと同じ ICU メッセージフォーマットの機能をサポートします * 入力テンプレートが空の場合は空文字列を返します * 変数は ICU の書式ルールが適用される前に処理されます ## 次のステップ * さらに詳しいオプションについては、[`Intl.MessageFormat` のドキュメント](https://formatjs.github.io/docs/intl-messageformat/)を参照してください * 状態を保持するフォーマットには、GT クラスの [`formatMessage`](/docs/core/class/methods/formatting/format-message) を使用します * スタンドアロンの [`formatNum`](/docs/core/functions/formatting/format-num) で数値をフォーマットします * `FormatVariables` 型について確認します