I created a plugin that can help you handle pluralization in strings with variables, which can be tricky. It uses language variables, so you can translate a string according to each language’s own plural forms, as defined in the Unicode CLDR.
You can translate cardinal, ordinal, and range plurals. If you have these translations in en.php:
'translations' => [
'apples' => [
'one' => '{{ count }} apple',
'other' => '{{ count }} apples'
],
'place' => [
'one' => '{{ position }}st',
'two' => '{{ position }}nd',
'few' => '{{ position }}rd',
'other' => '{{ position }}th'
],
'cookies' => [
'other' => '{{ start }}-{{ end }} cookies'
]
]
…you can use the provided tp()
helper function to get the correct plural form:
tp('apples', [ 'count' => 1 ]); // 1 apple
tp('apples', [ 'count' => 3 ]); // 3 apples
tp('place', [ 'position' => 1 ]); // 1st
tp('place', [ 'position' => 103 ]); // 103rd
tp('cookies', [ 'start' => 3, 'end' => 4 ]); // 3-4 cookies
Check it here: