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