1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gettext\Extractors; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use Gettext\Translations; |
7
|
|
|
use Gettext\Utils\HeadersExtractorTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class to get gettext strings from php files returning arrays. |
11
|
|
|
*/ |
12
|
|
|
class PhpArray extends Extractor implements ExtractorInterface |
13
|
|
|
{ |
14
|
|
|
use HeadersExtractorTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
|
|
public static function fromFile($file, Translations $translations, array $options = []) |
20
|
|
|
{ |
21
|
|
|
foreach (static::getFiles($file) as $file) { |
22
|
|
|
static::extract(include($file), $translations); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return $translations; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public static function fromString($string, Translations $translations, array $options = []) |
32
|
|
|
{ |
33
|
|
|
throw new BadMethodCallException('PhpArray::fromString() cannot be called. Use PhpArray::fromFile()'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Handle an array of translations and append to the Translations instance. |
38
|
|
|
* |
39
|
|
|
* @param array $content |
40
|
|
|
* @param Translations $translations |
41
|
|
|
*/ |
42
|
|
|
public static function extract(array $content, Translations $translations) |
43
|
|
|
{ |
44
|
|
|
foreach ($content['messages'] as $context => $messages) { |
45
|
|
|
foreach ($messages as $original => $translation) { |
46
|
|
|
if ($original === '' && $context === '') { |
47
|
|
|
self::extractHeaders(array_shift($translation), $translations); |
48
|
|
|
continue; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$translations->insert($context, $original) |
52
|
|
|
->setTranslation(array_shift($translation)) |
53
|
|
|
->setPluralTranslations($translation); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (!empty($content['domain'])) { |
58
|
|
|
$translations->setDomain($content['domain']); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!empty($content['plural-forms'])) { |
62
|
|
|
$translations->setHeader(Translations::HEADER_PLURAL, $content['plural-forms']); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|