ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Gettext\Translations Class Reference

Class to manage a collection of translations. More...

+ Inheritance diagram for Gettext\Translations:
+ Collaboration diagram for Gettext\Translations:

Public Member Functions

 __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->toMoFile($filename); For example: $translations->addFromMoFile($filename);. More...
 
 __clone ()
 Magic method to clone each translation on clone the translations object. More...
 
 offsetSet ($index, $value)
 Control the new translations added. More...
 
 setPluralForms ($count, $rule)
 Set the plural definition. More...
 
 getPluralForms ()
 Returns the parsed plural definition. More...
 
 setHeader ($name, $value)
 Set a new header. More...
 
 getHeader ($name)
 Returns a header value. More...
 
 getHeaders ()
 Returns all header for this translations. More...
 
 deleteHeaders ()
 Removes all headers. More...
 
 deleteHeader ($name)
 Removes one header. More...
 
 getLanguage ()
 Returns the language value. More...
 
 setLanguage ($language)
 Sets the language and the plural forms. More...
 
 hasLanguage ()
 Checks whether the language is empty or not. More...
 
 setDomain ($domain)
 Set a new domain for this translations. More...
 
 getDomain ()
 Returns the domain. More...
 
 hasDomain ()
 Checks whether the domain is empty or not. More...
 
 find ($context, $original='')
 Search for a specific translation. More...
 
 insert ($context, $original, $plural='')
 Creates and insert/merges a new translation. More...
 
 mergeWith (Translations $translations, $method=null)
 Merges this translations with other translations. More...
 

Static Public Member Functions

static __callStatic ($name, $arguments)
 Magic method to create new instances using extractors For example: Translations::fromMoFile($filename);. More...
 

Data Fields

const MERGE_ADD = 1
 
const MERGE_REMOVE = 2
 
const MERGE_HEADERS = 4
 
const MERGE_REFERENCES = 8
 
const MERGE_COMMENTS = 16
 
const MERGE_LANGUAGE = 32
 
const MERGE_PLURAL = 64
 
const MERGE_OVERRIDE = 128
 
const HEADER_LANGUAGE = 'Language'
 
const HEADER_PLURAL = 'Plural-Forms'
 
const HEADER_DOMAIN = 'X-Domain'
 

Static Public Attributes

static $mergeDefault = 93
 

Private Attributes

 $headers
 
 $translationCount
 

Detailed Description

Class to manage a collection of translations.

Definition at line 11 of file Translations.php.

Constructor & Destructor Documentation

◆ __construct()

Gettext\Translations::__construct (   $input = array(),
  $flags = 0,
  $iterator_class = 'ArrayIterator' 
)
See also
\ArrayObject::__construct()

Definition at line 34 of file Translations.php.

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 }

References $input.

Member Function Documentation

◆ __call()

Gettext\Translations::__call (   $name,
  $arguments 
)

Magic method to import/export the translations to a specific format For example: $translations->toMoFile($filename); For example: $translations->addFromMoFile($filename);.

Returns
self|bool

Definition at line 73 of file Translations.php.

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 }

References $name.

◆ __callStatic()

static Gettext\Translations::__callStatic (   $name,
  $arguments 
)
static

Magic method to create new instances using extractors For example: Translations::fromMoFile($filename);.

Returns
Translations

Definition at line 57 of file Translations.php.

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 }

References $name.

◆ __clone()

Gettext\Translations::__clone ( )

Magic method to clone each translation on clone the translations object.

Definition at line 95 of file Translations.php.

96 {
97 $array = array();
98
99 foreach ($this as $key => $translation) {
100 $array[$key] = clone $translation;
101 }
102
103 $this->exchangeArray($array);
104 }
$key
Definition: croninfo.php:18

References $key.

◆ deleteHeader()

Gettext\Translations::deleteHeader (   $name)

Removes one header.

Parameters
string$name

Definition at line 222 of file Translations.php.

223 {
224 unset($this->headers[$name]);
225 }

References $name.

◆ deleteHeaders()

Gettext\Translations::deleteHeaders ( )

Removes all headers.

Definition at line 212 of file Translations.php.

213 {
214 $this->headers = array();
215 }

◆ find()

Gettext\Translations::find (   $context,
  $original = '' 
)

Search for a specific translation.

Parameters
string | Translation$contextThe context of the translation or a translation instance
string$originalThe original string
Returns
Translation|false

Definition at line 309 of file Translations.php.

310 {
311 if ($context instanceof Translation) {
312 $id = $context->getId();
313 } else {
315 }
316
317 return $this->offsetExists($id) ? $this[$id] : false;
318 }
static generateId($context, $original)
Generates the id of a translation (context + glue + original).
Definition: Translation.php:29
if(!array_key_exists('StateId', $_REQUEST)) $id
$context
Definition: webdav.php:25

References $context, and $id.

Referenced by Gettext\Translations\mergeWith().

+ Here is the caller graph for this function:

◆ getDomain()

Gettext\Translations::getDomain ( )

Returns the domain.

Returns
string

Definition at line 284 of file Translations.php.

285 {
286 return $this->getHeader(self::HEADER_DOMAIN);
287 }
getHeader($name)
Returns a header value.

References Dflydev\FigCookies\getHeader().

Referenced by Gettext\Utils\FunctionsScanner\saveGettextFunctions().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getHeader()

Gettext\Translations::getHeader (   $name)

Returns a header value.

Parameters
string$name
Returns
null|string

Definition at line 194 of file Translations.php.

195 {
196 return isset($this->headers[$name]) ? $this->headers[$name] : null;
197 }

References $name.

Referenced by Gettext\Extractors\Po\parseHeaders().

+ Here is the caller graph for this function:

◆ getHeaders()

Gettext\Translations::getHeaders ( )

Returns all header for this translations.

Returns
array

Definition at line 204 of file Translations.php.

205 {
206 return $this->headers;
207 }

Referenced by Gettext\Translations\mergeWith(), Gettext\Generators\Mo\toString(), and Gettext\Generators\Po\toString().

+ Here is the caller graph for this function:

◆ getLanguage()

Gettext\Translations::getLanguage ( )

Returns the language value.

Returns
string $language

Definition at line 232 of file Translations.php.

233 {
234 return $this->getHeader(self::HEADER_LANGUAGE);
235 }

References Dflydev\FigCookies\getHeader().

Referenced by Gettext\Translations\mergeWith().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getPluralForms()

Gettext\Translations::getPluralForms ( )

Returns the parsed plural definition.

Parameters
null|array[count, rule]

Definition at line 154 of file Translations.php.

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 }

References $header, and Dflydev\FigCookies\getHeader().

Referenced by Gettext\Translations\mergeWith().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hasDomain()

Gettext\Translations::hasDomain ( )

Checks whether the domain is empty or not.

Returns
bool

Definition at line 294 of file Translations.php.

295 {
296 $domain = $this->getDomain();
297
298 return (is_string($domain) && ($domain !== '')) ? true : false;
299 }
if(!array_key_exists('domain', $_REQUEST)) $domain
Definition: resume.php:8
getDomain()
Returns the domain.

References $domain.

◆ hasLanguage()

Gettext\Translations::hasLanguage ( )

Checks whether the language is empty or not.

Returns
bool

Definition at line 262 of file Translations.php.

263 {
264 $language = $this->getLanguage();
265
266 return (is_string($language) && ($language !== '')) ? true : false;
267 }
getLanguage()
Returns the language value.

References $language.

◆ insert()

Gettext\Translations::insert (   $context,
  $original,
  $plural = '' 
)

Creates and insert/merges a new translation.

Parameters
string$contextThe translation context
string$originalThe translation original string
string$pluralThe translation original plural string
Returns
Translation The translation created

Definition at line 329 of file Translations.php.

330 {
331 return $this->offsetSet(null, new Translation($context, $original, $plural));
332 }
offsetSet($index, $value)
Control the new translations added.

References $context.

Referenced by Gettext\Extractors\Jed\insertTranslation(), Gettext\Extractors\PhpArray\insertTranslation(), and Gettext\Utils\FunctionsScanner\saveGettextFunctions().

+ Here is the caller graph for this function:

◆ mergeWith()

Gettext\Translations::mergeWith ( Translations  $translations,
  $method = null 
)

Merges this translations with other translations.

Parameters
Translations$translationsThe translations instance to merge with
int | null$methodOne or various Translations::MERGE_* constants to define how to merge the translations

Definition at line 340 of file Translations.php.

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 }
getHeaders()
Returns all header for this translations.
setHeader($name, $value)
Set a new header.
getPluralForms()
Returns the parsed plural definition.
setPluralForms($count, $rule)
Set the plural definition.
find($context, $original='')
Search for a specific translation.
setLanguage($language)
Sets the language and the plural forms.

References $language, $name, Gettext\Translations\find(), Dflydev\FigCookies\getHeader(), Gettext\Translations\getHeaders(), Gettext\Translations\getLanguage(), Gettext\Translations\getPluralForms(), and Sabre\HTTP\setHeader().

+ Here is the call graph for this function:

◆ offsetSet()

Gettext\Translations::offsetSet (   $index,
  $value 
)

Control the new translations added.

Parameters
mixed$index
Translation$value
Exceptions
InvalidArgumentExceptionIf the value is not an instance of Gettext\Translation
Returns
Translation

Definition at line 116 of file Translations.php.

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 }

References $id.

◆ setDomain()

Gettext\Translations::setDomain (   $domain)

Set a new domain for this translations.

Parameters
string$domain

Definition at line 274 of file Translations.php.

275 {
276 $this->setHeader(self::HEADER_DOMAIN, trim($domain));
277 }

References $domain, and Sabre\HTTP\setHeader().

Referenced by Gettext\Extractors\PhpArray\handleArray().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setHeader()

Gettext\Translations::setHeader (   $name,
  $value 
)

Set a new header.

Parameters
string$name
string$value

Definition at line 169 of file Translations.php.

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 }

References $name, and $t.

Referenced by Gettext\Extractors\Po\parseHeaders().

+ Here is the caller graph for this function:

◆ setLanguage()

Gettext\Translations::setLanguage (   $language)

Sets the language and the plural forms.

Parameters
string$language
Returns
bool Returns true if the plural rules has been updated, false if $language hasn't been recognized

Definition at line 244 of file Translations.php.

245 {
246 $this->setHeader(self::HEADER_LANGUAGE, trim($language));
247
249 $this->setPluralForms(count($info->categories), $info->formula);
250
251 return true;
252 }
253
254 return false;
255 }
static getById($id)
Return a Language instance given the language id.
Definition: Language.php:110
$info
Definition: index.php:5

References $info, $language, and Sabre\HTTP\setHeader().

+ Here is the call graph for this function:

◆ setPluralForms()

Gettext\Translations::setPluralForms (   $count,
  $rule 
)

Set the plural definition.

Parameters
int$count
string$rule

Definition at line 144 of file Translations.php.

145 {
146 $this->setHeader(self::HEADER_PLURAL, "nplurals={$count}; plural={$rule};");
147 }

References Sabre\HTTP\setHeader().

+ Here is the call graph for this function:

Field Documentation

◆ $headers

Gettext\Translations::$headers
private

Definition at line 28 of file Translations.php.

◆ $mergeDefault

Gettext\Translations::$mergeDefault = 93
static

Definition at line 26 of file Translations.php.

◆ $translationCount

Gettext\Translations::$translationCount
private

Definition at line 29 of file Translations.php.

◆ HEADER_DOMAIN

const Gettext\Translations::HEADER_DOMAIN = 'X-Domain'

Definition at line 24 of file Translations.php.

◆ HEADER_LANGUAGE

const Gettext\Translations::HEADER_LANGUAGE = 'Language'

Definition at line 22 of file Translations.php.

◆ HEADER_PLURAL

const Gettext\Translations::HEADER_PLURAL = 'Plural-Forms'

Definition at line 23 of file Translations.php.

◆ MERGE_ADD

const Gettext\Translations::MERGE_ADD = 1

Definition at line 13 of file Translations.php.

◆ MERGE_COMMENTS

const Gettext\Translations::MERGE_COMMENTS = 16

Definition at line 17 of file Translations.php.

◆ MERGE_HEADERS

const Gettext\Translations::MERGE_HEADERS = 4

Definition at line 15 of file Translations.php.

◆ MERGE_LANGUAGE

const Gettext\Translations::MERGE_LANGUAGE = 32

Definition at line 18 of file Translations.php.

◆ MERGE_OVERRIDE

const Gettext\Translations::MERGE_OVERRIDE = 128

Definition at line 20 of file Translations.php.

◆ MERGE_PLURAL

const Gettext\Translations::MERGE_PLURAL = 64

Definition at line 19 of file Translations.php.

◆ MERGE_REFERENCES

const Gettext\Translations::MERGE_REFERENCES = 8

Definition at line 16 of file Translations.php.

◆ MERGE_REMOVE

const Gettext\Translations::MERGE_REMOVE = 2

Definition at line 14 of file Translations.php.


The documentation for this class was generated from the following file: