Php yii2-如何翻译一个同时依赖复数和性别的单词
如何实现对单个单词的复数和性别的依赖翻译 我知道如何翻译单词取决于复数:Php yii2-如何翻译一个同时依赖复数和性别的单词,php,yii2,localization,internationalization,Php,Yii2,Localization,Internationalization,如何实现对单个单词的复数和性别的依赖翻译 我知道如何翻译单词取决于复数: Yii::t('app', '{n,plural,=1{Approved} other{Approved}}', ['n' => 0]) 关于性别: Yii::t('app', '{gender,select,feminine{Approved} masculine{Approved} other{Approved}', ['gender' => 'feminine']); 没有办法直接做到这一点。您可以通
Yii::t('app', '{n,plural,=1{Approved} other{Approved}}', ['n' => 0])
关于性别:
Yii::t('app', '{gender,select,feminine{Approved} masculine{Approved} other{Approved}', ['gender' => 'feminine']);
没有办法直接做到这一点。您可以通过创建将两个参数连接在一起的helper方法来解决问题
class TranslationHelper
{
protected static function countToString($count)
{
if ($count === 1) {
return 'sin';
}
retrun 'plu';
}
public static function joinGenderAndCount($gender, $count)
{
return $gender . '-' . static::countToString($count);
}
}
然后,您将在transaltion中使用它,并像这样选择
Yii::t(
'app',
'{genderPl,select,feminine-plu{Approved} feminine-sin{Approved} masculine-plu{Approved} masculine-sin{Approved} other{Approved}',
[
'genderPl' => TranslationHelper::joinGenderAndCount('feminine', 1)
]
);
如果需要更复杂的复数规则,可以在countToString
方法中使用Yii::t
来应用正确的复数规则
protected static function countToString($count)
{
return Yii::t(
'other-category',
'{n,plural,=1{sin} one{sin} few{few} many{plu} other{plu}'
['n' => $count]
);
}
我建议将这种类型的“内部”翻译保留在不同的类别中,以防止与标准翻译冲突。没有办法直接做到这一点。您可以通过创建将两个参数连接在一起的helper方法来解决问题
class TranslationHelper
{
protected static function countToString($count)
{
if ($count === 1) {
return 'sin';
}
retrun 'plu';
}
public static function joinGenderAndCount($gender, $count)
{
return $gender . '-' . static::countToString($count);
}
}
然后,您将在transaltion中使用它,并像这样选择
Yii::t(
'app',
'{genderPl,select,feminine-plu{Approved} feminine-sin{Approved} masculine-plu{Approved} masculine-sin{Approved} other{Approved}',
[
'genderPl' => TranslationHelper::joinGenderAndCount('feminine', 1)
]
);
如果需要更复杂的复数规则,可以在countToString
方法中使用Yii::t
来应用正确的复数规则
protected static function countToString($count)
{
return Yii::t(
'other-category',
'{n,plural,=1{sin} one{sin} few{few} many{plu} other{plu}'
['n' => $count]
);
}
我建议将这种类型的“内部”翻译保留在不同的类别中,以防止与标准翻译冲突。Yii 2使用ICU,它允许您嵌套性别和复数规则。像这样的方法应该会奏效:
Yii::t(
'app',
'{gender,select,'
. 'feminine{{n,plural,=1{Approved} other{Approved}}} '
. 'masculine{{n,plural,=1{Approved} other{Approved}}} '
. 'other{{n,plural,=1{Approved} other{Approved}}}'
. '}',
[
'gender' => 'feminine',
'n' => 0,
]
);
另请参见ICU文档中的示例:Yii 2使用ICU,它允许您嵌套性别和复数规则。像这样的方法应该会奏效:
Yii::t(
'app',
'{gender,select,'
. 'feminine{{n,plural,=1{Approved} other{Approved}}} '
. 'masculine{{n,plural,=1{Approved} other{Approved}}} '
. 'other{{n,plural,=1{Approved} other{Approved}}}'
. '}',
[
'gender' => 'feminine',
'n' => 0,
]
);
另见ICU文档中的示例:我想知道嵌套是否可能,我尝试了你的建议,但我得到了“n,复数,=1{Approved}other{Approved}”@tsanchev我修复了我的示例,请重试。我想知道嵌套是否可能,我尝试了你的建议,但我得到了“n,复数,=1{Approved}other{Approved}”@tsanchev我修复了我的示例,请再试一次。