14
14
use Symfony \Component \Translation \Translator as BaseTranslator ;
15
15
use Symfony \Component \Translation \MessageSelector ;
16
16
use Symfony \Component \DependencyInjection \ContainerInterface ;
17
- use Symfony \Component \Config \ConfigCache ;
18
17
19
18
/**
20
19
* Translator.
24
23
class Translator extends BaseTranslator
25
24
{
26
25
protected $ container ;
27
- protected $ options = array (
28
- 'cache_dir ' => null ,
29
- 'debug ' => false ,
30
- );
31
26
protected $ loaderIds ;
32
27
33
28
/**
@@ -50,14 +45,7 @@ public function __construct(ContainerInterface $container, MessageSelector $sele
50
45
$ this ->container = $ container ;
51
46
$ this ->loaderIds = $ loaderIds ;
52
47
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 );
61
49
}
62
50
63
51
/**
@@ -80,72 +68,10 @@ public function getLocale()
80
68
/**
81
69
* {@inheritdoc}
82
70
*/
83
- protected function loadCatalogue ($ locale )
71
+ protected function initializeCatalogue ($ locale )
84
72
{
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 );
149
75
}
150
76
151
77
protected function initialize ()
0 commit comments