ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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)
 
static translateFromArray ($context, $translations)
 Pick a translation from a given array of translations for the current language. More...
 

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:177

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

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:453
static getModuleDir($module)
Retrieve the base directory for a module.
Definition: Module.php:39
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 }
$languages
Definition: cssgen2.php:34

References $languages.

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 335 of file Translate.php.

336 {
337 if ($fallbacktag) {
338 return 'not translated ('.$tag.')';
339 } else {
340 return $tag;
341 }
342 }
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 355 of file Translate.php.

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

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 377 of file Translate.php.

378 {
379 if (!empty($otherConfig)) {
380 $filebase = $otherConfig->getPathValue('dictionarydir', 'dictionaries/');
381 } else {
382 $filebase = $this->configuration->getPathValue('dictionarydir', 'dictionaries/');
383 }
384
385 $lang = $this->readDictionaryFile($filebase.$file);
386 \SimpleSAML\Logger::debug('Template: Merging language array. Loading ['.$file.']');
387 $this->langtext = array_merge($this->langtext, $lang);
388 }
for($i=1; $i<=count($kw_cases_sel); $i+=1) $lang
Definition: langwiz.php:349

References $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 453 of file Translate.php.

454 {
455 assert(is_string($filename));
456
457 \SimpleSAML\Logger::debug('Template: Reading ['.$filename.']');
458
459 $jsonFile = $filename.'.definition.json';
460 if (file_exists($jsonFile)) {
461 return $this->readDictionaryJSON($filename);
462 }
463
464 $phpFile = $filename.'.php';
465 if (file_exists($phpFile)) {
466 return $this->readDictionaryPHP($filename);
467 }
468
470 $_SERVER['PHP_SELF'].' - Template: Could not find dictionary file at ['.$filename.']'
471 );
472 return array();
473 }
$filename
Definition: buildRTE.php:89
readDictionaryJSON($filename)
Read a dictionary file in JSON format.
Definition: Translate.php:398
readDictionaryPHP($filename)
Read a dictionary file in PHP format.
Definition: Translate.php:431
static error($string)
Definition: Logger.php:166
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 398 of file Translate.php.

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

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 431 of file Translate.php.

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

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.

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

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:

◆ translateFromArray()

static SimpleSAML\Locale\Translate::translateFromArray (   $context,
  $translations 
)
static

Pick a translation from a given array of translations for the current language.

Parameters
array$contextAn array of options. The current language must be specified as an ISO 639 code accessible with the key "currentLanguage" in the array.
array$translationsAn array of translations. Each translation has an ISO 639 code as its key, identifying the language it corresponds to.
Returns
null|string The translation appropriate for the current language, or null if none found. If the $context or $translations arrays are null, or $context['currentLanguage'] is not defined, null is also returned.

Definition at line 515 of file Translate.php.

516 {
517 if (!is_array($translations) || $translations === null) {
518 return null;
519 }
520
521 if (!is_array($context) || !isset($context['currentLanguage'])) {
522 return null;
523 }
524
525 if (isset($translations[$context['currentLanguage']])) {
526 return $translations[$context['currentLanguage']];
527 }
528
529 // we don't have a translation for the current language, load alternative priorities
531 $langcfg = $sspcfg->getConfigItem('language', null);
532 $priorities = array();
533 if ($langcfg instanceof \SimpleSAML_Configuration) {
534 $priorities = $langcfg->getArray('priorities', array());
535 }
536
537 foreach ($priorities[$context['currentLanguage']] as $lang) {
538 if (isset($translations[$lang])) {
539 return $translations[$lang];
540 }
541 }
542
543 // nothing we can use, return null so that we can set a default
544 return null;
545 }
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
$context
Definition: webdav.php:25

References $context, $lang, and SimpleSAML_Configuration\getInstance().

+ Here is the call graph for this function:

◆ translatePluralGettext()

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

Definition at line 490 of file Translate.php.

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

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

◆ translateSingularGettext()

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

Definition at line 476 of file Translate.php.

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

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: