Paraglide JS

バリアントを使うと、複数形処理、性別による出し分け、A/Bテストなどを実現できます。バリアントは inlang の強力な機能で、条件に応じて同じメッセージの異なるバージョンを作成できます。

[!TIP]ロケールに応じた数値・日付のフォーマット(number、datetime)が必要な場合は、フォーマットを参照してください。

マッチング

以下のメッセージは、次の条件に一致します。

プラットフォームユーザーの性別メッセージ
androidmale{username} は Google Play Store から自分のスマートフォンにアプリをダウンロードする必要があります。
iosfemale{username} は App Store から自分の iPhone にアプリをダウンロードする必要があります。
**アプリをダウンロードする必要があります。

[!NOTE]以下の例では、説明のために Inlang Message Format プラグインを使用しています。構文は使用するプラグインによって異なる場合があります。

{
  "jojo_mountain_day": [{
    "match": {
      "platform=android, userGender=male": "{username} has to download the app on his phone from the Google Play Store.",
      "platform=ios, userGender=female": "{username} has to download the app on her iPhone from the App Store.",
      "platform=*, userGender=*": "The person has to download the app."
    }
  }]
}

バリアントの優先順位

複数のバリアントが一致する場合、Paraglide は MessageFormat 2 と同じ方法で 1 つを選びます。セレクターは宣言された順に比較され、各セレクターでは、キャッチオール * のバリアントよりもリテラルキーのバリアントが優先されます。バリアントの保存順序は関係ありません。

たとえば、セレクターが platform, userGender、バリアントが platform=*, userGender=male、platform=android, userGender=*、platform=*, userGender=* の場合、入力 platform=android, userGender=male に対しては platform=android, userGender=* が選ばれます。このバリアントでは、最初のセレクターである platform がリテラルで一致しているためです。

複数形処理

メッセージ内で変数を定義し、その変数をセレクターで使えます。Paraglide は内部で Intl.PluralRules を使って複数形を判定します。

入力条件メッセージ
count=1countPlural=one猫が1匹います。
count>1countPlural=other猫がたくさんいます。

[!TIP]local countPlural = count: plural という構文は、「plural(count) と等しいローカル変数 countPlural を作成する」という意味です。

{
"some_happy_cat": [{
    "declarations": ["input count", "local countPlural = count: plural"],
    "selectors": ["countPlural"],
    "match": {
      "countPlural=one": "There is one cat.",
      "countPlural=other": "There are many cats.",
    },
  }]
}

正確な数値 (=0、=1、…)

複数形セレクターが返すのはカテゴリ (zero、one、two、few、many、other) で、数値は返しません。そのため、countPlural セレクター自体に 0 のようなキーを指定しても、一致することはありません。正確な数値に一致させるには、関数を使わずに入力をエイリアスする2つ目のセレクターを追加し、複数形セレクターの前に置きます。こうすると、0 も含むフランス語の one のようなカテゴリよりも、正確な数値の一致条件が優先されます。

{
"some_happy_cat": [{
    "declarations": [
      "input count",
      "local countPluralExact = count",
      "local countPlural = count: plural"
    ],
    "selectors": ["countPluralExact", "countPlural"],
    "match": {
      "countPluralExact=0, countPlural=*": "There are no cats.",
      "countPluralExact=*, countPlural=one": "There is one cat.",
      "countPluralExact=*, countPlural=*": "There are {count} cats."
    }
  }]
}

これは、ICU MessageFormat 1 プラグインが {count, plural, =0 {…} one {…} other {…}} をインポートしたときの形です。入力のエイリアスに対する数値キーは、数値 0 と文字列 "0" の両方に一致します。

[!NOTE]local countPluralExact = count のようなアノテーションなしのローカル変数を使うには、これに対応したバージョンのメッセージフォーマットプラグインが必要です (opral/inlang#4440) 。また古いバージョンでは、エクスポート時にセレクターがアルファベット順に並べ替えられるため、countPlural が countPluralExact より前に来ます。その結果、完全一致の一致条件よりも、同じ数値を含むカテゴリのほうが優先されてしまいます。

複数形のオフセット

plural は offset オプションを受け付けます。ICU MessageFormat 1 の {count, plural, offset:1 …} は local countPluralOffset1 = count: plural offset=1 としてインポートされます。カテゴリは count - offset の値で選択されますが、=1 のような完全一致の数値は引き続き count 自体と比較されます。英語で offset=1 の場合、count が 2 なら one、3 なら other が選択されます。

このような plural 内の ICU # は、式 {$count :icu:pound offset=1} としてインポートされ(opral/inlang#4441)、count - offset を表示します。表示は number と同様にロケールに応じてフォーマットされます。たとえば count が 1234 の場合、You and # others は「You and 1,233 others」とレンダリングされます。オフセットがなければ、# は count を number と同様にフォーマットして表示します。count が数値でも数値文字列でもない場合(例: "abc"、"1,234"、null、undefined)は、オフセットを差し引かずに {count} と同様にそのまま表示されます。

序数の複数形処理 (1st、2nd、3rd…)

plural はオプションを Intl.PluralRules にそのまま渡すため、type=ordinal を渡せば序数のカテゴリーを使えます。

{
  "finished_readout": [{
    "declarations": [
      "input placeNumber",
      "local ordinalCategory = placeNumber: plural type=ordinal"
    ],
    "selectors": ["ordinalCategory"],
    "match": {
      "ordinalCategory=one": "You finished in {placeNumber}st place",
      "ordinalCategory=two": "You finished in {placeNumber}nd place",
      "ordinalCategory=few": "You finished in {placeNumber}rd place",
      "ordinalCategory=*": "You finished in {placeNumber}th place"
    }
  }]
}

[!TIP]序数のカテゴリ名 (one、two、few、other など) は、現在のロケールにおける Intl.PluralRules に従います。