1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gettext\Extractors; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Gettext\Translations; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class to get gettext strings from php files returning arrays. |
10
|
|
|
*/ |
11
|
|
|
class PhpArray extends Extractor implements ExtractorInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Extract the translations from a file. |
15
|
|
|
* |
16
|
|
|
* @param array|string $file A path of a file or files |
17
|
|
|
* @param null|Translations $translations The translations instance to append the new translations. |
18
|
|
|
* |
19
|
|
|
* @return Translations |
20
|
|
|
*/ |
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
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
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
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Handle an array of translations and append to the Translations instance. |
44
|
|
|
* |
45
|
|
|
* @param array $content |
46
|
|
|
* @param Translations $translations |
47
|
|
|
*/ |
48
|
|
|
public static function handleArray(array $content, Translations $translations) |
49
|
|
|
{ |
50
|
|
|
$content = $content['messages']; |
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
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Extract and insert a new translation |
66
|
|
|
* |
67
|
|
|
* @param Translations $translations |
68
|
|
|
* @param string $key |
69
|
|
|
* @param string $message |
70
|
|
|
*/ |
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
|
|
|
} |
87
|
|
|
|