|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
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 Symfony\Component\Translation; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\Translation\Loader\LoaderInterface; |
|
|
15 |
|
|
|
16 |
/** |
|
|
17 |
* Translator. |
|
|
18 |
* |
|
|
19 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
20 |
* |
|
|
21 |
* @api |
|
|
22 |
*/ |
|
|
23 |
class Translator implements TranslatorInterface |
|
|
24 |
{ |
|
|
25 |
protected $catalogues; |
|
|
26 |
protected $locale; |
|
|
27 |
private $fallbackLocale; |
|
|
28 |
private $loaders; |
|
|
29 |
private $resources; |
|
|
30 |
private $selector; |
|
|
31 |
|
|
|
32 |
/** |
|
|
33 |
* Constructor. |
|
|
34 |
* |
|
|
35 |
* @param string $locale The locale |
|
|
36 |
* @param MessageSelector $selector The message selector for pluralization |
|
|
37 |
* |
|
|
38 |
* @api |
|
|
39 |
*/ |
|
|
40 |
public function __construct($locale, MessageSelector $selector) |
|
|
41 |
{ |
|
|
42 |
$this->locale = $locale; |
|
|
43 |
$this->selector = $selector; |
|
|
44 |
$this->loaders = array(); |
|
|
45 |
$this->resources = array(); |
|
|
46 |
$this->catalogues = array(); |
|
|
47 |
} |
|
|
48 |
|
|
|
49 |
/** |
|
|
50 |
* Adds a Loader. |
|
|
51 |
* |
|
|
52 |
* @param string $format The name of the loader (@see addResource()) |
|
|
53 |
* @param LoaderInterface $loader A LoaderInterface instance |
|
|
54 |
* |
|
|
55 |
* @api |
|
|
56 |
*/ |
|
|
57 |
public function addLoader($format, LoaderInterface $loader) |
|
|
58 |
{ |
|
|
59 |
$this->loaders[$format] = $loader; |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
/** |
|
|
63 |
* Adds a Resource. |
|
|
64 |
* |
|
|
65 |
* @param string $format The name of the loader (@see addLoader()) |
|
|
66 |
* @param mixed $resource The resource name |
|
|
67 |
* @param string $locale The locale |
|
|
68 |
* @param string $domain The domain |
|
|
69 |
* |
|
|
70 |
* @api |
|
|
71 |
*/ |
|
|
72 |
public function addResource($format, $resource, $locale, $domain = 'messages') |
|
|
73 |
{ |
|
|
74 |
$this->resources[$locale][] = array($format, $resource, $domain); |
|
|
75 |
} |
|
|
76 |
|
|
|
77 |
/** |
|
|
78 |
* {@inheritdoc} |
|
|
79 |
* |
|
|
80 |
* @api |
|
|
81 |
*/ |
|
|
82 |
public function setLocale($locale) |
|
|
83 |
{ |
|
|
84 |
$this->locale = $locale; |
|
|
85 |
} |
|
|
86 |
|
|
|
87 |
/** |
|
|
88 |
* {@inheritdoc} |
|
|
89 |
* |
|
|
90 |
* @api |
|
|
91 |
*/ |
|
|
92 |
public function getLocale() |
|
|
93 |
{ |
|
|
94 |
return $this->locale; |
|
|
95 |
} |
|
|
96 |
|
|
|
97 |
/** |
|
|
98 |
* Sets the fallback locale. |
|
|
99 |
* |
|
|
100 |
* @param string $locale The fallback locale |
|
|
101 |
* |
|
|
102 |
* @api |
|
|
103 |
*/ |
|
|
104 |
public function setFallbackLocale($locale) |
|
|
105 |
{ |
|
|
106 |
// needed as the fallback locale is used to fill-in non-yet translated messages |
|
|
107 |
$this->catalogues = array(); |
|
|
108 |
|
|
|
109 |
$this->fallbackLocale = $locale; |
|
|
110 |
} |
|
|
111 |
|
|
|
112 |
/** |
|
|
113 |
* {@inheritdoc} |
|
|
114 |
* |
|
|
115 |
* @api |
|
|
116 |
*/ |
|
|
117 |
public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null) |
|
|
118 |
{ |
|
|
119 |
if (!isset($locale)) { |
|
|
120 |
$locale = $this->getLocale(); |
|
|
121 |
} |
|
|
122 |
|
|
|
123 |
if (!isset($this->catalogues[$locale])) { |
|
|
124 |
$this->loadCatalogue($locale); |
|
|
125 |
} |
|
|
126 |
|
|
|
127 |
return strtr($this->catalogues[$locale]->get((string) $id, $domain), $parameters); |
|
|
128 |
} |
|
|
129 |
|
|
|
130 |
/** |
|
|
131 |
* {@inheritdoc} |
|
|
132 |
* |
|
|
133 |
* @api |
|
|
134 |
*/ |
|
|
135 |
public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null) |
|
|
136 |
{ |
|
|
137 |
if (!isset($locale)) { |
|
|
138 |
$locale = $this->getLocale(); |
|
|
139 |
} |
|
|
140 |
|
|
|
141 |
if (!isset($this->catalogues[$locale])) { |
|
|
142 |
$this->loadCatalogue($locale); |
|
|
143 |
} |
|
|
144 |
|
|
|
145 |
return strtr($this->selector->choose($this->catalogues[$locale]->get((string) $id, $domain), (int) $number, $locale), $parameters); |
|
|
146 |
} |
|
|
147 |
|
|
|
148 |
protected function loadCatalogue($locale) |
|
|
149 |
{ |
|
|
150 |
$this->catalogues[$locale] = new MessageCatalogue($locale); |
|
|
151 |
|
|
|
152 |
if (isset($this->resources[$locale])) { |
|
|
153 |
foreach ($this->resources[$locale] as $resource) { |
|
|
154 |
if (!isset($this->loaders[$resource[0]])) { |
|
|
155 |
throw new \RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0])); |
|
|
156 |
} |
|
|
157 |
$this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2])); |
|
|
158 |
} |
|
|
159 |
} |
|
|
160 |
|
|
|
161 |
$this->optimizeCatalogue($locale); |
|
|
162 |
} |
|
|
163 |
|
|
|
164 |
private function optimizeCatalogue($locale) |
|
|
165 |
{ |
|
|
166 |
if (strlen($locale) > 3) { |
|
|
167 |
$fallback = substr($locale, 0, -strlen(strrchr($locale, '_'))); |
|
|
168 |
} else { |
|
|
169 |
$fallback = $this->fallbackLocale; |
|
|
170 |
} |
|
|
171 |
|
|
|
172 |
if (!$fallback) { |
|
|
173 |
return; |
|
|
174 |
} |
|
|
175 |
|
|
|
176 |
if (!isset($this->catalogues[$fallback])) { |
|
|
177 |
$this->loadCatalogue($fallback); |
|
|
178 |
} |
|
|
179 |
|
|
|
180 |
$this->catalogues[$locale]->addFallbackCatalogue($this->catalogues[$fallback]); |
|
|
181 |
} |
|
|
182 |
} |