1 | <?php |
||
35 | class Translator |
||
36 | { |
||
37 | /** |
||
38 | * None error. |
||
39 | */ |
||
40 | const ERROR_NONE = 0; |
||
41 | /** |
||
42 | * File does not exist. |
||
43 | */ |
||
44 | const ERROR_DOES_NOT_EXIST = 1; |
||
45 | /** |
||
46 | * File has bad magic number. |
||
47 | */ |
||
48 | const ERROR_BAD_MAGIC = 2; |
||
49 | /** |
||
50 | * Error while reading file, probably too short. |
||
51 | */ |
||
52 | const ERROR_READING = 3; |
||
53 | |||
54 | /** |
||
55 | * Big endian mo file magic bytes. |
||
56 | */ |
||
57 | const MAGIC_BE = "\x95\x04\x12\xde"; |
||
58 | /** |
||
59 | * Little endian mo file magic bytes. |
||
60 | */ |
||
61 | const MAGIC_LE = "\xde\x12\x04\x95"; |
||
62 | |||
63 | /** |
||
64 | * Parse error code (0 if no error). |
||
65 | * |
||
66 | * @var int |
||
67 | */ |
||
68 | public $error = self::ERROR_NONE; |
||
69 | |||
70 | /** |
||
71 | * Cache header field for plural forms. |
||
72 | * |
||
73 | * @var string|null |
||
74 | */ |
||
75 | private $pluralequation = null; |
||
76 | /** |
||
77 | * @var ExpressionLanguage|null Evaluator for plurals |
||
78 | */ |
||
79 | private $pluralexpression = null; |
||
80 | /** |
||
81 | * @var int|null number of plurals |
||
82 | */ |
||
83 | private $pluralcount = null; |
||
84 | /** |
||
85 | * Array with original -> translation mapping. |
||
86 | * |
||
87 | * @var array |
||
88 | */ |
||
89 | private $cache_translations = array(); |
||
90 | |||
91 | /** |
||
92 | * Constructor. |
||
93 | * |
||
94 | * @param string $filename Name of mo file to load |
||
95 | */ |
||
96 | 23 | public function __construct($filename) |
|
139 | |||
140 | /** |
||
141 | * Translates a string. |
||
142 | * |
||
143 | * @param string $msgid String to be translated |
||
144 | * |
||
145 | * @return string translated string (or original, if not found) |
||
146 | */ |
||
147 | 20 | public function gettext($msgid) |
|
155 | |||
156 | /** |
||
157 | * Sanitize plural form expression for use in ExpressionLanguage. |
||
158 | * |
||
159 | * @param string $expr Expression to sanitize |
||
160 | * |
||
161 | * @return string sanitized plural form expression |
||
162 | */ |
||
163 | 10 | public static function sanitizePluralExpression($expr) |
|
184 | |||
185 | /** |
||
186 | * Extracts number of plurals from plurals form expression. |
||
187 | * |
||
188 | * @param string $expr Expression to process |
||
189 | * |
||
190 | * @return int Total number of plurals |
||
191 | */ |
||
192 | 9 | public static function extractPluralCount($expr) |
|
202 | |||
203 | /** |
||
204 | * Parse full PO header and extract only plural forms line. |
||
205 | * |
||
206 | * @param string $header Gettext header |
||
207 | * |
||
208 | * @return string verbatim plural form header field |
||
209 | */ |
||
210 | 8 | public static function extractPluralsForms($header) |
|
222 | |||
223 | /** |
||
224 | * Get possible plural forms from MO header. |
||
225 | * |
||
226 | * @return string plural form header |
||
227 | */ |
||
228 | 5 | private function getPluralForms() |
|
243 | |||
244 | /** |
||
245 | * Detects which plural form to take. |
||
246 | * |
||
247 | * @param int $n count of objects |
||
248 | * |
||
249 | * @return int array index of the right plural form |
||
250 | */ |
||
251 | 5 | private function selectString($n) |
|
266 | |||
267 | /** |
||
268 | * Plural version of gettext. |
||
269 | * |
||
270 | * @param string $msgid Single form |
||
271 | * @param string $msgidPlural Plural form |
||
272 | * @param int $number Number of objects |
||
273 | * |
||
274 | * @return string translated plural form |
||
275 | */ |
||
276 | 12 | public function ngettext($msgid, $msgidPlural, $number) |
|
292 | |||
293 | /** |
||
294 | * Translate with context. |
||
295 | * |
||
296 | * @param string $msgctxt Context |
||
297 | * @param string $msgid String to be translated |
||
298 | * |
||
299 | * @return string translated plural form |
||
300 | */ |
||
301 | 10 | public function pgettext($msgctxt, $msgid) |
|
311 | |||
312 | /** |
||
313 | * Plural version of pgettext. |
||
314 | * |
||
315 | * @param string $msgctxt Context |
||
316 | * @param string $msgid Single form |
||
317 | * @param string $msgidPlural Plural form |
||
318 | * @param int $number Number of objects |
||
319 | * |
||
320 | * @return string translated plural form |
||
321 | */ |
||
322 | 4 | public function npgettext($msgctxt, $msgid, $msgidPlural, $number) |
|
332 | } |
||
333 |