Types

RuntimeTranslationOptions

RuntimeTranslationOptions 型のAPIリファレンス

概要

RuntimeTranslationOptions 型は、インライン翻訳に variables を渡し、そのレンダー動作を指定するために使用します。 翻訳の言語を切り替えるために locale を追加することもできます。 これは tx 関数と併せて使用します。

ランタイム翻訳: 変数をオンデマンドで翻訳するには、tx に渡す文字列内に直接含めてください。 options オブジェクト経由で tx に渡された variables は翻訳されません。

リファレンス

パラメータ

Prop

Type

説明

Prop説明
variables各 value が文字列内のどこにマッピングされるかをキーで示すオブジェクト。
$locale翻訳の locale を指定するために、任意で variables オブジェクト内に Variable として $locale を含められます。デフォルトは、アプリでサポートされている中でブラウザーの最優先の locale になります。

サンプル

変数の渡し方

文字列に変数を挿入するには、{variable-name} 構文を使用します。これは、波かっこで変数の名称を囲む書式です。

Component.tsx
import { tx } from 'gt-next/server';

const Component = () => {
  return <div>
    {tx(`{username}さん、こんにちは!`,{ username: 'Brian123' })}
  </div>;
};

ICU メッセージ形式の使用

gt-next は ICU メッセージ形式をサポートしており、variables の書式設定も可能です。

Component.tsx
import { tx } from 'gt-next/server';

const Component = () => {
  return <div>
    { tx(
      'あなたの口座残高:{dollars, number, ::currency/USD}',
      {
        "dollars" : 1000000,
      }
    ) }
  </div>;
};

variables を翻訳する

variables を翻訳するには、tx に渡す文字列内に直接埋め込んでください。

Component.tsx
import { tx } from 'gt-next/server';

const Component = ({ hairColor }: { hairColor: string }) => {
  return <div>{tx(
    `こんにちは、{username}さん!あなたの髪色は${hairColor}です。`,
    { username: 'Brian123' }
  )}</div>;
};

変数 hairColor は翻訳されますが、username は翻訳されません。

locale を指定する

翻訳に使用する locale を指定できます。

Component.tsx
import { tx } from 'gt-next/server';

const Component = () => {
  return <div>{tx('こんにちは、世界!', { $locale: 'fr' })}</div>;
};

これは常にフランス語に翻訳されます。


注意

  • RuntimeTranslationOptions は、実行時に文字列を翻訳するために使用します。
  • variables オブジェクトは、テキストに値を渡します。
  • variablesOptions オブジェクトは、variables の挙動を定義します。

次のステップ

  • インライン文字列の翻訳については、txを参照してください。
  • フォーマットの options の詳細については、ICU message formatを参照してください。

このガイドはいかがですか?