Skip to content

Commit f46b3f1

Browse files
aitboudadOwlyCode
authored andcommitted
[FrameworkBundle][Translation] moved cache to Translation component
[Translation][Cache] removed accessors for options.
1 parent 3fbecf7 commit f46b3f1

File tree

1 file changed

+4
-78
lines changed

1 file changed

+4
-78
lines changed

Translation/Translator.php

Lines changed: 4 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Symfony\Component\Translation\Translator as BaseTranslator;
1515
use Symfony\Component\Translation\MessageSelector;
1616
use Symfony\Component\DependencyInjection\ContainerInterface;
17-
use Symfony\Component\Config\ConfigCache;
1817

1918
/**
2019
* Translator.
@@ -24,10 +23,6 @@
2423
class Translator extends BaseTranslator
2524
{
2625
protected $container;
27-
protected $options = array(
28-
'cache_dir' => null,
29-
'debug' => false,
30-
);
3126
protected $loaderIds;
3227

3328
/**
@@ -50,14 +45,7 @@ public function __construct(ContainerInterface $container, MessageSelector $sele
5045
$this->container = $container;
5146
$this->loaderIds = $loaderIds;
5247

53-
// check option names
54-
if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
55-
throw new \InvalidArgumentException(sprintf('The Translator does not support the following options: \'%s\'.', implode('\', \'', $diff)));
56-
}
57-
58-
$this->options = array_merge($this->options, $options);
59-
60-
parent::__construct(null, $selector);
48+
parent::__construct(null, $selector, $options);
6149
}
6250

6351
/**
@@ -80,72 +68,10 @@ public function getLocale()
8068
/**
8169
* {@inheritdoc}
8270
*/
83-
protected function loadCatalogue($locale)
71+
protected function initializeCatalogue($locale)
8472
{
85-
if (isset($this->catalogues[$locale])) {
86-
return;
87-
}
88-
89-
if (null === $this->options['cache_dir']) {
90-
$this->initialize();
91-
92-
return parent::loadCatalogue($locale);
93-
}
94-
95-
$this->assertValidLocale($locale);
96-
97-
$cache = new ConfigCache($this->options['cache_dir'].'/catalogue.'.$locale.'.php', $this->options['debug']);
98-
if (!$cache->isFresh()) {
99-
$this->initialize();
100-
101-
parent::loadCatalogue($locale);
102-
103-
$fallbackContent = '';
104-
$current = '';
105-
$replacementPattern = '/[^a-z0-9_]/i';
106-
foreach ($this->computeFallbackLocales($locale) as $fallback) {
107-
$fallbackSuffix = ucfirst(preg_replace($replacementPattern, '_', $fallback));
108-
$currentSuffix = ucfirst(preg_replace($replacementPattern, '_', $current));
109-
110-
$fallbackContent .= sprintf(<<<EOF
111-
\$catalogue%s = new MessageCatalogue('%s', %s);
112-
\$catalogue%s->addFallbackCatalogue(\$catalogue%s);
113-
114-
115-
EOF
116-
,
117-
$fallbackSuffix,
118-
$fallback,
119-
var_export($this->catalogues[$fallback]->all(), true),
120-
$currentSuffix,
121-
$fallbackSuffix
122-
);
123-
$current = $fallback;
124-
}
125-
126-
$content = sprintf(<<<EOF
127-
<?php
128-
129-
use Symfony\Component\Translation\MessageCatalogue;
130-
131-
\$catalogue = new MessageCatalogue('%s', %s);
132-
133-
%s
134-
return \$catalogue;
135-
136-
EOF
137-
,
138-
$locale,
139-
var_export($this->catalogues[$locale]->all(), true),
140-
$fallbackContent
141-
);
142-
143-
$cache->write($content, $this->catalogues[$locale]->getResources());
144-
145-
return;
146-
}
147-
148-
$this->catalogues[$locale] = include $cache;
73+
$this->initialize();
74+
parent::initializeCatalogue($locale);
14975
}
15076

15177
protected function initialize()

0 commit comments

Comments
 (0)