|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace L10nBundle\Business; |
|
4
|
|
|
|
|
5
|
|
|
use L10nBundle\Exception\ResourceNotFoundException; |
|
6
|
|
|
use L10nBundle\Manager\L10nManagerInterface; |
|
7
|
|
|
use L10nBundle\Utils\Resolver\L10nResolver; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class with all methods to retrieve L10nResources, |
|
11
|
|
|
* including fallback managment |
|
12
|
|
|
* |
|
13
|
|
|
* @author Cyril Otal |
|
14
|
|
|
*/ |
|
15
|
|
|
class L10nProvider |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var L10nManagerInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $l10nManager; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var L10nResolver |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $l10nResolver; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $defaultLocalization; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $defaultLocale; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $fallbackLocale; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $fallbackLocalization; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param L10nManagerInterface $l10nManager |
|
49
|
|
|
* @param L10nResolver $l10nResolver |
|
50
|
|
|
* @param string $fallbackLocalization |
|
51
|
|
|
* @param string $fallbackLocale |
|
52
|
|
|
*/ |
|
53
|
7 |
|
public function __construct( |
|
54
|
|
|
L10nManagerInterface $l10nManager, |
|
55
|
|
|
L10nResolver $l10nResolver, |
|
56
|
|
|
$fallbackLocalization, |
|
57
|
|
|
$fallbackLocale |
|
58
|
|
|
) { |
|
59
|
7 |
|
$this->l10nManager = $l10nManager; |
|
60
|
7 |
|
$this->l10nResolver = $l10nResolver; |
|
61
|
7 |
|
$this->fallbackLocalization = $fallbackLocalization; |
|
62
|
7 |
|
$this->fallbackLocale = $fallbackLocale; |
|
63
|
7 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function getDefaultLocalization() |
|
69
|
|
|
{ |
|
70
|
1 |
|
return $this->defaultLocalization; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $defaultLocalization |
|
75
|
|
|
* |
|
76
|
|
|
* @return L10nProvider |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public function setDefaultLocalization($defaultLocalization) |
|
79
|
|
|
{ |
|
80
|
2 |
|
$this->defaultLocalization = $defaultLocalization; |
|
81
|
|
|
|
|
82
|
2 |
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function getDefaultLocale() |
|
89
|
|
|
{ |
|
90
|
1 |
|
return $this->defaultLocale; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $defaultLocale |
|
95
|
|
|
* |
|
96
|
|
|
* @return L10nProvider |
|
97
|
|
|
*/ |
|
98
|
2 |
|
public function setDefaultLocale($defaultLocale) |
|
99
|
|
|
{ |
|
100
|
2 |
|
$this->defaultLocale = $defaultLocale; |
|
101
|
|
|
|
|
102
|
2 |
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param $fallbackLocale |
|
107
|
|
|
* |
|
108
|
|
|
* @return $this |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function setFallbackLocale($fallbackLocale) |
|
111
|
|
|
{ |
|
112
|
1 |
|
$this->fallbackLocale = $fallbackLocale; |
|
113
|
|
|
|
|
114
|
1 |
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function getFallbackLocale() |
|
121
|
|
|
{ |
|
122
|
1 |
|
return $this->fallbackLocale; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param $fallbackLocalization |
|
127
|
|
|
* |
|
128
|
|
|
* @return $this |
|
129
|
|
|
*/ |
|
130
|
1 |
|
public function setFallbackLocalization($fallbackLocalization) |
|
131
|
|
|
{ |
|
132
|
1 |
|
$this->fallbackLocalization = $fallbackLocalization; |
|
133
|
|
|
|
|
134
|
1 |
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
1 |
|
public function getFallbackLocalization() |
|
141
|
|
|
{ |
|
142
|
1 |
|
return $this->fallbackLocalization; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Return a localized value |
|
147
|
|
|
* |
|
148
|
|
|
* @param mixed $idResource |
|
149
|
|
|
* @param mixed $idLocalization |
|
150
|
|
|
* @param string $locale |
|
151
|
|
|
* |
|
152
|
|
|
* @return string |
|
153
|
|
|
* @throws ResourceNotFoundException |
|
154
|
|
|
*/ |
|
155
|
6 |
|
public function getL10n($idResource, $idLocalization = null, $locale = null) |
|
156
|
|
|
{ |
|
157
|
6 |
|
if (is_null($idLocalization)) { |
|
158
|
1 |
|
$idLocalization = $this->defaultLocalization; |
|
159
|
1 |
|
} |
|
160
|
6 |
|
if (is_null($locale)) { |
|
161
|
1 |
|
$locale = $this->defaultLocale; |
|
162
|
1 |
|
} |
|
163
|
|
|
|
|
164
|
6 |
|
$resource = $this->getResourceOrFallbackResource($idResource, $idLocalization); |
|
165
|
5 |
|
$value = $resource->getValue($locale, $this->fallbackLocale); |
|
166
|
|
|
|
|
167
|
5 |
|
return $this->l10nResolver->resolve($value); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param $idResource |
|
172
|
|
|
* @param $idLocalization |
|
173
|
|
|
* |
|
174
|
|
|
* @return \L10nBundle\Entity\L10nResource |
|
175
|
|
|
* @throws \L10nBundle\Exception\ResourceNotFoundException |
|
176
|
|
|
*/ |
|
177
|
6 |
|
protected function getResourceOrFallbackResource($idResource, $idLocalization) |
|
178
|
|
|
{ |
|
179
|
6 |
|
$resource = $this->l10nManager->getL10nResource($idResource, $idLocalization); |
|
180
|
|
|
|
|
181
|
6 |
|
if (!$resource) { |
|
182
|
2 |
|
$resource = $this->l10nManager->getL10nResource($idResource, $this->fallbackLocalization); |
|
183
|
2 |
|
if (!$resource) { |
|
184
|
1 |
|
throw new ResourceNotFoundException( |
|
185
|
1 |
|
sprintf('Resource not found for idResource %s and idLocalization %s', $idResource, $idLocalization) |
|
186
|
1 |
|
); |
|
187
|
|
|
} |
|
188
|
1 |
|
} |
|
189
|
|
|
|
|
190
|
5 |
|
return $resource; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|