/
Translator.php
148 lines (120 loc) · 4.04 KB
/
Translator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Translation;
use Symfony\Component\Translation\Translator as BaseTranslator;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Config\ConfigCache;
/**
* Translator.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Translator extends BaseTranslator
{
protected $container;
protected $options;
protected $loaderIds;
/**
* Constructor.
*
* Available options:
*
* * cache_dir: The cache directory (or null to disable caching)
* * debug: Whether to enable debugging or not (false by default)
*
* @param ContainerInterface $container A ContainerInterface instance
* @param MessageSelector $selector The message selector for pluralization
* @param array $loaderIds An array of loader Ids
* @param array $options An array of options
*/
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array())
{
$this->container = $container;
$this->loaderIds = $loaderIds;
$this->options = array(
'cache_dir' => null,
'debug' => false,
);
// check option names
if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
throw new \InvalidArgumentException(sprintf('The Translator does not support the following options: \'%s\'.', implode('\', \'', $diff)));
}
$this->options = array_merge($this->options, $options);
parent::__construct(null, $selector);
}
/**
* {@inheritdoc}
*/
public function getLocale()
{
if (null === $this->locale && $this->container->has('request')) {
$this->locale = $this->container->get('request')->getLocale();
}
return $this->locale;
}
/**
* {@inheritdoc}
*/
protected function loadCatalogue($locale)
{
if (isset($this->catalogues[$locale])) {
return;
}
if (null === $this->options['cache_dir']) {
$this->initialize();
return parent::loadCatalogue($locale);
}
$cache = new ConfigCache($this->options['cache_dir'].'/catalogue.'.$locale.'.php', $this->options['debug']);
if (!$cache->isFresh()) {
$this->initialize();
parent::loadCatalogue($locale);
$fallbackContent = '';
$current = '';
foreach ($this->computeFallbackLocales($locale) as $fallback) {
$fallbackContent .= sprintf(<<<EOF
\$catalogue%s = new MessageCatalogue('%s', %s);
\$catalogue%s->addFallbackCatalogue(\$catalogue%s);
EOF
,
ucfirst($fallback),
$fallback,
var_export($this->catalogues[$fallback]->all(), true),
ucfirst($current),
ucfirst($fallback)
);
$current = $fallback;
}
$content = sprintf(<<<EOF
<?php
use Symfony\Component\Translation\MessageCatalogue;
\$catalogue = new MessageCatalogue('%s', %s);
%s
return \$catalogue;
EOF
,
$locale,
var_export($this->catalogues[$locale]->all(), true),
$fallbackContent
);
$cache->write($content, $this->catalogues[$locale]->getResources());
return;
}
$this->catalogues[$locale] = include $cache;
}
protected function initialize()
{
foreach ($this->loaderIds as $id => $aliases) {
foreach ($aliases as $alias) {
$this->addLoader($alias, $this->container->get($id));
}
}
}
}