ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Translations.php
Go to the documentation of this file.
1<?php
2
3namespace Gettext;
4
6use BadMethodCallException;
7
11class Translations extends \ArrayObject
12{
13 const MERGE_ADD = 1;
14 const MERGE_REMOVE = 2;
15 const MERGE_HEADERS = 4;
16 const MERGE_REFERENCES = 8;
17 const MERGE_COMMENTS = 16;
18 const MERGE_LANGUAGE = 32;
19 const MERGE_PLURAL = 64;
20 const MERGE_OVERRIDE = 128;
21
22 const HEADER_LANGUAGE = 'Language';
23 const HEADER_PLURAL = 'Plural-Forms';
24 const HEADER_DOMAIN = 'X-Domain';
25
26 public static $mergeDefault = 93; // self::MERGE_ADD | self::MERGE_HEADERS | self::MERGE_COMMENTS | self::MERGE_REFERENCES | self::MERGE_PLURAL
27
28 private $headers;
30
34 public function __construct($input = array(), $flags = 0, $iterator_class = 'ArrayIterator')
35 {
36 $this->headers = array(
37 'Project-Id-Version' => '',
38 'Report-Msgid-Bugs-To' => '',
39 'Last-Translator' => '',
40 'Language-Team' => '',
41 'MIME-Version' => '1.0',
42 'Content-Type' => 'text/plain; charset=UTF-8',
43 'Content-Transfer-Encoding' => '8bit',
44 'POT-Creation-Date' => date('c'),
45 'PO-Revision-Date' => date('c'),
46 );
47 $this->headers[self::HEADER_LANGUAGE] = '';
48 parent::__construct($input, $flags, $iterator_class);
49 }
50
57 public static function __callStatic($name, $arguments)
58 {
59 if (!preg_match('/^from(\w+)(File|String)$/i', $name, $matches)) {
60 throw new BadMethodCallException("The method $name does not exists");
61 }
62
63 return call_user_func_array('Gettext\\Extractors\\'.$matches[1].'::from'.$matches[2], $arguments);
64 }
65
73 public function __call($name, $arguments)
74 {
75 if (!preg_match('/^(addFrom|to)(\w+)(File|String)$/i', $name, $matches)) {
76 throw new BadMethodCallException("The method $name does not exists");
77 }
78
79 if ($matches[1] === 'addFrom') {
80 $arguments[] = $this;
81
82 call_user_func_array('Gettext\\Extractors\\'.$matches[2].'::from'.$matches[3], $arguments);
83
84 return $this;
85 }
86
87 array_unshift($arguments, $this);
88
89 return call_user_func_array('Gettext\\Generators\\'.$matches[2].'::to'.$matches[3], $arguments);
90 }
91
95 public function __clone()
96 {
97 $array = array();
98
99 foreach ($this as $key => $translation) {
100 $array[$key] = clone $translation;
101 }
102
103 $this->exchangeArray($array);
104 }
105
116 public function offsetSet($index, $value)
117 {
118 if (!($value instanceof Translation)) {
119 throw new \InvalidArgumentException('Only instances of Gettext\\Translation must be added to a Gettext\\Translations');
120 }
121
122 $id = $value->getId();
123
124 if ($this->offsetExists($id)) {
125 $this[$id]->mergeWith($value);
126 $this[$id]->setTranslationCount($this->translationCount);
127
128 return $this[$id];
129 }
130
131 $value->setTranslationCount($this->translationCount);
132
133 parent::offsetSet($id, $value);
134
135 return $value;
136 }
137
144 public function setPluralForms($count, $rule)
145 {
146 $this->setHeader(self::HEADER_PLURAL, "nplurals={$count}; plural={$rule};");
147 }
148
154 public function getPluralForms()
155 {
156 $header = $this->getHeader(self::HEADER_PLURAL);
157
158 if (!empty($header) && preg_match('/^nplurals\s*=\s*(\d+)\s*;\s*plural\s*=\s*([^;]+)\s*;$/', $header, $matches)) {
159 return array(intval($matches[1]), $matches[2]);
160 }
161 }
162
169 public function setHeader($name, $value)
170 {
171 $name = trim($name);
172 $this->headers[$name] = trim($value);
173
174 if ($name === self::HEADER_PLURAL) {
175 if ($forms = $this->getPluralForms()) {
176 $this->translationCount = $forms[0];
177
178 foreach ($this as $t) {
179 $t->setTranslationCount($this->translationCount);
180 }
181 } else {
182 $this->translationCount = null;
183 }
184 }
185 }
186
194 public function getHeader($name)
195 {
196 return isset($this->headers[$name]) ? $this->headers[$name] : null;
197 }
198
204 public function getHeaders()
205 {
206 return $this->headers;
207 }
208
212 public function deleteHeaders()
213 {
214 $this->headers = array();
215 }
216
222 public function deleteHeader($name)
223 {
224 unset($this->headers[$name]);
225 }
226
232 public function getLanguage()
233 {
234 return $this->getHeader(self::HEADER_LANGUAGE);
235 }
236
244 public function setLanguage($language)
245 {
246 $this->setHeader(self::HEADER_LANGUAGE, trim($language));
247
248 if (($info = Language::getById($language))) {
249 $this->setPluralForms(count($info->categories), $info->formula);
250
251 return true;
252 }
253
254 return false;
255 }
256
262 public function hasLanguage()
263 {
264 $language = $this->getLanguage();
265
266 return (is_string($language) && ($language !== '')) ? true : false;
267 }
268
274 public function setDomain($domain)
275 {
276 $this->setHeader(self::HEADER_DOMAIN, trim($domain));
277 }
278
284 public function getDomain()
285 {
286 return $this->getHeader(self::HEADER_DOMAIN);
287 }
288
294 public function hasDomain()
295 {
296 $domain = $this->getDomain();
297
298 return (is_string($domain) && ($domain !== '')) ? true : false;
299 }
300
309 public function find($context, $original = '')
310 {
311 if ($context instanceof Translation) {
312 $id = $context->getId();
313 } else {
314 $id = Translation::generateId($context, $original);
315 }
316
317 return $this->offsetExists($id) ? $this[$id] : false;
318 }
319
329 public function insert($context, $original, $plural = '')
330 {
331 return $this->offsetSet(null, new Translation($context, $original, $plural));
332 }
333
340 public function mergeWith(Translations $translations, $method = null)
341 {
342 if ($method === null) {
343 $method = self::$mergeDefault;
344 }
345
346 if ($method & self::MERGE_HEADERS) {
347 foreach ($translations->getHeaders() as $name => $value) {
348 if (!$this->getHeader($name)) {
349 $this->setHeader($name, $value);
350 }
351 }
352 }
353
354 $add = (boolean) ($method & self::MERGE_ADD);
355
356 foreach ($translations as $entry) {
357 if (($existing = $this->find($entry))) {
358 $existing->mergeWith($entry, $method);
359 } elseif ($add) {
360 $this[] = clone $entry;
361 }
362 }
363
364 if ($method & self::MERGE_REMOVE) {
365 $filtered = array();
366
367 foreach ($this as $entry) {
368 if ($translations->find($entry)) {
369 $filtered[] = $entry;
370 }
371 }
372
373 $this->exchangeArray($filtered);
374 }
375
376 if ($method & self::MERGE_LANGUAGE) {
377 $language = $translations->getLanguage();
378 $pluralForm = $translations->getPluralForms();
379
380 if (!$pluralForm) {
381 if (!empty($language)) {
382 $this->setLanguage($language);
383 }
384 } else {
385 if (!empty($language)) {
386 $this->setHeader(self::HEADER_LANGUAGE, $language);
387 }
388
389 $this->setPluralForms($pluralForm[0], $pluralForm[1]);
390 }
391 }
392 }
393}
if(!array_key_exists('domain', $_REQUEST)) $domain
Definition: resume.php:8
An exception for terminatinating execution or to throw for unit testing.
Main class to convert the plural rules of a language from CLDR to gettext.
Definition: Language.php:10
Class to manage a translation string.
Definition: Translation.php:9
Class to manage a collection of translations.
getHeaders()
Returns all header for this translations.
deleteHeaders()
Removes all headers.
hasDomain()
Checks whether the domain is empty or not.
getHeader($name)
Returns a header value.
setHeader($name, $value)
Set a new header.
getLanguage()
Returns the language value.
offsetSet($index, $value)
Control the new translations added.
mergeWith(Translations $translations, $method=null)
Merges this translations with other translations.
getPluralForms()
Returns the parsed plural definition.
setPluralForms($count, $rule)
Set the plural definition.
__clone()
Magic method to clone each translation on clone the translations object.
deleteHeader($name)
Removes one header.
hasLanguage()
Checks whether the language is empty or not.
find($context, $original='')
Search for a specific translation.
getDomain()
Returns the domain.
insert($context, $original, $plural='')
Creates and insert/merges a new translation.
setLanguage($language)
Sets the language and the plural forms.
__construct($input=array(), $flags=0, $iterator_class='ArrayIterator')
__call($name, $arguments)
Magic method to import/export the translations to a specific format For example: $translations->toMoF...
static __callStatic($name, $arguments)
Magic method to create new instances using extractors For example: Translations::fromMoFile($filename...
setDomain($domain)
Set a new domain for this translations.
$key
Definition: croninfo.php:18
if(!array_key_exists('StateId', $_REQUEST)) $id
$index
Definition: metadata.php:60
$info
Definition: index.php:5
setHeader($name, $value)
Updates a HTTP header.
$rule
Definition: showstats.php:43
$context
Definition: webdav.php:25