|
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 (context => [original => [translation, plural1, pluraln...]]). |
|
9
|
|
|
*/ |
|
10
|
|
|
trait MultidimensionalArrayTrait |
|
11
|
|
|
{ |
|
12
|
|
|
use HeadersGeneratorTrait; |
|
13
|
|
|
use HeadersExtractorTrait; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Returns a multidimensional array. |
|
17
|
|
|
* |
|
18
|
|
|
* @param Translations $translations |
|
19
|
|
|
* @param bool $includeHeaders |
|
20
|
|
|
* @param bool $forceArray |
|
21
|
|
|
* |
|
22
|
|
|
* @return array |
|
23
|
|
|
*/ |
|
24
|
|
|
private static function toArray(Translations $translations, $includeHeaders, $forceArray = false) |
|
25
|
|
|
{ |
|
26
|
|
|
$pluralForm = $translations->getPluralForms(); |
|
27
|
|
|
$pluralLimit = is_array($pluralForm) ? ($pluralForm[0] - 1) : null; |
|
28
|
|
|
$messages = []; |
|
29
|
|
|
|
|
30
|
|
|
if ($includeHeaders) { |
|
31
|
|
|
$messages[''] = [ |
|
32
|
|
|
'' => [self::generateHeaders($translations)], |
|
33
|
|
|
]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
foreach ($translations as $translation) { |
|
37
|
|
|
$context = $translation->getContext(); |
|
38
|
|
|
$original = $translation->getOriginal(); |
|
39
|
|
|
|
|
40
|
|
|
if (!isset($messages[$context])) { |
|
41
|
|
|
$messages[$context] = []; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ($translation->hasPluralTranslations()) { |
|
45
|
|
|
$messages[$context][$original] = $translation->getPluralTranslations($pluralLimit); |
|
46
|
|
|
array_unshift($messages[$context][$original], $translation->getTranslation()); |
|
47
|
|
|
} elseif ($forceArray) { |
|
48
|
|
|
$messages[$context][$original] = [$translation->getTranslation()]; |
|
49
|
|
|
} else { |
|
50
|
|
|
$messages[$context][$original] = $translation->getTranslation(); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $messages; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Extract the entries from a multidimensional array. |
|
59
|
|
|
* |
|
60
|
|
|
* @param array $messages |
|
61
|
|
|
* @param Translations $translations |
|
62
|
|
|
*/ |
|
63
|
|
|
private static function fromArray(array $messages, Translations $translations) |
|
64
|
|
|
{ |
|
65
|
|
|
foreach ($messages as $context => $contextTranslations) { |
|
66
|
|
|
foreach ($contextTranslations as $original => $value) { |
|
67
|
|
|
if ($context === '' && $original === '') { |
|
68
|
|
|
self::extractHeaders(is_array($value) ? array_shift($value) : $value, $translations); |
|
69
|
|
|
continue; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$translation = $translations->insert($context, $original); |
|
73
|
|
|
|
|
74
|
|
|
if (is_array($value)) { |
|
75
|
|
|
$translation->setTranslation(array_shift($value)); |
|
76
|
|
|
$translation->setPluralTranslations($value); |
|
77
|
|
|
} else { |
|
78
|
|
|
$translation->setTranslation($value); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|