|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gettext\Loader; |
|
4
|
|
|
|
|
5
|
|
|
use Gettext\Translations; |
|
6
|
|
|
use Gettext\Translation; |
|
7
|
|
|
use Gettext\Headers; |
|
8
|
|
|
use BadMethodCallException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class to load a array file |
|
12
|
|
|
*/ |
|
13
|
|
|
final class ArrayLoader extends Loader |
|
14
|
|
|
{ |
|
15
|
|
|
use HeadersLoaderTrait; |
|
16
|
|
|
|
|
17
|
|
|
public function loadFile(string $filename, Translations $translations = null): Translations |
|
18
|
|
|
{ |
|
19
|
|
|
$array = self::includeSafe($filename); |
|
20
|
|
|
|
|
21
|
|
|
return $this->loadArray($array, $translations); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function loadString(string $string, Translations $translations = null): Translations |
|
25
|
|
|
{ |
|
26
|
|
|
throw new BadMethodCallException('Arrays cannot be loaded from string. Use ArrayLoader::loadFile() instead'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
private static function includeSafe($filename): array |
|
30
|
|
|
{ |
|
31
|
|
|
return include($filename); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function loadArray(array $array, Translations $translations = null): Translations |
|
35
|
|
|
{ |
|
36
|
|
|
if (!$translations) { |
|
37
|
|
|
$translations = $this->createTranslations(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$messages = $array['messages'] ?? []; |
|
41
|
|
|
|
|
42
|
|
|
foreach ($messages as $context => $contextTranslations) { |
|
43
|
|
|
if ($context === '') { |
|
44
|
|
|
$context = null; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
foreach ($contextTranslations as $original => $value) { |
|
48
|
|
|
//Headers |
|
49
|
|
|
if ($context === null && $original === '') { |
|
50
|
|
|
$string = is_array($value) ? array_shift($value) : $value; |
|
51
|
|
|
$headers = $translations->getHeaders(); |
|
52
|
|
|
|
|
53
|
|
|
foreach (static::parseHeaders($string) as $name => $value) { |
|
|
|
|
|
|
54
|
|
|
$headers->set($name, $value); |
|
55
|
|
|
} |
|
56
|
|
|
continue; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$translation = $this->createTranslation($context, $original); |
|
60
|
|
|
$translations->add($translation); |
|
61
|
|
|
|
|
62
|
|
|
if (is_array($value)) { |
|
63
|
|
|
$translation->translate(array_shift($value)); |
|
64
|
|
|
$translation->translatePlural(...$value); |
|
65
|
|
|
} else { |
|
66
|
|
|
$translation->translate($value); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (!empty($array['domain'])) { |
|
72
|
|
|
$translations->setDomain($array['domain']); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (!empty($array['plural-forms'])) { |
|
76
|
|
|
$translations->getHeaders()->set(Headers::HEADER_PLURAL, $array['plural-forms']); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $translations; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
Late static binding only has effect in subclasses. A
finalclass cannot be extended anymore so late static binding cannot occurr. Consider replacingstatic::withself::.To learn more about late static binding, please refer to the PHP core documentation.