变体可用于实现复数形式、性别区分、A/B 测试等功能。作为 inlang 的一项强大功能,变体可以根据条件为同一条消息创建不同版本。
[!TIP]需要按语言区域格式化数字或日期(
number、datetime)?请参阅格式化。
匹配
下面的消息会匹配以下条件:
| 平台 | 用户性别 | 消息 |
|---|---|---|
| android | male | {username} 需要在他的手机上通过 Google Play Store 下载该应用。 |
| ios | female | {username} 需要在她的 iPhone 上通过 App Store 下载该应用。 |
| * | * | 此人需要下载该应用。 |
[!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 的规则选出其中一个:按声明顺序依次比较选择器,对每个选择器而言,带字面量键的变体优先于使用通配符 * 的变体。变体的存储顺序不影响结果。
例如,假设选择器为 platform, userGender,变体为 platform=*, userGender=male、platform=android, userGender=* 和 platform=*, userGender=*,那么输入 platform=android, userGender=male 会选中 platform=android, userGender=*,因为在该变体中,第一个选择器 platform 是字面量匹配。
复数形式
你可以在消息中定义一个变量,然后在选择器中使用它。Paraglide 底层借助 Intl.PluralRules 来确定复数形式。
| 输入 | 条件 | 消息 |
|---|---|---|
| count=1 | countPlural=one | There is one cat. |
| count>1 | countPlural=other | There are many cats. |
[!TIP]
local countPlural = count: plural这段语法可以理解为“创建一个局部变量countPlural,其值等于plural(count)”。
{
"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 这样的键永远不会匹配。要匹配精确数字,请再添加一个不带函数、直接作为输入别名的选择器,并将其放在复数选择器之前。这样精确匹配就会优先于类别生效,比如法语的 one 类别也涵盖 0:
{
"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,count 为 3 选中 other。
此类复数中的 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规则。
