ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Gettext\Translator Class Reference
+ Inheritance diagram for Gettext\Translator:
+ Collaboration diagram for Gettext\Translator:

Public Member Functions

 loadTranslations ($translations)
 Loads translation from a Translations instance, a file on an array. More...
 
 gettext ($original)
 
 ngettext ($original, $plural, $value)
 
 dngettext ($domain, $original, $plural, $value)
 
 npgettext ($context, $original, $plural, $value)
 
 pgettext ($context, $original)
 
 dgettext ($domain, $original)
 
 dpgettext ($domain, $context, $original)
 
 dnpgettext ($domain, $context, $original, $plural, $value)
 
- Public Member Functions inherited from Gettext\BaseTranslator
 register ()
 

Protected Member Functions

 addTranslations (array $translations)
 Set new translations to the dictionary. More...
 
 getTranslation ($domain, $context, $original)
 Search and returns a translation. More...
 
 isPlural ($domain, $n)
 Executes the plural decision code given the number to decide which plural version to take. More...
 

Static Private Member Functions

static fixTerseIfs ($code, $inner=false)
 This function will recursively wrap failure states in brackets if they contain a nested terse if. More...
 

Private Attributes

 $domain
 
 $dictionary = array()
 
 $context_glue = "\004"
 
 $plurals = array()
 

Additional Inherited Members

- Static Public Member Functions inherited from Gettext\BaseTranslator
static initGettextFunctions (TranslatorInterface $translator)
 Set a translation instance as global, to use it with the gettext functions. More...
 
- Static Public Attributes inherited from Gettext\BaseTranslator
static $current
 

Detailed Description

Definition at line 7 of file Translator.php.

Member Function Documentation

◆ addTranslations()

Gettext\Translator::addTranslations ( array  $translations)
protected

Set new translations to the dictionary.

Parameters
array$translations

Definition at line 136 of file Translator.php.

References $code, $domain, $info, and array.

137  {
138  $info = isset($translations['']) ? $translations[''] : null;
139  unset($translations['']);
140 
141  $domain = isset($info['domain']) ? $info['domain'] : 'messages';
142 
143  //Set the first domain loaded as default domain
144  if (!$this->domain) {
145  $this->domain = $domain;
146  }
147 
148  if (!isset($this->dictionary[$domain])) {
149  // If a plural form is set we extract those values
150  $pluralForms = empty($info['plural-forms']) ? 'nplurals=2; plural=(n != 1)' : $info['plural-forms'];
151 
152  list($count, $code) = explode(';', $pluralForms, 2);
153 
154  // extract just the expression turn 'n' into a php variable '$n'.
155  // Slap on a return keyword and semicolon at the end.
156  $this->plurals[$domain] = array(
157  'count' => (int) str_replace('nplurals=', '', $count),
158  'code' => str_replace('plural=', 'return ', str_replace('n', '$n', $code)).';',
159  );
160 
161  $this->dictionary[$domain] = $translations;
162  } else {
163  $this->dictionary[$domain] = array_replace_recursive($this->dictionary[$domain], $translations);
164  }
165  }
$code
Definition: example_050.php:99
Create styles array
The data for the language used.
$info
Definition: index.php:5

◆ dgettext()

Gettext\Translator::dgettext (   $domain,
  $original 
)
See also
TranslatorInterface

{Gets a translation checking the domain.

Parameters
string$domain
string$original
Returns
string
}

Implements Gettext\TranslatorInterface.

Definition at line 93 of file Translator.php.

References $domain.

94  {
95  return $this->dpgettext($domain, null, $original);
96  }
dpgettext($domain, $context, $original)
Definition: Translator.php:103

◆ dngettext()

Gettext\Translator::dngettext (   $domain,
  $original,
  $plural,
  $value 
)
See also
TranslatorInterface

{Gets a translation checking the domain and the plural form.

Parameters
string$domain
string$original
string$plural
string$value
Returns
string
}

Implements Gettext\TranslatorInterface.

Definition at line 63 of file Translator.php.

References $domain.

64  {
65  return $this->dnpgettext($domain, null, $original, $plural, $value);
66  }
dnpgettext($domain, $context, $original, $plural, $value)
Definition: Translator.php:119

◆ dnpgettext()

Gettext\Translator::dnpgettext (   $domain,
  $context,
  $original,
  $plural,
  $value 
)
See also
TranslatorInterface

{Gets a translation checking the domain, the context and the plural form.

Parameters
string$domain
string$context
string$original
string$plural
string$value
}

Implements Gettext\TranslatorInterface.

Definition at line 119 of file Translator.php.

References $domain, and $key.

120  {
121  $key = $this->isPlural($domain, $value);
122  $translation = $this->getTranslation($domain, $context, $original);
123 
124  if (isset($translation[$key]) && $translation[$key] !== '') {
125  return $translation[$key];
126  }
127 
128  return ($key === 1) ? $original : $plural;
129  }
getTranslation($domain, $context, $original)
Search and returns a translation.
Definition: Translator.php:176
isPlural($domain, $n)
Executes the plural decision code given the number to decide which plural version to take...
Definition: Translator.php:192
$key
Definition: croninfo.php:18

◆ dpgettext()

Gettext\Translator::dpgettext (   $domain,
  $context,
  $original 
)
See also
TranslatorInterface

{Gets a translation checking the domain and context.

Parameters
string$domain
string$context
string$original
Returns
string
}

Implements Gettext\TranslatorInterface.

Definition at line 103 of file Translator.php.

References $domain.

104  {
105  $translation = $this->getTranslation($domain, $context, $original);
106 
107  if (isset($translation[1]) && $translation[1] !== '') {
108  return $translation[1];
109  }
110 
111  return $original;
112  }
getTranslation($domain, $context, $original)
Search and returns a translation.
Definition: Translator.php:176

◆ fixTerseIfs()

static Gettext\Translator::fixTerseIfs (   $code,
  $inner = false 
)
staticprivate

This function will recursively wrap failure states in brackets if they contain a nested terse if.

This because PHP can not handle nested terse if's unless they are wrapped in brackets.

This code probably only works for the gettext plural decision codes.

return ($n==1 ? 0 : $n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2); becomes return ($n==1 ? 0 : ($n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2));

Parameters
string$codethe terse if string
bool$innerIf inner is true we wrap it in brackets
Returns
string A formatted terse If that PHP can work with.

Definition at line 228 of file Translator.php.

References $code, $failure, and $success.

229  {
230  /*
231  * (?P<expression>[^?]+) Capture everything up to ? as 'expression'
232  * \? ?
233  * (?P<success>[^:]+) Capture everything up to : as 'success'
234  * : :
235  * (?P<failure>[^;]+) Capture everything up to ; as 'failure'
236  */
237  preg_match('/(?P<expression>[^?]+)\?(?P<success>[^:]+):(?P<failure>[^;]+)/', $code, $matches);
238 
239  // If no match was found then no terse if was present
240  if (!isset($matches[0])) {
241  return $code;
242  }
243 
244  $expression = $matches['expression'];
245  $success = $matches['success'];
246  $failure = $matches['failure'];
247 
248  // Go look for another terse if in the failure state.
249  $failure = self::fixTerseIfs($failure, true);
250  $code = $expression.' ? '.$success.' : '.$failure;
251 
252  if ($inner) {
253  return "($code)";
254  }
255 
256  // note the semicolon. We need that for executing the code.
257  return "$code;";
258  }
$failure
$code
Definition: example_050.php:99
$success
Definition: Utf8Test.php:86

◆ gettext()

Gettext\Translator::gettext (   $original)
See also
TranslatorInterface

{Gets a translation using the original string.

Parameters
string$original
Returns
string
}

Implements Gettext\TranslatorInterface.

Definition at line 43 of file Translator.php.

44  {
45  return $this->dpgettext($this->domain, null, $original);
46  }
dpgettext($domain, $context, $original)
Definition: Translator.php:103

◆ getTranslation()

Gettext\Translator::getTranslation (   $domain,
  $context,
  $original 
)
protected

Search and returns a translation.

Parameters
string$domain
string$context
string$original
Returns
array

Definition at line 176 of file Translator.php.

References $domain, and $key.

177  {
178  $key = isset($context) ? $context.$this->context_glue.$original : $original;
179 
180  return isset($this->dictionary[$domain][$key]) ? $this->dictionary[$domain][$key] : false;
181  }
$key
Definition: croninfo.php:18

◆ isPlural()

Gettext\Translator::isPlural (   $domain,
  $n 
)
protected

Executes the plural decision code given the number to decide which plural version to take.

Parameters
string$domain
string$n
Returns
int

Definition at line 192 of file Translator.php.

References $domain, and $n.

193  {
194  //Not loaded domain, use a fallback
195  if (!isset($this->plurals[$domain])) {
196  return $n == 1 ? 1 : 2;
197  }
198 
199  if (!isset($this->plurals[$domain]['function'])) {
200  $this->plurals[$domain]['function'] = create_function('$n', self::fixTerseIfs($this->plurals[$domain]['code']));
201  }
202 
203  if ($this->plurals[$domain]['count'] <= 2) {
204  return (call_user_func($this->plurals[$domain]['function'], $n)) ? 2 : 1;
205  }
206 
207  // We need to +1 because while (GNU) gettext codes assume 0 based,
208  // this gettext actually stores 1 based.
209  return (call_user_func($this->plurals[$domain]['function'], $n)) + 1;
210  }
$n
Definition: RandomTest.php:85

◆ loadTranslations()

Gettext\Translator::loadTranslations (   $translations)

Loads translation from a Translations instance, a file on an array.

Parameters
Translations | string | array$translations
Returns
self

Definition at line 21 of file Translator.php.

References ILIAS\UI\Implementation\Component\toArray().

22  {
23  if (is_object($translations) && $translations instanceof Translations) {
24  $translations = PhpArray::toArray($translations);
25  } elseif (is_string($translations) && is_file($translations)) {
26  $translations = include $translations;
27  } elseif (!is_array($translations)) {
28  throw new \InvalidArgumentException('Invalid Translator: only arrays, files or instance of Translations are allowed');
29  }
30 
31  foreach ($translations as $translation) {
32  $this->addTranslations($translation);
33  }
34 
35  return $this;
36  }
static toArray(Translations $translations)
Generates an array with the translations.
Definition: PhpArray.php:26
addTranslations(array $translations)
Set new translations to the dictionary.
Definition: Translator.php:136
+ Here is the call graph for this function:

◆ ngettext()

Gettext\Translator::ngettext (   $original,
  $plural,
  $value 
)
See also
TranslatorInterface

{Gets a translation checking the plural form.

Parameters
string$original
string$plural
string$value
Returns
string
}

Implements Gettext\TranslatorInterface.

Definition at line 53 of file Translator.php.

54  {
55  return $this->dnpgettext($this->domain, null, $original, $plural, $value);
56  }
dnpgettext($domain, $context, $original, $plural, $value)
Definition: Translator.php:119

◆ npgettext()

Gettext\Translator::npgettext (   $context,
  $original,
  $plural,
  $value 
)
See also
TranslatorInterface

{Gets a translation checking the context and the plural form.

Parameters
string$context
string$original
string$plural
string$value
Returns
string
}

Implements Gettext\TranslatorInterface.

Definition at line 73 of file Translator.php.

74  {
75  return $this->dnpgettext($this->domain, $context, $original, $plural, $value);
76  }
dnpgettext($domain, $context, $original, $plural, $value)
Definition: Translator.php:119

◆ pgettext()

Gettext\Translator::pgettext (   $context,
  $original 
)
See also
TranslatorInterface

{Gets a translation checking the context.

Parameters
string$context
string$original
Returns
string
}

Implements Gettext\TranslatorInterface.

Definition at line 83 of file Translator.php.

84  {
85  return $this->dpgettext($this->domain, $context, $original);
86  }
dpgettext($domain, $context, $original)
Definition: Translator.php:103

Field Documentation

◆ $context_glue

Gettext\Translator::$context_glue = "\004"
private

Definition at line 11 of file Translator.php.

◆ $dictionary

Gettext\Translator::$dictionary = array()
private

Definition at line 10 of file Translator.php.

◆ $domain

Gettext\Translator::$domain
private

Definition at line 9 of file Translator.php.

◆ $plurals

Gettext\Translator::$plurals = array()
private

Definition at line 12 of file Translator.php.


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