Skip to content

Instantly share code, notes, and snippets.

@MaslennikovYV
Last active October 17, 2019 09:19
Show Gist options
  • Save MaslennikovYV/dc7774453138436de913 to your computer and use it in GitHub Desktop.
Save MaslennikovYV/dc7774453138436de913 to your computer and use it in GitHub Desktop.
Russian plural Twig filter
public function pluralFilter($number, $word_1, $word_2, $word_5)
//1 предмет, 2 предмета, 5 предметов
{
return [$word_1, $word_2, $word_5][$number % 10 == 1 && $number % 100 != 11? 0 : ($number % 10 >= 2 && $number % 10 <= 4 && $number % 100 < 10 || $number % 100 >= 20 ? 1 : 2)];
}
@eduard-sukharev
Copy link

Here's more readable (thus, easier to support and alter) version, compatible with PHP 5.4:

    public function pluralFilter($number, $word_1, $word_2, $word_5)
    {
        if ($number % 10 == 1 && $number % 100 != 11) {
            return $word_1;
        }
        if (($number % 10 >= 2 && $number % 10 <= 4 && $number % 100 < 10) || $number % 100 >= 20) {
            return $word_2;
        }

        return $word_5;
    }

@MaslennikovYV
Copy link
Author

Okay, it's better

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment