1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\TranslationsBundle\Service\Export; |
13
|
|
|
|
14
|
|
|
use ONGR\TranslationsBundle\Document\Message; |
15
|
|
|
use ONGR\TranslationsBundle\Document\Translation; |
16
|
|
|
use ONGR\TranslationsBundle\Service\TranslationManager; |
17
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
18
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Export. |
22
|
|
|
*/ |
23
|
|
|
class ExportManager |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var TranslationManager |
27
|
|
|
*/ |
28
|
|
|
private $translationManager; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var YmlExport |
32
|
|
|
*/ |
33
|
|
|
private $exporter; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $locales; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Translation[] |
42
|
|
|
*/ |
43
|
|
|
private $refresh = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param ParameterBag $loadersContainer |
47
|
|
|
* @param TranslationManager $translationManager |
48
|
|
|
* @param YmlExport $exporter |
49
|
|
|
*/ |
50
|
|
|
public function __construct( |
51
|
|
|
ParameterBag $loadersContainer, |
52
|
|
|
TranslationManager $translationManager, |
53
|
|
|
YmlExport $exporter |
54
|
|
|
) { |
55
|
|
|
$this->loadersContainer = $loadersContainer; |
|
|
|
|
56
|
|
|
$this->translationManager = $translationManager; |
57
|
|
|
$this->exporter = $exporter; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function getLocales() |
64
|
|
|
{ |
65
|
|
|
return $this->locales; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param array $locales |
70
|
|
|
*/ |
71
|
|
|
public function setLocales($locales) |
72
|
|
|
{ |
73
|
|
|
$this->locales = $locales; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Exports translations from ES to files. |
78
|
|
|
* |
79
|
|
|
* @param array $domains To export. |
80
|
|
|
* @param bool $force |
81
|
|
|
*/ |
82
|
|
|
public function export($domains = [], $force = null) |
83
|
|
|
{ |
84
|
|
|
foreach ($this->formExportList($domains, $force) as $file => $translations) { |
|
|
|
|
85
|
|
|
if (!file_exists($file)) { |
86
|
|
|
(new Filesystem())->touch($file); |
87
|
|
|
} |
88
|
|
|
list($domain, $locale, $extension) = explode('.', $file); |
89
|
|
|
if ($this->loadersContainer && $this->loadersContainer->has($extension)) { |
90
|
|
|
$messageCatalogue = $this->loadersContainer->get($extension)->load($file, $locale, $domain); |
91
|
|
|
$translations = array_merge($messageCatalogue->all($domain), $translations); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->exporter->export($file, $translations); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->translationManager->save($this->refresh); |
|
|
|
|
98
|
|
|
$this->refresh = []; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get translations for export. |
103
|
|
|
* |
104
|
|
|
* @param array $domains To read from storage. |
105
|
|
|
* @param bool $force Determines if the message status is relevant. |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
private function formExportList($domains, $force) |
110
|
|
|
{ |
111
|
|
|
$output = []; |
112
|
|
|
$filters = array_filter([ |
113
|
|
|
'messages.locale' => $this->getLocales(), |
114
|
|
|
'domain' => $domains |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
|
$translations = $this->translationManager->getAll($filters); |
|
|
|
|
118
|
|
|
|
119
|
|
|
/** @var Translation $translation */ |
120
|
|
|
foreach ($translations as $translation) { |
121
|
|
|
$messages = $translation->getMessages(); |
122
|
|
|
|
123
|
|
|
foreach ($messages as $key => $message) { |
124
|
|
|
if ($message->getStatus() === Message::DIRTY || $force) { |
125
|
|
|
$path = sprintf( |
126
|
|
|
'%s' . DIRECTORY_SEPARATOR . '%s.%s.%s', |
127
|
|
|
$translation->getPath(), |
128
|
|
|
'messages', |
129
|
|
|
$message->getLocale(), |
130
|
|
|
$translation->getFormat() |
131
|
|
|
); |
132
|
|
|
$data[$path][$translation->getKey()] = $message->getMessage(); |
|
|
|
|
133
|
|
|
|
134
|
|
|
$message->setStatus(Message::FRESH); |
135
|
|
|
$this->refresh[] = $translation; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $output; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: