|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gettext\Utils; |
|
4
|
|
|
|
|
5
|
|
|
use Gettext\Translations; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Trait used by all generators that exports the translations to multidimensional arrays |
|
9
|
|
|
* (context => [original => [translation, plural1, pluraln...]]). |
|
10
|
|
|
*/ |
|
11
|
|
|
trait MultidimensionalArrayTrait |
|
12
|
|
|
{ |
|
13
|
|
|
use HeadersGeneratorTrait; |
|
14
|
|
|
use HeadersExtractorTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Returns a multidimensional array. |
|
18
|
|
|
* |
|
19
|
|
|
* @param Translations $translations |
|
20
|
|
|
* @param bool $includeHeaders |
|
21
|
|
|
* @param bool $forceArray |
|
22
|
|
|
* |
|
23
|
|
|
* @return array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected static function toArray(Translations $translations, $includeHeaders, $forceArray = false) |
|
26
|
|
|
{ |
|
27
|
|
|
$pluralForm = $translations->getPluralForms(); |
|
28
|
|
|
$pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null; |
|
29
|
|
|
$messages = []; |
|
30
|
|
|
|
|
31
|
|
|
if ($includeHeaders) { |
|
32
|
|
|
$messages[''] = [ |
|
33
|
|
|
'' => [static::generateHeaders($translations)], |
|
34
|
|
|
]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
foreach ($translations as $translation) { |
|
38
|
|
|
if ($translation->isDisabled()) { |
|
39
|
|
|
continue; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$context = $translation->getContext(); |
|
43
|
|
|
$original = $translation->getOriginal(); |
|
44
|
|
|
|
|
45
|
|
|
if (!isset($messages[$context])) { |
|
46
|
|
|
$messages[$context] = []; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if ($translation->hasPluralTranslations(true)) { |
|
50
|
|
|
$messages[$context][$original] = $translation->getPluralTranslations($pluralSize); |
|
51
|
|
|
array_unshift($messages[$context][$original], $translation->getTranslation()); |
|
52
|
|
|
} elseif ($forceArray) { |
|
53
|
|
|
$messages[$context][$original] = [$translation->getTranslation()]; |
|
54
|
|
|
} else { |
|
55
|
|
|
$messages[$context][$original] = $translation->getTranslation(); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return [ |
|
60
|
|
|
'domain' => $translations->getDomain(), |
|
61
|
|
|
'plural-forms' => $translations->getHeader('Plural-Forms'), |
|
62
|
|
|
'messages' => $messages, |
|
63
|
|
|
]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Extract the entries from a multidimensional array. |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $messages |
|
70
|
|
|
* @param Translations $translations |
|
71
|
|
|
*/ |
|
72
|
|
|
protected static function fromArray(array $messages, Translations $translations) |
|
73
|
|
|
{ |
|
74
|
|
|
if (!empty($messages['domain'])) { |
|
75
|
|
|
$translations->setDomain($messages['domain']); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (!empty($messages['plural-forms'])) { |
|
79
|
|
|
$translations->setHeader(Translations::HEADER_PLURAL, $messages['plural-forms']); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
foreach ($messages['messages'] as $context => $contextTranslations) { |
|
83
|
|
|
foreach ($contextTranslations as $original => $value) { |
|
84
|
|
|
if ($context === '' && $original === '') { |
|
85
|
|
|
static::extractHeaders(is_array($value) ? array_shift($value) : $value, $translations); |
|
86
|
|
|
continue; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$translation = $translations->insert($context, $original); |
|
90
|
|
|
|
|
91
|
|
|
if (is_array($value)) { |
|
92
|
|
|
$translation->setTranslation(array_shift($value)); |
|
93
|
|
|
$translation->setPluralTranslations($value); |
|
94
|
|
|
} else { |
|
95
|
|
|
$translation->setTranslation($value); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|