Plural formula in PHP to evaluate ending for a number in Polish language
Providing correct plural form of countable words in English is easy, because there is a difference only between one (1 item) and other (0 items, 2 items, 5 items etc.).
In Polish (and some other languages) this is much harder, because Polish has two different plural forms and plural form of countable words is way, way more complicated and based sometimes not only on whole number but also on last digit of this number etc.
Some examples:
- singular: 1 kot (1 cat),
- first plural: 2 koty (2 cats), 3 koty (3 cats), 4 koty (4 cats), …, 22 koty (22 cats), 23 koty (23 cats), 24 koty (24 cats), …
- second plural: 5 kotów (5 cats), 6 kotów (6 cats), …, 11 kotów (11 cats), 12 kotów (12 cats), …, 19 kotów (19 cats), 20 kotów (20 cats), 21 kotów (21 cats), 25 kotów (25 cats), …
To solve this, following formula / condition can be considered:
(($n==1)?(0):((((($n%10)>=2)&&(($n%10)<=4))&&((($n%100)>10)||(($n%100)>=20)))?(1):2))
It should give you:
0
: singular,1
: first plural,2
: second plural.
To fully understand above formula / condition, we must beautify it:
(
($n == 1) ? 0 :
(
(
(
(
($n % 10) >= 2
)
&&
(
($n % 10) <= 4
)
)
&&
(
(
($n % 100) > 10
)
||
(
($n % 100) >= 20
)
)
) ? 1 : 2
)
)
Yeap! Polish language is harder than English! :>