Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php yii2-如何翻译一个同时依赖复数和性别的单词_Php_Yii2_Localization_Internationalization - Fatal编程技术网

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我修复了我的示例,请再试一次。