Lines
97.65%
125 / 128
Methods
60.00%
3 / 5
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| plural | 100.00% | 112 / 112 | 100.00% | 1 / 1 | 83 | ||
| get | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| getMsgTemplate | 80.00% | 4 / 5 | 0.00% | 0 / 1 | 3.07 | ||
| getPlural | 66.66% | 4 / 6 | 0.00% | 0 / 1 | 3.33 | ||
| 29 | readonly class Plurals | |
| 30 | { | |
| 31 | /** | |
| 32 | * The number of plural forms for the current language $lang. | |
| 33 | */ | |
| 34 | private int $nPlurals; | |
| 35 | ||
| 36 | /** | |
| 37 | * The language code of the current language. | |
| 38 | * | |
| 39 | * @var string | |
| 40 | */ | |
| 41 | private mixed $lang; | |
| 42 | ||
| 43 | /** | |
| 44 | * True when there is no support for plural forms in the current language $lang. | |
| 45 | */ | |
| 46 | private bool $useDefaultPluralForm; | |
| 47 | ||
| 48 | public function __construct() | |
| 49 | { | |
| 50 | $this->nPlurals = (int) Translation::get(key: 'nplurals'); | |
| 51 | $metaLanguage = Translation::get(key: 'metaLanguage'); | |
| 52 | $this->lang = is_string($metaLanguage) ? $metaLanguage : 'en'; | |
| 53 | ||
| 54 | $this->useDefaultPluralForm = $this->plural(language: $this->lang, number: 0) === -1; | |
| 55 | } | |
| 56 | ||
| 57 | /** | |
| 58 | * Returns the plural form for language $lang or -1 if language $lang is not supported. | |
| 59 | * | |
| 60 | * @link https://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms | |
| 61 | * @param string $language The language code | |
| 62 | * @param int $number The number used to determine the plural form | |
| 63 | */ | |
| 64 | private function plural(string $language, int $number): int | |
| 65 | { | |
| 66 | switch ($language) { | |
| 67 | case 'ar': | |
| 68 | // Arabic has 6 plural forms following the GNU gettext standard: | |
| 69 | // Form 0: zero (n==0) | |
| 70 | // Form 1: one (n==1) | |
| 71 | // Form 2: two (n==2) | |
| 72 | // Form 3: few (n%100>=3 && n%100<=10) - e.g., 3, 10, 103, 210 | |
| 73 | // Form 4: many (n%100>=11 && n%100<=99) - e.g., 11, 99, 111, 199 | |
| 74 | // Form 5: other (everything else) - e.g., 100, 101, 102, 200, 1000 | |
| 75 | if ($number === 0) { | |
| 76 | return 0; | |
| 77 | } | |
| 78 | ||
| 79 | if ($number === 1) { | |
| 80 | return 1; | |
| 81 | } | |
| 82 | ||
| 83 | if ($number === 2) { | |
| 84 | return 2; | |
| 85 | } | |
| 86 | ||
| 87 | $n100 = $number % 100; | |
| 88 | if ($n100 >= 3 && $n100 <= 10) { | |
| 89 | return 3; | |
| 90 | } | |
| 91 | ||
| 92 | if ($n100 >= 11 && $n100 <= 99) { | |
| 93 | return 4; | |
| 94 | } | |
| 95 | ||
| 96 | return 5; | |
| 97 | ||
| 98 | case 'bn': | |
| 99 | case 'he': | |
| 100 | case 'hi': | |
| 101 | case 'id': | |
| 102 | case 'ja': | |
| 103 | case 'ko': | |
| 104 | case 'th': | |
| 105 | case 'tr': | |
| 106 | case 'zh_tw': | |
| 107 | case 'vi': | |
| 108 | case 'zh': | |
| 109 | return 0; | |
| 110 | ||
| 111 | case 'cy': | |
| 112 | if ($number === 1) { | |
| 113 | return 0; | |
| 114 | } | |
| 115 | ||
| 116 | if ($number === 2) { | |
| 117 | return 1; | |
| 118 | } | |
| 119 | ||
| 120 | if ($number !== 8 && $number !== 11) { | |
| 121 | return 2; | |
| 122 | } | |
| 123 | ||
| 124 | return 3; | |
| 125 | ||
| 126 | case 'cs': | |
| 127 | if ($number === 1) { | |
| 128 | return 0; | |
| 129 | } | |
| 130 | ||
| 131 | if ($number >= 2 && $number <= 4) { | |
| 132 | return 1; | |
| 133 | } | |
| 134 | ||
| 135 | return 2; | |
| 136 | ||
| 137 | case 'da': | |
| 138 | case 'de': | |
| 139 | case 'el': | |
| 140 | case 'en': | |
| 141 | case 'es': | |
| 142 | case 'eu': | |
| 143 | case 'fa': | |
| 144 | case 'fi': | |
| 145 | case 'it': | |
| 146 | case 'nb': | |
| 147 | case 'nl': | |
| 148 | case 'hu': | |
| 149 | case 'pt': | |
| 150 | case 'sv': | |
| 151 | if ($number !== 1) { | |
| 152 | return 1; | |
| 153 | } | |
| 154 | ||
| 155 | return 0; | |
| 156 | ||
| 157 | case 'fr': | |
| 158 | case 'pt_br': | |
| 159 | if ($number > 1) { | |
| 160 | return 1; | |
| 161 | } | |
| 162 | ||
| 163 | return 0; | |
| 164 | ||
| 165 | case 'lt': | |
| 166 | $n10 = $number % 10; | |
| 167 | $n100 = $number % 100; | |
| 168 | if ($n10 === 1 && $n100 !== 11) { | |
| 169 | return 0; | |
| 170 | } | |
| 171 | ||
| 172 | if ($n10 >= 2 && ($n100 < 10 || $n100 >= 20)) { | |
| 173 | return 1; | |
| 174 | } | |
| 175 | ||
| 176 | return 2; | |
| 177 | ||
| 178 | case 'lv': | |
| 179 | $n10 = $number % 10; | |
| 180 | $n100 = $number % 100; | |
| 181 | if ($n10 === 1 && $n100 !== 11) { | |
| 182 | return 0; | |
| 183 | } | |
| 184 | ||
| 185 | if ($number !== 0) { | |
| 186 | return 1; | |
| 187 | } | |
| 188 | ||
| 189 | return 2; | |
| 190 | ||
| 191 | case 'pl': | |
| 192 | $n10 = $number % 10; | |
| 193 | $n100 = $number % 100; | |
| 194 | if ($number === 1) { | |
| 195 | return 0; | |
| 196 | } | |
| 197 | ||
| 198 | if ($n10 >= 2 && $n10 <= 4 && ($n100 < 10 || $n100 >= 20)) { | |
| 199 | return 1; | |
| 200 | } | |
| 201 | ||
| 202 | return 2; | |
| 203 | ||
| 204 | case 'ro': | |
| 205 | $n100 = $number % 100; | |
| 206 | if ($number === 1) { | |
| 207 | return 0; | |
| 208 | } | |
| 209 | ||
| 210 | if ($number === 0 || $n100 > 0 && $n100 < 20) { | |
| 211 | return 1; | |
| 212 | } | |
| 213 | ||
| 214 | return 2; | |
| 215 | ||
| 216 | case 'ru': | |
| 217 | case 'sr': | |
| 218 | case 'uk': | |
| 219 | $n10 = $number % 10; | |
| 220 | $n100 = $number % 100; | |
| 221 | if ($n10 === 1 && $n100 !== 11) { | |
| 222 | return 0; | |
| 223 | } | |
| 224 | ||
| 225 | if ($n10 >= 2 && $n10 <= 4 && ($n100 < 10 || $n100 >= 20)) { | |
| 226 | return 1; | |
| 227 | } | |
| 228 | ||
| 229 | return 2; | |
| 230 | ||
| 231 | case 'sl': | |
| 232 | $n100 = $number % 100; | |
| 233 | if ($n100 === 1) { | |
| 234 | return 0; | |
| 235 | } | |
| 236 | ||
| 237 | if ($n100 === 2) { | |
| 238 | return 1; | |
| 239 | } | |
| 240 | ||
| 241 | if ($n100 === 3 || $n100 === 4) { | |
| 242 | return 2; | |
| 243 | } | |
| 244 | ||
| 245 | return 3; | |
| 246 | ||
| 247 | default: | |
| 248 | return -1; | |
| 249 | } | |
| 250 | } | |
| 251 | ||
| 252 | /** | |
| 253 | * Returns a translated string in the correct plural form, | |
| 254 | * produced according to the formatting of the message. | |
| 255 | * | |
| 256 | * @param string $key Message identification | |
| 257 | * @param int $number The number used to determine the plural form | |
| 258 | */ | |
| 259 | public function get(string $key, int $number): string | |
| 260 | { | |
| 261 | return sprintf($this->getMsgTemplate($key, $number), $number); | |
| 262 | } | |
| 263 | ||
| 264 | /** | |
| 265 | * Helper function that returns the translation template in the correct plural form, | |
| 266 | * If translation is missing, a message in English plural form is returned. | |
| 267 | * | |
| 268 | * @param string $key Message identification | |
| 269 | * @param int $number The number used to determine the plural form | |
| 270 | */ | |
| 271 | public function getMsgTemplate(string $key, int $number): string | |
| 272 | { | |
| 273 | $plural = $this->getPlural($number); | |
| 274 | $templates = Translation::get($key); | |
| 275 | if (!is_array($templates)) { | |
| 276 | return is_string($templates) ? $templates : ''; | |
| 277 | } | |
| 278 | ||
| 279 | return (string) ($templates[$plural] ?? $templates[1] ?? ''); | |
| 280 | } | |
| 281 | ||
| 282 | /** | |
| 283 | * Determines the correct plural form for integer $n | |
| 284 | * Returned integer is from an interval [0, $nPlurals). | |
| 285 | * | |
| 286 | * @param int $number The number used to determine the plural form | |
| 287 | */ | |
| 288 | private function getPlural(int $number): int | |
| 289 | { | |
| 290 | if ($this->useDefaultPluralForm) { | |
| 291 | // this means we have to fall back to English, so return the correct English plural form | |
| 292 | return $this->plural(language: 'en', number: $number); | |
| 293 | } | |
| 294 | ||
| 295 | $plural = $this->plural(language: $this->lang, number: $number); | |
| 296 | if ($plural > ($this->nPlurals - 1)) { | |
| 297 | // incorrectly defined plural function or wrong $nPlurals | |
| 298 | return $this->nPlurals - 1; | |
| 299 | } | |
| 300 | ||
| 301 | return $plural; | |
| 302 | } | |
| 303 | } |