1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Printful\GettextCms; |
4
|
|
|
|
5
|
|
|
use Gettext\Translation; |
6
|
|
|
use Gettext\Translations; |
7
|
|
|
use Printful\GettextCms\Exceptions\InvalidTranslationException; |
8
|
|
|
use Printful\GettextCms\Interfaces\MessageRepositoryInterface; |
9
|
|
|
use Printful\GettextCms\Structures\MessageItem; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class handles all saving and retrieving logic of translations using the given repository |
13
|
|
|
*/ |
14
|
|
|
class MessageStorage |
15
|
|
|
{ |
16
|
|
|
/** @var MessageRepositoryInterface */ |
17
|
|
|
private $repository; |
18
|
|
|
|
19
|
11 |
|
public function __construct(MessageRepositoryInterface $repository) |
20
|
|
|
{ |
21
|
11 |
|
$this->repository = $repository; |
22
|
11 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Save translation object for a single domain and locale |
26
|
|
|
* |
27
|
|
|
* @param Translations $translations |
28
|
|
|
* @throws InvalidTranslationException |
29
|
|
|
*/ |
30
|
6 |
|
public function saveTranslations(Translations $translations) |
31
|
|
|
{ |
32
|
6 |
|
$locale = $translations->getLanguage(); |
33
|
6 |
|
$domain = (string)$translations->getDomain(); |
34
|
|
|
|
35
|
6 |
|
if (!$locale) { |
36
|
1 |
|
throw new InvalidTranslationException('Locale is missing'); |
37
|
|
|
} |
38
|
|
|
|
39
|
5 |
|
if (!$domain) { |
40
|
1 |
|
throw new InvalidTranslationException('Domain is missing'); |
41
|
|
|
} |
42
|
|
|
|
43
|
4 |
|
foreach ($translations as $v) { |
44
|
4 |
|
$this->saveSingleTranslation($locale, $domain, $v); |
45
|
|
|
} |
46
|
4 |
|
} |
47
|
|
|
|
48
|
6 |
|
public function saveSingleTranslation(string $locale, string $domain, Translation $translation) |
49
|
|
|
{ |
50
|
6 |
|
$key = $this->getKey($locale, $domain, $translation); |
51
|
|
|
|
52
|
6 |
|
$existingItem = $this->repository->getSingle($key); |
53
|
|
|
|
54
|
6 |
|
if ($existingItem->exists()) { |
55
|
2 |
|
$existingTranslation = $this->itemToTranslation($existingItem); |
56
|
2 |
|
$existingTranslation->mergeWith($translation); |
57
|
2 |
|
$item = $this->translationToItem($locale, $domain, $existingTranslation); |
58
|
|
|
|
59
|
|
|
// Override the disabled state of the existing translation |
60
|
2 |
|
$item->isDisabled = $translation->isDisabled(); |
61
|
|
|
} else { |
62
|
6 |
|
$item = $this->translationToItem($locale, $domain, $translation); |
63
|
|
|
} |
64
|
|
|
|
65
|
6 |
|
$item->isTranslated = $translation->hasTranslation(); |
66
|
6 |
|
$item->needsChecking = !$translation->hasTranslation(); |
67
|
|
|
|
68
|
6 |
|
if ($translation->hasPlural() && !$translation->hasPluralTranslations()) { |
69
|
2 |
|
$item->needsChecking = true; |
70
|
|
|
} |
71
|
|
|
|
72
|
6 |
|
$this->repository->save($item); |
73
|
6 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Generate a unique key for storage (basically a primary key) |
77
|
|
|
* |
78
|
|
|
* @param string $locale |
79
|
|
|
* @param string $domain |
80
|
|
|
* @param Translation $translation |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
6 |
|
private function getKey(string $locale, string $domain, Translation $translation): string |
84
|
|
|
{ |
85
|
6 |
|
return md5($locale . '|' . $domain . '|' . $translation->getContext() . '|' . $translation->getOriginal()); |
86
|
|
|
} |
87
|
|
|
|
88
|
8 |
|
private function itemToTranslation(MessageItem $item): Translation |
89
|
|
|
{ |
90
|
8 |
|
$translation = new Translation($item->context, $item->original, $item->originalPlural); |
91
|
|
|
|
92
|
8 |
|
$translation->setTranslation($item->translation); |
93
|
8 |
|
$translation->setPluralTranslations($item->pluralTranslations); |
94
|
8 |
|
$translation->setDisabled($item->isDisabled); |
95
|
|
|
|
96
|
8 |
|
foreach ($item->references as $v) { |
97
|
1 |
|
$translation->addReference($v[0], $v[1]); |
98
|
|
|
} |
99
|
|
|
|
100
|
8 |
|
foreach ($item->comments as $v) { |
101
|
1 |
|
$translation->addComment($v); |
102
|
|
|
} |
103
|
|
|
|
104
|
8 |
|
foreach ($item->extractedComments as $v) { |
105
|
1 |
|
$translation->addExtractedComment($v); |
106
|
|
|
} |
107
|
|
|
|
108
|
8 |
|
return $translation; |
109
|
|
|
} |
110
|
|
|
|
111
|
6 |
|
private function translationToItem(string $locale, string $domain, Translation $translation): MessageItem |
112
|
|
|
{ |
113
|
6 |
|
$item = new MessageItem; |
114
|
|
|
|
115
|
6 |
|
$item->key = $this->getKey($locale, $domain, $translation); |
116
|
6 |
|
$item->domain = $domain; |
117
|
6 |
|
$item->locale = $locale; |
118
|
6 |
|
$item->context = (string)$translation->getContext(); |
119
|
6 |
|
$item->original = $translation->getOriginal(); |
120
|
6 |
|
$item->translation = $translation->getTranslation(); |
121
|
6 |
|
$item->originalPlural = $translation->getPlural(); |
122
|
6 |
|
$item->pluralTranslations = $translation->getPluralTranslations(); |
123
|
6 |
|
$item->references = $translation->getReferences(); |
124
|
6 |
|
$item->comments = $translation->getComments(); |
125
|
6 |
|
$item->extractedComments = $translation->getExtractedComments(); |
126
|
6 |
|
$item->isDisabled = $translation->isDisabled(); |
127
|
|
|
|
128
|
6 |
|
return $item; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* All translations, including disabled, enabled and untranslated |
133
|
|
|
* |
134
|
|
|
* @param string $locale |
135
|
|
|
* @param $domain |
136
|
|
|
* @return Translations |
137
|
|
|
*/ |
138
|
6 |
|
public function getAll(string $locale, $domain): Translations |
139
|
|
|
{ |
140
|
6 |
|
return $this->getTranslations($locale, $domain, false, false); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* All enabled translations including untranslated |
145
|
|
|
* |
146
|
|
|
* @param string $locale |
147
|
|
|
* @param $domain |
148
|
|
|
* @return Translations |
149
|
|
|
*/ |
150
|
2 |
|
public function getAllEnabled(string $locale, $domain): Translations |
151
|
|
|
{ |
152
|
2 |
|
return $this->getTranslations($locale, $domain, true, false); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Enabled and translated translations only |
157
|
|
|
* |
158
|
|
|
* @param string $locale |
159
|
|
|
* @param $domain |
160
|
|
|
* @return Translations |
161
|
|
|
*/ |
162
|
2 |
|
public function getEnabledTranslated(string $locale, $domain): Translations |
163
|
|
|
{ |
164
|
2 |
|
return $this->getTranslations($locale, $domain, true, true); |
165
|
|
|
} |
166
|
|
|
|
167
|
8 |
|
private function getTranslations(string $locale, $domain, bool $enabledOnly, bool $translatedOnly): Translations |
168
|
|
|
{ |
169
|
8 |
|
$domain = (string)$domain; |
170
|
|
|
|
171
|
8 |
|
$translations = new Translations; |
172
|
8 |
|
$translations->setDomain($domain); |
173
|
8 |
|
$translations->setLanguage($locale); |
174
|
|
|
|
175
|
8 |
|
if ($enabledOnly && $translatedOnly) { |
176
|
2 |
|
$items = $this->repository->getEnabledTranslated($locale, $domain); |
177
|
7 |
|
} elseif ($enabledOnly) { |
178
|
2 |
|
$items = $this->repository->getEnabled($locale, $domain); |
179
|
|
|
} else { |
180
|
6 |
|
$items = $this->repository->getAll($locale, $domain); |
181
|
|
|
} |
182
|
|
|
|
183
|
8 |
|
foreach ($items as $v) { |
184
|
8 |
|
$translation = $this->itemToTranslation($v); |
185
|
8 |
|
$translations[] = $translation; |
186
|
|
|
} |
187
|
|
|
|
188
|
8 |
|
return $translations; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Mark all messages in the given domain and locale as disabled |
193
|
|
|
* |
194
|
|
|
* @param string $locale |
195
|
|
|
* @param string $domain |
196
|
|
|
*/ |
197
|
2 |
|
public function disableAll(string $locale, string $domain) |
198
|
|
|
{ |
199
|
2 |
|
$this->repository->disableAll($locale, $domain); |
200
|
|
|
} |
201
|
|
|
} |