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\Resource\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 getTranslation($locale = null) |
52
|
|
|
{ |
53
|
|
|
$locale = $locale ?: $this->currentLocale; |
54
|
|
|
if (null === $locale) { |
55
|
|
|
throw new \RuntimeException('No locale has been set and current locale is undefined.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($this->currentTranslation && $locale === $this->currentTranslation->getLocale()) { |
59
|
|
|
return $this->currentTranslation; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$translation = $this->translations->get($locale) |
63
|
|
|
if (null === $translation) { |
|
|
|
|
64
|
|
|
if (null === $this->fallbackLocale) { |
65
|
|
|
throw new \RuntimeException('No fallback locale has been set.'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$fallbackTranslation = $this->translations->get($this->fallbackLocale) |
69
|
|
|
if (null === $fallbackTranslation) { |
70
|
|
|
$translation = $this->createTranslation(); |
71
|
|
|
$translation->setLocale($locale); |
72
|
|
|
|
73
|
|
|
$this->addTranslation($translation); |
74
|
|
|
} else { |
75
|
|
|
$translation = clone $fallbackTranslation; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->currentTranslation = $translation; |
80
|
|
|
|
81
|
|
|
return $translation; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return TranslationInterface[] |
86
|
|
|
*/ |
87
|
|
|
public function getTranslations() |
88
|
|
|
{ |
89
|
|
|
return $this->translations; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param TranslationInterface $translation |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public function hasTranslation(TranslationInterface $translation) |
98
|
|
|
{ |
99
|
|
|
return $this->translations->containsKey($translation->getLocale()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param TranslationInterface $translation |
104
|
|
|
*/ |
105
|
|
|
public function addTranslation(TranslationInterface $translation) |
106
|
|
|
{ |
107
|
|
|
if (!$this->translations->containsKey($translation->getLocale())) { |
108
|
|
|
$this->translations->set($translation->getLocale(), $translation); |
109
|
|
|
$translation->setTranslatable($this); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param TranslationInterface $translation |
115
|
|
|
*/ |
116
|
|
|
public function removeTranslation(TranslationInterface $translation) |
117
|
|
|
{ |
118
|
|
|
if ($this->translations->removeElement($translation)) { |
119
|
|
|
$translation->setTranslatable(null); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $currentLocale |
125
|
|
|
*/ |
126
|
|
|
public function setCurrentLocale($currentLocale) |
127
|
|
|
{ |
128
|
|
|
$this->currentLocale = $currentLocale; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $fallbackLocale |
133
|
|
|
*/ |
134
|
|
|
public function setFallbackLocale($fallbackLocale) |
135
|
|
|
{ |
136
|
|
|
$this->fallbackLocale = $fallbackLocale; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return TranslationInterface |
141
|
|
|
*/ |
142
|
|
|
abstract protected function createTranslation(); |
143
|
|
|
} |
144
|
|
|
|