1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace phpMyFAQ\Language; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* The plurals class provides support for plural forms in PMF translations. |
7
|
|
|
* |
8
|
|
|
* |
9
|
|
|
* |
10
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public License, |
11
|
|
|
* v. 2.0. If a copy of the MPL was not distributed with this file, You can |
12
|
|
|
* obtain one at http://mozilla.org/MPL/2.0/. |
13
|
|
|
* |
14
|
|
|
* @package phpMyFAQ |
15
|
|
|
* @author Aurimas Fišeras <[email protected]> |
16
|
|
|
* @copyright 2009-2019 Aurimas Fišeras and phpMyFAQ Team |
17
|
|
|
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
18
|
|
|
* @link https://www.phpmyfaq.de |
19
|
|
|
* @since 2009-07-30 |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
if (!defined('IS_VALID_PHPMYFAQ')) { |
23
|
|
|
exit(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Language_Plurals |
28
|
|
|
* |
29
|
|
|
* @package phpMyFAQ |
30
|
|
|
* @author Aurimas Fišeras <[email protected]> |
31
|
|
|
* @copyright 2009-2019 Aurimas Fišeras and phpMyFAQ Team |
32
|
|
|
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
33
|
|
|
* @link https://www.phpmyfaq.de |
34
|
|
|
* @since 2009-07-30 |
35
|
|
|
*/ |
36
|
|
|
class Plurals |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* The currently loaded PMF translations. |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $PMF_TRANSL = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The number of plural forms for current language $lang. |
47
|
|
|
* |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
private $nPlurals; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The language code of current language. |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $lang; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* True when there is no support for plural forms in current language $lang. |
61
|
|
|
* |
62
|
|
|
* @var bool |
63
|
|
|
*/ |
64
|
|
|
private $useDefaultPluralForm; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Constructor. |
68
|
|
|
* |
69
|
|
|
* @param array $translation PMF translation array for current language |
70
|
|
|
*/ |
71
|
|
|
public function __construct($translation) |
72
|
|
|
{ |
73
|
|
|
$this->PMF_TRANSL = $translation; |
74
|
|
|
$this->nPlurals = (int)$this->PMF_TRANSL['nplurals']; |
75
|
|
|
$this->lang = $this->PMF_TRANSL['metaLanguage']; |
76
|
|
|
|
77
|
|
|
if ($this->plural($this->lang, 0) != -1) { |
78
|
|
|
$this->useDefaultPluralForm = false; |
79
|
|
|
} else { |
80
|
|
|
// @todo update $this->PMF_TRANSL with English plural messages for fall-back |
81
|
|
|
// @todo display warning!? |
82
|
|
|
//echo "function plural_".$this->PMF_TRANSL['metaLanguage']." was not found for language ".$this->PMF_TRANSL['language']) |
83
|
|
|
$this->useDefaultPluralForm = true; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Determines the correct plural form for integer $n |
89
|
|
|
* Returned integer is from interval [0, $nPlurals). |
90
|
|
|
* |
91
|
|
|
* @param int $n The number used to determine the plural form |
92
|
|
|
* |
93
|
|
|
* @return int |
94
|
|
|
*/ |
95
|
|
|
private function _getPlural($n) |
96
|
|
|
{ |
97
|
|
|
if ($this->useDefaultPluralForm) { |
98
|
|
|
// this means we have to fallback to English, so return correct English plural form |
99
|
|
|
return $this->plural('en', $n); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$plural = $this->plural($this->lang, $n); |
103
|
|
|
if ($plural > $this->nPlurals - 1) { |
104
|
|
|
// incorrectly defined plural function or wrong $nPlurals |
105
|
|
|
return $this->nPlurals - 1; |
106
|
|
|
} else { |
107
|
|
|
return $plural; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Helper function that returns the translation template in the correct plural form |
113
|
|
|
* If translation is missing, message in English plural form is returned. |
114
|
|
|
* |
115
|
|
|
* @param string $msgID Message identificator |
116
|
|
|
* @param int $n The number used to determine the plural form |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getMsgTemplate($msgID, $n) |
121
|
|
|
{ |
122
|
|
|
$plural = $this->_getPlural($n); |
123
|
|
|
if (isset($this->PMF_TRANSL[$msgID][$plural])) { |
124
|
|
|
return $this->PMF_TRANSL[$msgID][$plural]; |
125
|
|
|
} else { |
126
|
|
|
// translation for current plural form (>2, since we allways have 2 English plural forms) |
127
|
|
|
// in current language is missing, so as a last resort return default English plural form |
128
|
|
|
return $this->PMF_TRANSL[$msgID][1]; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Returns a translated string in the correct plural form, |
134
|
|
|
* produced according to the formatting of the message. |
135
|
|
|
* |
136
|
|
|
* @param string $msgID Message identificator |
137
|
|
|
* @param int $n The number used to determine the plural form |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function getMsg($msgID, $n) |
142
|
|
|
{ |
143
|
|
|
return sprintf($this->getMsgTemplate($msgID, $n), $n); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Returns the plural form for language $lang or -1 if language $lang is not supported. |
148
|
|
|
* |
149
|
|
|
* @param string $lang The language code |
150
|
|
|
* @param int $n The number used to determine the plural form |
151
|
|
|
* |
152
|
|
|
* @return int |
153
|
|
|
* |
154
|
|
|
* @link http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms |
155
|
|
|
*/ |
156
|
|
|
private function plural($lang, $n) |
157
|
|
|
{ |
158
|
|
|
switch ($lang) { |
159
|
|
|
// Note: expressions in .po files are not strict C expressions, so extra braces might be |
160
|
|
|
// needed for that expression to work here (for example see 'lt') |
161
|
|
|
case 'ar': |
162
|
|
|
return ($n == 0) ? 0 : ($n == 1 ? 1 : ($n == 2 ? 2 : (($n%100 >= 3 && $n%100 <= 10) ? 3 : (($n%100 >= 11 && $n%100 <= 99) || ($n%100 == 1) || ($n%100 == 2) ? 4 : 5)))); |
163
|
|
|
case 'bn': |
164
|
|
|
return 0; |
165
|
|
|
case 'cy': |
166
|
|
|
return ($n == 1) ? 0 : ($n == 2 ? 1 : (($n != 8 && $n != 11) ? 2 : 3)); |
167
|
|
|
case 'cs': |
168
|
|
|
return ($n == 1) ? 0 : (($n >= 2 && $n <= 4) ? 1 : 2); |
169
|
|
|
case 'da': |
170
|
|
|
return $n != 1; |
171
|
|
|
case 'de': |
172
|
|
|
return $n != 1; |
173
|
|
|
case 'el': |
174
|
|
|
return $n != 1; |
175
|
|
|
case 'en': |
176
|
|
|
return $n != 1; |
177
|
|
|
case 'es': |
178
|
|
|
return $n != 1; |
179
|
|
|
case 'eu': |
180
|
|
|
return $n != 1; |
181
|
|
|
case 'fa': |
182
|
|
|
return $n != 1; |
183
|
|
|
case 'fi': |
184
|
|
|
return $n != 1; |
185
|
|
|
case 'fr': |
186
|
|
|
return $n > 1; |
187
|
|
|
case 'he': |
188
|
|
|
return $n != 1; |
189
|
|
|
case 'hi': |
190
|
|
|
return $n != 1; |
191
|
|
|
case 'hu': |
192
|
|
|
return $n != 1; |
193
|
|
|
case 'id': |
194
|
|
|
return 0; |
195
|
|
|
case 'it': |
196
|
|
|
return $n != 1; |
197
|
|
|
case 'ja': |
198
|
|
|
return 0; |
199
|
|
|
case 'ko': |
200
|
|
|
return 0; |
201
|
|
View Code Duplication |
case 'lt': |
202
|
|
|
return ($n%10 == 1 && $n%100 != 11) ? 0 : ($n%10 >= 2 && ($n%100 < 10 || $n%100 >= 20) ? 1 : 2); |
203
|
|
|
case 'lv': |
204
|
|
|
return ($n%10 == 1 && $n%100 != 11) ? 0 : ($n != 0 ? 1 : 2); |
205
|
|
|
case 'nb': |
206
|
|
|
return $n != 1; |
207
|
|
|
case 'nl': |
208
|
|
|
return $n != 1; |
209
|
|
View Code Duplication |
case 'pl': |
210
|
|
|
return ($n == 1) ? 0 : ($n%10 >= 2 && $n%10 <= 4 && ($n%100 < 10 || $n%100 >= 20) ? 1 : 2); |
211
|
|
|
case 'pt': |
212
|
|
|
return $n != 1; |
213
|
|
|
case 'pt-br': |
214
|
|
|
return $n > 1; |
215
|
|
|
case 'ro': |
216
|
|
|
return ($n == 1) ? 0 : (($n == 0 || ($n%100 > 0 && $n%100 < 20)) ? 1 : 2); |
217
|
|
View Code Duplication |
case 'ru': |
218
|
|
|
return ($n%10 == 1 && $n%100 != 11) ? 0 : ($n%10 >= 2 && $n%10 <= 4 && ($n%100 < 10 || $n%100 >= 20) ? 1 : 2); |
219
|
|
|
case 'sl': |
220
|
|
|
return ($n%100 == 1) ? 0 : ($n%100 == 2 ? 1 : ($n%100 == 3 || n%100 == 4 ? 2 : 3)); |
221
|
|
View Code Duplication |
case 'sr': |
222
|
|
|
return ($n%10 == 1 && $n%100 != 11) ? 0 : ($n%10 >= 2 && $n%10 <= 4 && ($n%100 < 10 || $n%100 >= 20) ? 1 : 2); |
223
|
|
|
case 'sv': |
224
|
|
|
return $n != 1; |
225
|
|
|
case 'th': |
226
|
|
|
return 0; |
227
|
|
|
case 'tr': |
228
|
|
|
return 0; |
229
|
|
|
case 'tw': |
230
|
|
|
return 0; |
231
|
|
View Code Duplication |
case 'uk': |
232
|
|
|
return ($n%10 == 1 && $n%100 != 11) ? 0 : ($n%10 >= 2 && $n%10 <= 4 && ($n%100 < 10 || $n%100 >= 20) ? 1 : 2); |
233
|
|
|
case 'vi': |
234
|
|
|
return 0; |
235
|
|
|
case 'zh': |
236
|
|
|
return 0; |
237
|
|
|
default: |
238
|
|
|
//plural expressions can't return negative values, so use -1 to signal unsupported language |
239
|
|
|
return -1; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|