|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Printful\GettextCms; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Gettext\Translation; |
|
8
|
|
|
use Gettext\Translations; |
|
9
|
|
|
use Printful\GettextCms\Interfaces\MessageRepositoryInterface; |
|
10
|
|
|
use Printful\GettextCms\Structures\MessageItem; |
|
11
|
|
|
|
|
12
|
|
|
class MessageStorage |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var MessageRepositoryInterface */ |
|
15
|
|
|
private $repository; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(MessageRepositoryInterface $repository) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->repository = $repository; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function saveTranslations(Translations $translations) |
|
23
|
|
|
{ |
|
24
|
|
|
$locale = $translations->getLanguage(); |
|
25
|
|
|
$domain = (string)$translations->getDomain(); |
|
26
|
|
|
|
|
27
|
|
|
foreach ($translations as $v) { |
|
28
|
|
|
$this->saveSingleTranslation($locale, $domain, $v); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
private function saveSingleTranslation(string $locale, string $domain, Translation $translation): void |
|
33
|
|
|
{ |
|
34
|
|
|
$key = $this->getKey($locale, $domain, $translation); |
|
35
|
|
|
|
|
36
|
|
|
$existingItem = $this->repository->getSingle($key); |
|
37
|
|
|
|
|
38
|
|
|
if ($existingItem->exists()) { |
|
39
|
|
|
$existingTranslation = $this->itemToTranslation($existingItem); |
|
40
|
|
|
$existingTranslation->mergeWith($translation); |
|
41
|
|
|
$item = $this->translationToItem($locale, $domain, $existingTranslation); |
|
42
|
|
|
|
|
43
|
|
|
// Override the disabled state of the existing translation |
|
44
|
|
|
$item->isDisabled = $translation->isDisabled(); |
|
45
|
|
|
} else { |
|
46
|
|
|
$item = $this->translationToItem($locale, $domain, $translation); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$this->repository->save($item); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Generate a unique key for storage (basically a primary key) |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $locale |
|
56
|
|
|
* @param string $domain |
|
57
|
|
|
* @param Translation $translation |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
private function getKey(string $locale, string $domain, Translation $translation): string |
|
61
|
|
|
{ |
|
62
|
|
|
return md5($locale . '|' . $domain . '|' . $translation->getContext() . '|' . $translation->getOriginal()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function itemToTranslation(MessageItem $item): Translation |
|
66
|
|
|
{ |
|
67
|
|
|
$translation = new Translation($item->context, $item->original, $item->originalPlural); |
|
68
|
|
|
|
|
69
|
|
|
$translation->setTranslation($item->translation); |
|
70
|
|
|
$translation->setPluralTranslations($item->pluralTranslations); |
|
71
|
|
|
$translation->setDisabled($item->isDisabled); |
|
72
|
|
|
|
|
73
|
|
|
foreach ($item->references as $v) { |
|
74
|
|
|
$translation->addReference($v[0], $v[1]); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
foreach ($item->comments as $v) { |
|
78
|
|
|
$translation->addComment($v); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
foreach ($item->extractedComments as $v) { |
|
82
|
|
|
$translation->addExtractedComment($v); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $translation; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private function translationToItem(string $locale, string $domain, Translation $translation): MessageItem |
|
89
|
|
|
{ |
|
90
|
|
|
$item = new MessageItem; |
|
91
|
|
|
|
|
92
|
|
|
$item->key = $this->getKey($locale, $domain, $translation); |
|
93
|
|
|
$item->domain = $domain; |
|
94
|
|
|
$item->locale = $locale; |
|
95
|
|
|
$item->context = (string)$translation->getContext(); |
|
96
|
|
|
$item->original = $translation->getOriginal(); |
|
97
|
|
|
$item->translation = $translation->getTranslation(); |
|
98
|
|
|
$item->originalPlural = $translation->getPlural(); |
|
99
|
|
|
$item->pluralTranslations = $translation->getPluralTranslations(); |
|
100
|
|
|
$item->references = $translation->getReferences(); |
|
101
|
|
|
$item->comments = $translation->getComments(); |
|
102
|
|
|
$item->extractedComments = $translation->getExtractedComments(); |
|
103
|
|
|
$item->isDisabled = $translation->isDisabled(); |
|
104
|
|
|
|
|
105
|
|
|
return $item; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getTranslations(string $locale, $domain): Translations |
|
109
|
|
|
{ |
|
110
|
|
|
$domain = (string)$domain; |
|
111
|
|
|
|
|
112
|
|
|
$translations = new Translations; |
|
113
|
|
|
$translations->setDomain($domain); |
|
114
|
|
|
$translations->setLanguage($locale); |
|
115
|
|
|
|
|
116
|
|
|
$items = $this->repository->getAll($locale, $domain); |
|
117
|
|
|
|
|
118
|
|
|
foreach ($items as $v) { |
|
119
|
|
|
$translation = $this->itemToTranslation($v); |
|
120
|
|
|
$translations->offsetSet(null, $translation); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $translations; |
|
124
|
|
|
} |
|
125
|
|
|
} |