Types

InlineTranslationOptions

InlineTranslationOptions型のAPIリファレンス

概要

InlineTranslationOptions型は、インライン翻訳に変数を渡し、そのレンダリング動作を指定するために使用されます。 翻訳にコンテキストと識別子を追加することもできます。 これはuseGT()getGT()と組み合わせて使用し、インライン文字列翻訳に変数を渡します。

ビルドタイム翻訳: 変数はuseGT()getGT()では翻訳されず、元の文字列のみが翻訳されます。 動的コンテンツを含む文字列の翻訳についてはtx()を参照してください。

リファレンス

パラメータ

PropTypeDefault
variablesOptions??
Record<string, Intl.NumberFormatOptions | Intl.DateTimeFormatOptions>
undefined
variables??
Record<string, any>
undefined
id??
string
undefined
context??
string
undefined

説明

プロパティ説明
contextコンテンツのコンテキスト(翻訳に使用されます)。
id翻訳エディターで使用するオプションの識別子。
variablesキーが文字列内の各値のマッピング先を識別するオブジェクト。
variablesOptionsキーが変数を識別し、値が変数の動作を定義するオブジェクト。詳細については、Intl.NumberFormatOptions および Intl.DateTimeFormatOptions を参照してください。

コンテキスト

文字列にコンテキストを追加するには、context プロパティを使用します。

Component.tsx
import { useGT } from 'gt-next';

const Component = () => {
  const t = useGT();
  return <div>{t('Hello, world!', { context: 'a formal greeting' })}</div>;
};

変数の渡し方

文字列に変数を追加するには、{variable-name} 構文を使用します。ここで、波括弧が変数名を囲みます。

Component.tsx
import { useGT } from 'gt-next';

const Component = () => {
  const t = useGT();
  return <div>{t('Hello, {username}! How is your day?', { variables: { 'username' : 'Brian123' } })}</div>;
};

変数オプションの追加

variablesOptions プロパティを使用して、変数のレンダリング方法をカスタマイズすることもできます。

Component.tsx
import { useGT } from 'gt-next';

const Component = () => {
  const t = useGT();
  return <div>
    { t(
      'Your account balance: {account-balance}!',
      {
        variables: { "account-balance" : 1000000 },
        variableOptions: { "account-balance": { style: 'currency', currency: 'USD' } }
      }
    ) }
  </div>;
};

Notes

  • InlineTranslationOptionsは文字列翻訳に使用されます。
  • variablesオブジェクトはテキストに値を渡します。
  • variablesOptionsオブジェクトは変数の動作を定義します。

次のステップ

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