G
G
Gennady Dorokhin2019-01-19 12:09:30
Yii
Gennady Dorokhin, 2019-01-19 12:09:30

How to implement case declension in Yii2?

What is the best way to implement word declensions by cases in Yii2, what would be written 2 vacancies I found, 100 vacancies Y found, 5 vacancies Y found, 1 vacancy Y found , etc...

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Anton Mashletov, 2019-01-19
@mashletov

I have used this feature before. I don't know how bad it is:

class Helper
{
    /**
     * Выбирает слово с правильными окончанием после числительного.
     *
     * @param int $number число
     * @param array $words варианты склонений ['яблоко', 'яблока', 'яблок']
     * @return string
     */
    public static function plural(int $number, array $words): string
    {
        return $words[($number % 100 > 4 && $number % 100 < 20) ? 2 : [2, 0, 1, 1, 1, 2][min($number % 10, 5)]];
    }
}

// echo "Загружено $count " . Helper::plural($count, ['запись', 'записи', 'записей'];

A
alex stephen, 2019-01-19
@berezuev

https://yiiframework.ru/forum/viewtopic.php?t=22495
well, or google yii2 russian plural form

B
Boris Korobkov, 2019-01-19
@BorisKorobkov

RTFM https://github.com/yiisoft/yii2/blob/master/docs/g...

S
Sergey Vanyushin, 2019-05-27
@wapmorgan

A way to inflect arbitrary words has already been invented: morphos.io/try/numerals?number=5&word=%D1%81%D0%BE...
And an adapter for yii2 - https://github.com/wapmorgan/yii2-inflection

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question