1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
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 Sylius\Component\Translation\Model; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Gonzalo Vilaseca <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
trait TranslatableTrait |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var TranslationInterface[] |
23
|
|
|
*/ |
24
|
|
|
protected $translations; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $currentLocale; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Cache current translation. Useful in Doctrine 2.4+ |
33
|
|
|
* |
34
|
|
|
* @var TranslationInterface |
35
|
|
|
*/ |
36
|
|
|
protected $currentTranslation; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $fallbackLocale; |
42
|
|
|
|
43
|
|
|
public function __construct() |
44
|
|
|
{ |
45
|
|
|
$this->translations = new ArrayCollection(); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function getTranslations() |
52
|
|
|
{ |
53
|
|
|
return $this->translations; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function addTranslation(TranslationInterface $translation) |
60
|
|
|
{ |
61
|
|
|
if (!$this->translations->containsKey($translation->getLocale())) { |
62
|
|
|
$this->translations->set($translation->getLocale(), $translation); |
63
|
|
|
$translation->setTranslatable($this); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function removeTranslation(TranslationInterface $translation) |
71
|
|
|
{ |
72
|
|
|
if ($this->translations->removeElement($translation)) { |
73
|
|
|
$translation->setTranslatable(null); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param TranslationInterface $translation |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function hasTranslation(TranslationInterface $translation) |
83
|
|
|
{ |
84
|
|
|
return $this->translations->containsKey($translation->getLocale()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
public function setCurrentLocale($currentLocale) |
91
|
|
|
{ |
92
|
|
|
$this->currentLocale = $currentLocale; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getCurrentLocale() |
99
|
|
|
{ |
100
|
|
|
return $this->currentLocale; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function setFallbackLocale($fallbackLocale) |
107
|
|
|
{ |
108
|
|
|
$this->fallbackLocale = $fallbackLocale; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function getFallbackLocale() |
115
|
|
|
{ |
116
|
|
|
return $this->fallbackLocale; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
public function translate($locale = null) |
123
|
|
|
{ |
124
|
|
|
$locale = $locale ?: $this->currentLocale; |
125
|
|
|
if (null === $locale) { |
126
|
|
|
throw new \RuntimeException('No locale has been set and current locale is undefined.'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if ($this->currentTranslation && $locale === $this->currentTranslation->getLocale()) { |
130
|
|
|
return $this->currentTranslation; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (!$translation = $this->translations->get($locale)) { |
134
|
|
|
if (null === $this->fallbackLocale) { |
135
|
|
|
throw new \RuntimeException('No fallback locale has been set.'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (!$fallbackTranslation = $this->translations->get($this->getFallbackLocale())) { |
139
|
|
|
$className = $this->getTranslationClass(); |
140
|
|
|
|
141
|
|
|
/** @var TranslationInterface $translation */ |
142
|
|
|
$translation = new $className(); |
143
|
|
|
$translation->setLocale($locale); |
144
|
|
|
|
145
|
|
|
$this->addTranslation($translation); |
146
|
|
|
} else { |
147
|
|
|
$translation = clone $fallbackTranslation; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$this->currentTranslation = $translation; |
152
|
|
|
|
153
|
|
|
return $translation; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Return translation model class. |
158
|
|
|
* |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
public static function getTranslationClass() |
162
|
|
|
{ |
163
|
|
|
return get_called_class().'Translation'; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..