|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\Translator; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Translation\Translator as SymfonyTranslator; |
|
6
|
|
|
|
|
7
|
|
|
use Charcoal\Translator\LocalesManager; |
|
8
|
|
|
use Charcoal\Translator\Translation; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Extends the symfony translator to allow returned values in a "Translation" oject, |
|
12
|
|
|
* containing localizations for all locales. |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
class Translator extends SymfonyTranslator |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var LocalesManager |
|
19
|
|
|
*/ |
|
20
|
|
|
private $manager; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param array $data Constructor data. |
|
24
|
|
|
* @return |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(array $data) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->setManager($data['manager']); |
|
29
|
|
|
|
|
30
|
|
|
$defaults = [ |
|
31
|
|
|
'locale' => $this->manager->currentLocale(), |
|
32
|
|
|
'message_selector' => null, |
|
33
|
|
|
'cache_dir' => null, |
|
34
|
|
|
'debug' => false |
|
35
|
|
|
]; |
|
36
|
|
|
$data = array_merge($defaults, $data); |
|
37
|
|
|
|
|
38
|
|
|
// If symfony-config is not installed, DON'T use cache. |
|
39
|
|
|
if (!class_exists('\Symfony\Component\Config\ConfigCacheFactory')) { |
|
40
|
|
|
$data['cache_dir'] = null; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
parent::__construct( |
|
44
|
|
|
$data['locale'], |
|
45
|
|
|
$data['message_selector'], |
|
46
|
|
|
$data['cache_dir'], |
|
47
|
|
|
$data['debug'] |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get a translation object from a (mixed) value. |
|
53
|
|
|
* |
|
54
|
|
|
* @param mixed $val The string or translation-object to retrieve. |
|
55
|
|
|
* @return Translation|null |
|
56
|
|
|
*/ |
|
57
|
|
|
public function translation($val) |
|
58
|
|
|
{ |
|
59
|
|
|
if ($this->isValidTranslation($val) === false) { |
|
60
|
|
|
return null; |
|
61
|
|
|
} |
|
62
|
|
|
$translation = new Translation($val, $this->manager); |
|
63
|
|
|
foreach ($this->availableLocales() as $lang) { |
|
64
|
|
|
if (!isset($translation[$lang]) || $translation[$lang] == $val) { |
|
65
|
|
|
$translation[$lang] = $this->trans($val, [], null, $lang); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
return $translation; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get a translated string from a (mixed) value. |
|
73
|
|
|
* |
|
74
|
|
|
* @param mixed $val The string or translation-object to retrieve. |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
|
|
public function translate($val) |
|
78
|
|
|
{ |
|
79
|
|
|
if (is_string($val)) { |
|
80
|
|
|
return $this->trans($val); |
|
81
|
|
|
} else { |
|
82
|
|
|
$translation = $this->translation($val); |
|
83
|
|
|
return (string)$translation; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return string[] |
|
89
|
|
|
*/ |
|
90
|
|
|
public function locales() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->manager->locales(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return string[] |
|
97
|
|
|
*/ |
|
98
|
|
|
public function availableLocales() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->manager->availableLocales(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Ensure that the `setLocale()` method also changes the locales manager's language. |
|
105
|
|
|
* |
|
106
|
|
|
* @see SymfonyTranslator::setLocale() |
|
107
|
|
|
* @param string $locale The locale ident (language) to set. |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
public function setLocale($locale) |
|
111
|
|
|
{ |
|
112
|
|
|
parent::setLocale($locale); |
|
113
|
|
|
$this->manager->setCurrentLocale($locale); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param LocalesManager $manager The Locales manager. |
|
118
|
|
|
* @return void |
|
119
|
|
|
*/ |
|
120
|
|
|
private function setManager(LocalesManager $manager) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->manager = $manager; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param mixed $val The value to be checked. |
|
127
|
|
|
* @return boolean |
|
128
|
|
|
*/ |
|
129
|
|
|
private function isValidTranslation($val) |
|
130
|
|
|
{ |
|
131
|
|
|
if ($val === null) { |
|
132
|
|
|
return false; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if (is_string($val)) { |
|
136
|
|
|
return !empty(trim($val)); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if ($val instanceof Translation) { |
|
140
|
|
|
return true; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if (is_array($val)) { |
|
144
|
|
|
return !!array_filter( |
|
145
|
|
|
$val, |
|
146
|
|
|
function ($v, $k) { |
|
147
|
|
|
if (is_string($k) && is_string($v)) { |
|
148
|
|
|
if (strlen($k) > 0) { |
|
149
|
|
|
return true; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return false; |
|
154
|
|
|
}, |
|
155
|
|
|
ARRAY_FILTER_USE_BOTH |
|
156
|
|
|
); |
|
157
|
|
|
} |
|
158
|
|
|
return false; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|