ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
PhpArray.php
Go to the documentation of this file.
1<?php
2
3namespace Gettext\Extractors;
4
5use Exception;
7
11class 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}
An exception for terminatinating execution or to throw for unit testing.
Class to get gettext strings from php files returning arrays.
Definition: PhpArray.php:12
static fromFile($file, Translations $translations=null)
Extract the translations from a file.
Definition: PhpArray.php:21
static insertTranslation(Translations $translations, $key, $message)
Extract and insert a new translation.
Definition: PhpArray.php:71
static fromString($string, Translations $translations=null, $file='')
{Parses a string and append the translations found in the Translations instance.Translations}
Definition: PhpArray.php:37
static handleArray(array $content, Translations $translations)
Handle an array of translations and append to the Translations instance.
Definition: PhpArray.php:48
Class to manage a collection of translations.
insert($context, $original, $plural='')
Creates and insert/merges a new translation.
setDomain($domain)
Set a new domain for this translations.
$key
Definition: croninfo.php:18
catch(Exception $e) $message
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file