ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 ()
 
 register ()
 Register this translator as global, to use with the gettext functions __(), p__(), etc. More...
 
 gettext ($original)
 Gets a translation using the original string. More...
 
 ngettext ($original, $plural, $value)
 Gets a translation checking the plural form. More...
 
 dngettext ($domain, $original, $plural, $value)
 Gets a translation checking the domain and the plural form. More...
 
 npgettext ($context, $original, $plural, $value)
 Gets a translation checking the context and the plural form. More...
 
 pgettext ($context, $original)
 Gets a translation checking the context. More...
 
 dgettext ($domain, $original)
 Gets a translation checking the domain. More...
 
 dpgettext ($domain, $context, $original)
 Gets a translation checking the domain and context. More...
 
 dnpgettext ($domain, $context, $original, $plural, $value)
 Gets a translation checking the domain, the context and the plural form. More...
 

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.

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
$info
Definition: index.php:5

References $code, $domain, and $info.

◆ 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.

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

References $domain.

◆ 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.

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

References $domain.

◆ 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.

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
$context
Definition: webdav.php:25

References $context, $domain, and $key.

◆ 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.

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 }

References $context, and $domain.

◆ 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.

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.
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 }
$success
Definition: Utf8Test.php:86
$failure
static fixTerseIfs($code, $inner=false)
This function will recursively wrap failure states in brackets if they contain a nested terse if.
Definition: Translator.php:228

References $code, $failure, and $success.

◆ 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 }

◆ 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.

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 }

References $context, $domain, and $key.

◆ 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.

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

References $domain, and $n.

◆ 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.

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 }
addTranslations(array $translations)
Set new translations to the dictionary.
Definition: Translator.php:136
toArray($value)
Wrap the given value in an array if it is no array.

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

+ 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 }

◆ 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 }

References $context.

◆ 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 }

References $context.

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: