ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Translator.php
Go to the documentation of this file.
1<?php
2
3namespace Gettext;
4
5use Gettext\Generators\PhpArray;
6
8{
9 private $domain;
10 private $dictionary = array();
11 private $context_glue = "\004";
12 private $plurals = array();
13
21 public function loadTranslations($translations)
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 }
37
43 public function gettext($original)
44 {
45 return $this->dpgettext($this->domain, null, $original);
46 }
47
53 public function ngettext($original, $plural, $value)
54 {
55 return $this->dnpgettext($this->domain, null, $original, $plural, $value);
56 }
57
63 public function dngettext($domain, $original, $plural, $value)
64 {
65 return $this->dnpgettext($domain, null, $original, $plural, $value);
66 }
67
73 public function npgettext($context, $original, $plural, $value)
74 {
75 return $this->dnpgettext($this->domain, $context, $original, $plural, $value);
76 }
77
83 public function pgettext($context, $original)
84 {
85 return $this->dpgettext($this->domain, $context, $original);
86 }
87
93 public function dgettext($domain, $original)
94 {
95 return $this->dpgettext($domain, null, $original);
96 }
97
103 public function dpgettext($domain, $context, $original)
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 }
113
119 public function dnpgettext($domain, $context, $original, $plural, $value)
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 }
130
136 protected function addTranslations(array $translations)
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 }
166
176 protected function getTranslation($domain, $context, $original)
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 }
182
192 protected function isPlural($domain, $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 }
211
228 private static function fixTerseIfs($code, $inner = false)
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 }
259}
$n
Definition: RandomTest.php:85
$success
Definition: Utf8Test.php:86
$failure
if(!array_key_exists('domain', $_REQUEST)) $domain
Definition: resume.php:8
An exception for terminatinating execution or to throw for unit testing.
Class to manage a collection of translations.
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
loadTranslations($translations)
Loads translation from a Translations instance, a file on an array.
Definition: Translator.php:21
getTranslation($domain, $context, $original)
Search and returns a translation.
Definition: Translator.php:176
ngettext($original, $plural, $value)
Definition: Translator.php:53
isPlural($domain, $n)
Executes the plural decision code given the number to decide which plural version to take.
Definition: Translator.php:192
npgettext($context, $original, $plural, $value)
Definition: Translator.php:73
pgettext($context, $original)
Definition: Translator.php:83
addTranslations(array $translations)
Set new translations to the dictionary.
Definition: Translator.php:136
dnpgettext($domain, $context, $original, $plural, $value)
Definition: Translator.php:119
dgettext($domain, $original)
Definition: Translator.php:93
gettext($original)
Definition: Translator.php:43
dpgettext($domain, $context, $original)
Definition: Translator.php:103
dngettext($domain, $original, $plural, $value)
Definition: Translator.php:63
$key
Definition: croninfo.php:18
$code
Definition: example_050.php:99
Interface used by all translators.
$info
Definition: index.php:5
toArray($value)
Wrap the given value in an array if it is no array.
$context
Definition: webdav.php:25