ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
SimpleSAML\Locale\Translate Class Reference
+ Collaboration diagram for SimpleSAML\Locale\Translate:

Public Member Functions

 __construct (\SimpleSAML_Configuration $configuration, $defaultDictionary=null)
 Constructor. More...
 
 getLanguage ()
 Return the internal language object used by this translator. More...
 
 getTag ($tag)
 This method retrieves a tag as an array with language => string mappings. More...
 
 getPreferredTranslation ($translations)
 Retrieve the preferred translation of a given text. More...
 
 getAttributeTranslation ($name)
 Translate the name of an attribute. More...
 
 t ( $tag, $replacements=array(), $fallbackdefault=true, $oldreplacements=array(), $striptags=false)
 Translate a tag into the current language, with a fallback to english. More...
 
 includeInlineTranslation ($tag, $translation)
 Include a translation inline instead of putting translations in dictionaries. More...
 
 includeLanguageFile ($file, $otherConfig=null)
 Include a language file from the dictionaries directory. More...
 

Static Public Member Functions

static noop ($tag)
 Mark a string for translation without translating it. More...
 
static translateSingularGettext ($original)
 
static translatePluralGettext ($original, $plural, $value)
 

Private Member Functions

 getDictionary ($name)
 This method retrieves a dictionary with the name given. More...
 
 getStringNotTranslated ($tag, $fallbacktag)
 Return the string that should be used when no translation was found. More...
 
 readDictionaryJSON ($filename)
 Read a dictionary file in JSON format. More...
 
 readDictionaryPHP ($filename)
 Read a dictionary file in PHP format. More...
 
 readDictionaryFile ($filename)
 Read a dictionary file. More...
 

Private Attributes

 $configuration
 
 $langtext = array()
 
 $dictionaries = array()
 Associative array of dictionaries. More...
 
 $defaultDictionary = null
 The default dictionary. More...
 
 $language
 

Detailed Description

Definition at line 13 of file Translate.php.

Constructor & Destructor Documentation

◆ __construct()

SimpleSAML\Locale\Translate::__construct ( \SimpleSAML_Configuration  $configuration,
  $defaultDictionary = null 
)

Constructor.

Parameters
\SimpleSAML_Configuration$configurationConfiguration object
string | null$defaultDictionaryThe default dictionary where tags will come from.

Definition at line 49 of file Translate.php.

50 {
51 $this->configuration = $configuration;
52 $this->language = new Language($configuration);
53
54 if ($defaultDictionary !== null && substr($defaultDictionary, -4) === '.php') {
55 // TODO: drop this entire if clause for 2.0
56 // for backwards compatibility - print warning
57 $backtrace = debug_backtrace();
58 $where = $backtrace[0]['file'].':'.$backtrace[0]['line'];
60 'Deprecated use of new SimpleSAML\Locale\Translate(...) at '.$where.
61 '. The last parameter is now a dictionary name, which should not end in ".php".'
62 );
63
64 $this->defaultDictionary = substr($defaultDictionary, 0, -4);
65 } else {
66 $this->defaultDictionary = $defaultDictionary;
67 }
68 }
$defaultDictionary
The default dictionary.
Definition: Translate.php:33
static warning($string)
Definition: Logger.php:179

References SimpleSAML\Locale\Translate\$configuration, SimpleSAML\Locale\Translate\$defaultDictionary, and SimpleSAML\Logger\warning().

+ Here is the call graph for this function:

Member Function Documentation

◆ getAttributeTranslation()

SimpleSAML\Locale\Translate::getAttributeTranslation (   $name)

Translate the name of an attribute.

Parameters
string$nameThe attribute name.
Returns
string The translated attribute name, or the original attribute name if no translation was found.

Definition at line 198 of file Translate.php.

199 {
200 // normalize attribute name
201 $normName = strtolower($name);
202 $normName = str_replace(":", "_", $normName);
203
204 // check for an extra dictionary
205 $extraDict = $this->configuration->getString('attributes.extradictionary', null);
206 if ($extraDict !== null) {
207 $dict = $this->getDictionary($extraDict);
208 if (array_key_exists($normName, $dict)) {
209 return $this->getPreferredTranslation($dict[$normName]);
210 }
211 }
212
213 // search the default attribute dictionary
214 $dict = $this->getDictionary('attributes');
215 if (array_key_exists('attribute_'.$normName, $dict)) {
216 return $this->getPreferredTranslation($dict['attribute_'.$normName]);
217 }
218
219 // no translations found
220 return $name;
221 }
getDictionary($name)
This method retrieves a dictionary with the name given.
Definition: Translate.php:90
getPreferredTranslation($translations)
Retrieve the preferred translation of a given text.
Definition: Translate.php:159
if($format !==null) $name
Definition: metadata.php:146

References $name, SimpleSAML\Locale\Translate\getDictionary(), and SimpleSAML\Locale\Translate\getPreferredTranslation().

+ Here is the call graph for this function:

◆ getDictionary()

SimpleSAML\Locale\Translate::getDictionary (   $name)
private

This method retrieves a dictionary with the name given.

Parameters
string$nameThe name of the dictionary, as the filename in the dictionary directory, without the '.php' ending.
Returns
array An associative array with the dictionary.

Definition at line 90 of file Translate.php.

91 {
92 assert('is_string($name)');
93
94 if (!array_key_exists($name, $this->dictionaries)) {
95 $sepPos = strpos($name, ':');
96 if ($sepPos !== false) {
97 $module = substr($name, 0, $sepPos);
98 $fileName = substr($name, $sepPos + 1);
99 $dictDir = \SimpleSAML\Module::getModuleDir($module).'/dictionaries/';
100 } else {
101 $dictDir = $this->configuration->getPathValue('dictionarydir', 'dictionaries/');
102 $fileName = $name;
103 }
104
105 $this->dictionaries[$name] = $this->readDictionaryFile($dictDir.$fileName);
106 }
107
108 return $this->dictionaries[$name];
109 }
readDictionaryFile($filename)
Read a dictionary file.
Definition: Translate.php:450
static getModuleDir($module)
Retrieve the base directory for a module.
Definition: Module.php:122
if($modEnd===false) $module
Definition: module.php:59

References $module, $name, SimpleSAML\Module\getModuleDir(), and SimpleSAML\Locale\Translate\readDictionaryFile().

Referenced by SimpleSAML\Locale\Translate\getAttributeTranslation(), and SimpleSAML\Locale\Translate\getTag().

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

◆ getLanguage()

SimpleSAML\Locale\Translate::getLanguage ( )

Return the internal language object used by this translator.

Returns
\SimpleSAML\Locale\Language

Definition at line 76 of file Translate.php.

References SimpleSAML\Locale\Translate\$language.

◆ getPreferredTranslation()

SimpleSAML\Locale\Translate::getPreferredTranslation (   $translations)

Retrieve the preferred translation of a given text.

Parameters
array$translationsThe translations, as an associative array with language => text mappings.
Returns
string The preferred translation.
Exceptions

Exception If there's no suitable translation.

Definition at line 159 of file Translate.php.

160 {
161 assert('is_array($translations)');
162
163 // look up translation of tag in the selected language
164 $selected_language = $this->language->getLanguage();
165 if (array_key_exists($selected_language, $translations)) {
166 return $translations[$selected_language];
167 }
168
169 // look up translation of tag in the default language
170 $default_language = $this->language->getDefaultLanguage();
171 if (array_key_exists($default_language, $translations)) {
172 return $translations[$default_language];
173 }
174
175 // check for english translation
176 if (array_key_exists('en', $translations)) {
177 return $translations['en'];
178 }
179
180 // pick the first translation available
181 if (count($translations) > 0) {
182 $languages = array_keys($translations);
183 return $translations[$languages[0]];
184 }
185
186 // we don't have anything to return
187 throw new \Exception('Nothing to return from translation.');
188 }

Referenced by SimpleSAML\Locale\Translate\getAttributeTranslation(), and SimpleSAML\Locale\Translate\t().

+ Here is the caller graph for this function:

◆ getStringNotTranslated()

SimpleSAML\Locale\Translate::getStringNotTranslated (   $tag,
  $fallbacktag 
)
private

Return the string that should be used when no translation was found.

Parameters
string$tagA name tag of the string that should be returned.
boolean$fallbacktagIf set to true and string was not found in any languages, return the tag itself. If false return null.
Returns
string The string that should be used, or the tag name if $fallbacktag is set to false.

Definition at line 332 of file Translate.php.

333 {
334 if ($fallbacktag) {
335 return 'not translated ('.$tag.')';
336 } else {
337 return $tag;
338 }
339 }
if(function_exists( 'posix_getuid') &&posix_getuid()===0) if(!array_key_exists('t', $options)) $tag
Definition: cron.php:35

References $tag.

Referenced by SimpleSAML\Locale\Translate\t().

+ Here is the caller graph for this function:

◆ getTag()

SimpleSAML\Locale\Translate::getTag (   $tag)

This method retrieves a tag as an array with language => string mappings.

Parameters
string$tagThe tag name. The tag name can also be on the form '{<dictionary>:<tag>}', to retrieve a tag from the specific dictionary.
Returns
array An associative array with language => string mappings, or null if the tag wasn't found.

Definition at line 120 of file Translate.php.

121 {
122 assert('is_string($tag)');
123
124 // first check translations loaded by the includeInlineTranslation and includeLanguageFile methods
125 if (array_key_exists($tag, $this->langtext)) {
126 return $this->langtext[$tag];
127 }
128
129 // check whether we should use the default dictionary or a dictionary specified in the tag
130 if (substr($tag, 0, 1) === '{' && preg_match('/^{((?:\w+:)?\w+?):(.*)}$/D', $tag, $matches)) {
131 $dictionary = $matches[1];
132 $tag = $matches[2];
133 } else {
134 $dictionary = $this->defaultDictionary;
135 if ($dictionary === null) {
136 // we don't have any dictionary to load the tag from
137 return null;
138 }
139 }
140
141 $dictionary = $this->getDictionary($dictionary);
142 if (!array_key_exists($tag, $dictionary)) {
143 return null;
144 }
145
146 return $dictionary[$tag];
147 }

References SimpleSAML\Locale\Translate\$defaultDictionary, $tag, and SimpleSAML\Locale\Translate\getDictionary().

Referenced by SimpleSAML\Locale\Translate\t().

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

◆ includeInlineTranslation()

SimpleSAML\Locale\Translate::includeInlineTranslation (   $tag,
  $translation 
)

Include a translation inline instead of putting translations in dictionaries.

This function is recommended to be used ONLU from variable data, or when the translation is already provided by an external source, as a database or in metadata.

Parameters
string$tagThe tag that has a translation
array | string$translationThe translation array
Exceptions

Exception If $translation is neither a string nor an array.

Definition at line 352 of file Translate.php.

353 {
354 if (is_string($translation)) {
355 $translation = array('en' => $translation);
356 } elseif (!is_array($translation)) {
357 throw new \Exception("Inline translation should be string or array. Is ".gettype($translation)." now!");
358 }
359
360 \SimpleSAML\Logger::debug('Template: Adding inline language translation for tag ['.$tag.']');
361 $this->langtext[$tag] = $translation;
362 }
static debug($string)
Definition: Logger.php:213

References $tag, and SimpleSAML\Logger\debug().

+ Here is the call graph for this function:

◆ includeLanguageFile()

SimpleSAML\Locale\Translate::includeLanguageFile (   $file,
  $otherConfig = null 
)

Include a language file from the dictionaries directory.

Parameters
string$fileFile name of dictionary to include
\SimpleSAML_Configuration | null$otherConfigOptionally provide a different configuration object than the one provided in the constructor to be used to find the directory of the dictionary. This allows to combine dictionaries inside the SimpleSAMLphp main code distribution together with external dictionaries. Defaults to null.

Definition at line 374 of file Translate.php.

375 {
376 if (!empty($otherConfig)) {
377 $filebase = $otherConfig->getPathValue('dictionarydir', 'dictionaries/');
378 } else {
379 $filebase = $this->configuration->getPathValue('dictionarydir', 'dictionaries/');
380 }
381
382 $lang = $this->readDictionaryFile($filebase.$file);
383 \SimpleSAML\Logger::debug('Template: Merging language array. Loading ['.$file.']');
384 $this->langtext = array_merge($this->langtext, $lang);
385 }
$lang
Definition: consent.php:3
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file

References $file, $lang, SimpleSAML\Logger\debug(), and SimpleSAML\Locale\Translate\readDictionaryFile().

+ Here is the call graph for this function:

◆ noop()

static SimpleSAML\Locale\Translate::noop (   $tag)
static

Mark a string for translation without translating it.

Parameters
string$tagA tag name to mark for translation.
Returns
string The tag, unchanged.

Definition at line 231 of file Translate.php.

232 {
233 return $tag;
234 }

References $tag.

◆ readDictionaryFile()

SimpleSAML\Locale\Translate::readDictionaryFile (   $filename)
private

Read a dictionary file.

Parameters
string$filenameThe absolute path to the dictionary file.
Returns
array An array holding all the translations in the file.

Definition at line 450 of file Translate.php.

451 {
452 assert('is_string($filename)');
453
454 \SimpleSAML\Logger::debug('Template: Reading ['.$filename.']');
455
456 $jsonFile = $filename.'.definition.json';
457 if (file_exists($jsonFile)) {
458 return $this->readDictionaryJSON($filename);
459 }
460
461 $phpFile = $filename.'.php';
462 if (file_exists($phpFile)) {
463 return $this->readDictionaryPHP($filename);
464 }
465
467 $_SERVER['PHP_SELF'].' - Template: Could not find dictionary file at ['.$filename.']'
468 );
469 return array();
470 }
readDictionaryJSON($filename)
Read a dictionary file in JSON format.
Definition: Translate.php:395
readDictionaryPHP($filename)
Read a dictionary file in PHP format.
Definition: Translate.php:428
static error($string)
Definition: Logger.php:168
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']

References $_SERVER, $filename, SimpleSAML\Logger\debug(), SimpleSAML\Logger\error(), SimpleSAML\Locale\Translate\readDictionaryJSON(), and SimpleSAML\Locale\Translate\readDictionaryPHP().

Referenced by SimpleSAML\Locale\Translate\getDictionary(), and SimpleSAML\Locale\Translate\includeLanguageFile().

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

◆ readDictionaryJSON()

SimpleSAML\Locale\Translate::readDictionaryJSON (   $filename)
private

Read a dictionary file in JSON format.

Parameters
string$filenameThe absolute path to the dictionary file, minus the .definition.json ending.
Returns
array An array holding all the translations in the file.

Definition at line 395 of file Translate.php.

396 {
397 $definitionFile = $filename.'.definition.json';
398 assert('file_exists($definitionFile)');
399
400 $fileContent = file_get_contents($definitionFile);
401 $lang = json_decode($fileContent, true);
402
403 if (empty($lang)) {
404 \SimpleSAML\Logger::error('Invalid dictionary definition file ['.$definitionFile.']');
405 return array();
406 }
407
408 $translationFile = $filename.'.translation.json';
409 if (file_exists($translationFile)) {
410 $fileContent = file_get_contents($translationFile);
411 $moreTrans = json_decode($fileContent, true);
412 if (!empty($moreTrans)) {
413 $lang = array_merge_recursive($lang, $moreTrans);
414 }
415 }
416
417 return $lang;
418 }

References $filename, $lang, and SimpleSAML\Logger\error().

Referenced by SimpleSAML\Locale\Translate\readDictionaryFile().

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

◆ readDictionaryPHP()

SimpleSAML\Locale\Translate::readDictionaryPHP (   $filename)
private

Read a dictionary file in PHP format.

Parameters
string$filenameThe absolute path to the dictionary file.
Returns
array An array holding all the translations in the file.

Definition at line 428 of file Translate.php.

429 {
430 $phpFile = $filename.'.php';
431 assert('file_exists($phpFile)');
432
433 $lang = null;
434 include($phpFile);
435 if (isset($lang)) {
436 return $lang;
437 }
438
439 return array();
440 }

References $filename, and $lang.

Referenced by SimpleSAML\Locale\Translate\readDictionaryFile().

+ Here is the caller graph for this function:

◆ t()

SimpleSAML\Locale\Translate::t (   $tag,
  $replacements = array(),
  $fallbackdefault = true,
  $oldreplacements = array(),
  $striptags = false 
)

Translate a tag into the current language, with a fallback to english.

This function is used to look up a translation tag in dictionaries, and return the translation into the current language. If no translation into the current language can be found, english will be tried, and if that fails, placeholder text will be returned.

An array can be passed as the tag. In that case, the array will be assumed to be on the form (language => text), and will be used as the source of translations.

This function can also do replacements into the translated tag. It will search the translated tag for the keys provided in $replacements, and replace any found occurrences with the value of the key.

Parameters
string | array$tagA tag name for the translation which should be looked up, or an array with (language => text) mappings. The array version will go away in 2.0
array$replacementsAn associative array of keys that should be replaced with values in the translated string.
boolean$fallbackdefaultDefault translation to use as a fallback if no valid translation was found.
Deprecated:
Not used in twig, gettext
Returns
string The translated tag, or a placeholder value if the tag wasn't found.

Definition at line 259 of file Translate.php.

265 {
266 $backtrace = debug_backtrace();
267 $where = $backtrace[0]['file'].':'.$backtrace[0]['line'];
268 if (!$fallbackdefault) {
270 'Deprecated use of new SimpleSAML\Locale\Translate::t(...) at '.$where.
271 '. This parameter will go away, the fallback will become' .
272 ' identical to the $tag in 2.0.'
273 );
274 }
275 if (!is_array($replacements)) {
276 // TODO: remove this entire if for 2.0
277
278 // old style call to t(...). Print warning to log
280 'Deprecated use of SimpleSAML\Locale\Translate::t(...) at '.$where.
281 '. Please update the code to use the new style of parameters.'
282 );
283
284 // for backwards compatibility
285 if (!$replacements && $this->getTag($tag) === null) {
287 'Code which uses $fallbackdefault === FALSE should be updated to use the getTag() method instead.'
288 );
289 return null;
290 }
291
292 $replacements = $oldreplacements;
293 }
294
295 if (is_array($tag)) {
296 $tagData = $tag;
298 'Deprecated use of new SimpleSAML\Locale\Translate::t(...) at '.$where.
299 '. The $tag-parameter can only be a string in 2.0.'
300 );
301 } else {
302 $tagData = $this->getTag($tag);
303 if ($tagData === null) {
304 // tag not found
305 \SimpleSAML\Logger::info('Template: Looking up ['.$tag.']: not translated at all.');
306 return $this->getStringNotTranslated($tag, $fallbackdefault);
307 }
308 }
309
310 $translated = $this->getPreferredTranslation($tagData);
311
312 foreach ($replacements as $k => $v) {
313 // try to translate if no replacement is given
314 if ($v == null) {
315 $v = $this->t($k);
316 }
317 $translated = str_replace($k, $v, $translated);
318 }
319 return $translated;
320 }
t( $tag, $replacements=array(), $fallbackdefault=true, $oldreplacements=array(), $striptags=false)
Translate a tag into the current language, with a fallback to english.
Definition: Translate.php:259
getStringNotTranslated($tag, $fallbacktag)
Return the string that should be used when no translation was found.
Definition: Translate.php:332
getTag($tag)
This method retrieves a tag as an array with language => string mappings.
Definition: Translate.php:120
static info($string)
Definition: Logger.php:201

References $tag, SimpleSAML\Locale\Translate\getPreferredTranslation(), SimpleSAML\Locale\Translate\getStringNotTranslated(), SimpleSAML\Locale\Translate\getTag(), SimpleSAML\Logger\info(), SimpleSAML\Locale\Translate\t(), and SimpleSAML\Logger\warning().

Referenced by SimpleSAML\Locale\Translate\t().

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

◆ translatePluralGettext()

static SimpleSAML\Locale\Translate::translatePluralGettext (   $original,
  $plural,
  $value 
)
static

Definition at line 487 of file Translate.php.

488 {
489 $text = \Gettext\BaseTranslator::$current->ngettext($original, $plural, $value);
490
491 if (func_num_args() === 3) {
492 return $text;
493 }
494
495 $args = array_slice(func_get_args(), 3);
496
497 return strtr($text, is_array($args[0]) ? $args[0] : $args);
498 }
$text
Definition: errorreport.php:18

References Gettext\BaseTranslator\$current, and $text.

◆ translateSingularGettext()

static SimpleSAML\Locale\Translate::translateSingularGettext (   $original)
static

Definition at line 473 of file Translate.php.

474 {
475 $text = \Gettext\BaseTranslator::$current->gettext($original);
476
477 if (func_num_args() === 1) {
478 return $text;
479 }
480
481 $args = array_slice(func_get_args(), 1);
482
483 return strtr($text, is_array($args[0]) ? $args[0] : $args);
484 }

References Gettext\BaseTranslator\$current, and $text.

Field Documentation

◆ $configuration

SimpleSAML\Locale\Translate::$configuration
private

Definition at line 21 of file Translate.php.

Referenced by SimpleSAML\Locale\Translate\__construct().

◆ $defaultDictionary

SimpleSAML\Locale\Translate::$defaultDictionary = null
private

The default dictionary.

Definition at line 33 of file Translate.php.

Referenced by SimpleSAML\Locale\Translate\__construct(), and SimpleSAML\Locale\Translate\getTag().

◆ $dictionaries

SimpleSAML\Locale\Translate::$dictionaries = array()
private

Associative array of dictionaries.

Definition at line 28 of file Translate.php.

◆ $langtext

SimpleSAML\Locale\Translate::$langtext = array()
private

Definition at line 23 of file Translate.php.

◆ $language

SimpleSAML\Locale\Translate::$language
private

Definition at line 40 of file Translate.php.

Referenced by SimpleSAML\Locale\Translate\getLanguage().


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