ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
PhpArray.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Gettext\Extractors;
4 
5 use Exception;
7 
11 class PhpArray extends Extractor implements ExtractorInterface
12 {
21  public static function fromFile($file, Translations $translations = null)
22  {
23  if ($translations === null) {
24  $translations = new Translations();
25  }
26 
27  foreach (self::getFiles($file) as $file) {
28  self::handleArray(include($file), $translations);
29  }
30 
31  return $translations;
32  }
33 
37  public static function fromString($string, Translations $translations = null, $file = '')
38  {
39  throw new Exception('PhpArray::fromString() cannot be called. Use PhpArray::fromFile()');
40  }
41 
48  public static function handleArray(array $content, Translations $translations)
49  {
50  $content = current($content);
51 
52  $translations_info = isset($content['']) ? $content[''] : null;
53  unset($content['']);
54 
55  if (isset($translations_info['domain'])) {
56  $translations->setDomain($translations_info['domain']);
57  }
58 
59  foreach ($content as $key => $message) {
60  static::insertTranslation($translations, $key, $message);
61  }
62  }
63 
71  protected static function insertTranslation(Translations $translations, $key, $message)
72  {
73  $context_glue = '\u0004';
74  $key = explode($context_glue, $key);
75 
76  $context = isset($key[1]) ? array_shift($key) : '';
77  $original = array_shift($key);
78  $plural = array_shift($message);
79  $translation = array_shift($message);
80  $plural_translation = array_shift($message);
81 
82  $entry = $translations->insert($context, $original, $plural);
83  $entry->setTranslation($translation);
84  $entry->setPluralTranslation($plural_translation);
85  }
86 }
setDomain($domain)
Set a new domain for this translations.
$context
Definition: webdav.php:25
insert($context, $original, $plural='')
Creates and insert/merges a new translation.
static fromFile($file, Translations $translations=null)
Extract the translations from a file.
Definition: PhpArray.php:21
static fromString($string, Translations $translations=null, $file='')
{Parses a string and append the translations found in the Translations instance.The file path to inse...
Definition: PhpArray.php:37
Class to manage a collection of translations.
static handleArray(array $content, Translations $translations)
Handle an array of translations and append to the Translations instance.
Definition: PhpArray.php:48
catch(Exception $e) $message
static insertTranslation(Translations $translations, $key, $message)
Extract and insert a new translation.
Definition: PhpArray.php:71
Class to get gettext strings from php files returning arrays.
Definition: PhpArray.php:11
$key
Definition: croninfo.php:18