|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Lug package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Lug\Component\Translation\Model; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
15
|
|
|
use Doctrine\Common\Collections\Collection; |
|
16
|
|
|
use Lug\Component\Resource\Factory\FactoryInterface; |
|
17
|
|
|
use Lug\Component\Translation\Exception\LocaleNotFoundException; |
|
18
|
|
|
use Lug\Component\Translation\Exception\TranslationNotFoundException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author GeLo <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
trait TranslatableTrait |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $locales; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string|null |
|
32
|
|
|
*/ |
|
33
|
|
|
private $fallbackLocale; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var FactoryInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $translationFactory; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var TranslationInterface[]|Collection |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $translations; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return string|null |
|
47
|
3 |
|
*/ |
|
48
|
|
|
public function getLocale() |
|
49
|
|
|
{ |
|
50
|
3 |
|
try { |
|
51
|
2 |
|
return $this->getTranslation()->getLocale(); |
|
52
|
|
|
} catch (TranslationNotFoundException $e) { |
|
|
|
|
|
|
53
|
2 |
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return string[] |
|
58
|
13 |
|
*/ |
|
59
|
|
|
public function getLocales() |
|
60
|
13 |
|
{ |
|
61
|
|
|
return $this->locales; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string[] $locales |
|
66
|
11 |
|
*/ |
|
67
|
|
|
public function setLocales($locales) |
|
68
|
11 |
|
{ |
|
69
|
11 |
|
$this->locales = $locales; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return bool |
|
74
|
7 |
|
*/ |
|
75
|
|
|
public function hasFallbackLocale() |
|
76
|
7 |
|
{ |
|
77
|
|
|
return $this->fallbackLocale !== null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return string|null |
|
82
|
7 |
|
*/ |
|
83
|
|
|
public function getFallbackLocale() |
|
84
|
7 |
|
{ |
|
85
|
|
|
return $this->fallbackLocale; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string|null $fallbackLocale |
|
90
|
8 |
|
*/ |
|
91
|
|
|
public function setFallbackLocale($fallbackLocale) |
|
92
|
8 |
|
{ |
|
93
|
8 |
|
$this->fallbackLocale = $fallbackLocale; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return FactoryInterface |
|
98
|
6 |
|
*/ |
|
99
|
|
|
public function getTranslationFactory() |
|
100
|
6 |
|
{ |
|
101
|
|
|
return $this->translationFactory; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param FactoryInterface $translationFactory |
|
106
|
5 |
|
*/ |
|
107
|
|
|
public function setTranslationFactory(FactoryInterface $translationFactory) |
|
108
|
5 |
|
{ |
|
109
|
5 |
|
$this->translationFactory = $translationFactory; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return TranslationInterface[]|Collection |
|
114
|
6 |
|
*/ |
|
115
|
|
|
public function getTranslations() |
|
116
|
6 |
|
{ |
|
117
|
|
|
return $this->translations; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param bool $allowCreate |
|
122
|
|
|
* |
|
123
|
|
|
* @return TranslationInterface |
|
124
|
10 |
|
*/ |
|
125
|
|
|
public function getTranslation($allowCreate = false) |
|
126
|
10 |
|
{ |
|
127
|
|
|
$locales = $this->getLocales(); |
|
128
|
10 |
|
|
|
129
|
1 |
|
if (empty($locales)) { |
|
130
|
|
|
throw new LocaleNotFoundException(); |
|
131
|
|
|
} |
|
132
|
9 |
|
|
|
133
|
|
|
$translation = null; |
|
134
|
9 |
|
|
|
135
|
9 |
|
foreach ($locales as $locale) { |
|
136
|
3 |
|
if (($translation = $this->translations->get($locale)) !== null) { |
|
137
|
|
|
break; |
|
138
|
9 |
|
} |
|
139
|
|
|
} |
|
140
|
9 |
|
|
|
141
|
2 |
|
if ($translation === null && $allowCreate) { |
|
142
|
|
|
$translation = $this->getTranslationFactory()->create(['locale' => reset($locales)]); |
|
143
|
2 |
|
$this->addTranslation($translation); |
|
144
|
2 |
|
} |
|
145
|
|
|
|
|
146
|
2 |
|
if ($translation === null && $this->hasFallbackLocale()) { |
|
147
|
2 |
|
$translation = $this->translations->get($this->getFallbackLocale()); |
|
148
|
|
|
} |
|
149
|
9 |
|
|
|
150
|
3 |
|
if ($translation === null) { |
|
151
|
3 |
|
if ($this->hasFallbackLocale()) { |
|
152
|
|
|
$locales[] = $this->getFallbackLocale(); |
|
153
|
9 |
|
} |
|
154
|
4 |
|
|
|
155
|
2 |
|
throw new TranslationNotFoundException(); |
|
156
|
2 |
|
} |
|
157
|
|
|
|
|
158
|
4 |
|
return $translation; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
5 |
|
/** |
|
162
|
|
|
* @param TranslationInterface[]|Collection $translations |
|
163
|
|
|
*/ |
|
164
|
|
|
public function setTranslations($translations) |
|
165
|
|
|
{ |
|
166
|
|
|
$this->translations->clear(); |
|
167
|
9 |
|
|
|
168
|
|
|
foreach ($translations as $translation) { |
|
169
|
9 |
|
$this->addTranslation($translation); |
|
170
|
9 |
|
} |
|
171
|
9 |
|
} |
|
172
|
9 |
|
|
|
173
|
9 |
|
/** |
|
174
|
|
|
* @param TranslationInterface $translation |
|
175
|
|
|
*/ |
|
176
|
|
|
public function addTranslation(TranslationInterface $translation) |
|
177
|
|
|
{ |
|
178
|
2 |
|
if (!$this->translations->containsKey($translation->getLocale())) { |
|
179
|
|
|
$this->translations->set($translation->getLocale(), $translation); |
|
180
|
2 |
|
$translation->setTranslatable($this); |
|
181
|
1 |
|
} |
|
182
|
1 |
|
} |
|
183
|
2 |
|
|
|
184
|
|
|
/** |
|
185
|
19 |
|
* @param TranslationInterface $translation |
|
186
|
|
|
*/ |
|
187
|
19 |
|
public function removeTranslation(TranslationInterface $translation) |
|
188
|
19 |
|
{ |
|
189
|
|
|
if ($this->translations->removeElement($translation)) { |
|
190
|
|
|
$translation->setTranslatable(null); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
private function initTranslatable() |
|
195
|
|
|
{ |
|
196
|
|
|
$this->translations = new ArrayCollection(); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|