Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7527f65

Browse files
committed
[WIP][Translator] move loading catalogue out of Translator.
1 parent4b71fe0 commit7527f65

File tree

7 files changed

+567
-100
lines changed

7 files changed

+567
-100
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
namespaceSymfony\Component\Translation;
13+
14+
useSymfony\Component\Config\ConfigCacheInterface;
15+
useSymfony\Component\Config\ConfigCacheFactoryInterface;
16+
useSymfony\Component\Config\ConfigCacheFactory;
17+
18+
/**
19+
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
20+
*/
21+
class CacheTranslatorBagimplements TranslatorBagInterface
22+
{
23+
/**
24+
* @var TranslatorBagInterface
25+
*/
26+
private$translatorBag;
27+
28+
/**
29+
* @var string|null
30+
*/
31+
private$cacheDir;
32+
33+
/**
34+
* @var bool
35+
*/
36+
private$debug;
37+
38+
/**
39+
* @var ConfigCacheFactoryInterface|null
40+
*/
41+
private$configCacheFactory;
42+
43+
/**
44+
* @param string|null $cacheDir The directory to use for the cache
45+
* @param bool $debug Use cache in debug mode ?
46+
* @param TranslatorBagInterface $translatorBag
47+
* @param ConfigCacheFactoryInterface $configCacheFactory
48+
*/
49+
publicfunction__construct($cacheDir,$debug =false,TranslatorBagInterface$translatorBag =null,ConfigCacheFactoryInterface$configCacheFactory =null)
50+
{
51+
$this->cacheDir =$cacheDir;
52+
$this->debug =$debug;
53+
$this->translatorBag =$translatorBag ?:newFallbackTranslatorBag();
54+
$this->configCacheFactory =$configCacheFactory ?:newConfigCacheFactory($this->debug);
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
publicfunctiongetCatalogue($locale =null)
61+
{
62+
$self =$this;// required for PHP 5.3 where "$this" cannot be use()d in anonymous functions. Change in Symfony 3.0.
63+
$cache =$this->configCacheFactory->cache($this->getCatalogueCachePath($locale),
64+
function (ConfigCacheInterface$cache)use ($self,$locale) {
65+
$self->dumpCatalogue($locale,$cache);
66+
}
67+
);
68+
69+
/* Read catalogue from cache. */
70+
returninclude$cache->getPath();
71+
}
72+
73+
/**
74+
* @internal
75+
*/
76+
publicfunctioninitializeCatalogue($locale)
77+
{
78+
unlink($this->getCatalogueCachePath($locale));
79+
80+
return$this->getCatalogue($locale);
81+
}
82+
83+
/**
84+
* This method is public because it needs to be callable from a closure in PHP 5.3. It should be made protected (or even private, if possible) in 3.0.
85+
*
86+
* @internal
87+
*/
88+
publicfunctiondumpCatalogue($locale,ConfigCacheInterface$cache)
89+
{
90+
$catalogue =$this->translatorBag->getCatalogue($locale);
91+
$fallbackContent =$this->getFallbackContent($catalogue);
92+
93+
$content =sprintf(<<<EOF
94+
<?php
95+
96+
use Symfony\Component\Translation\MessageCatalogue;
97+
98+
\$catalogue = new MessageCatalogue('%s', %s);
99+
100+
%s
101+
return\$catalogue;
102+
103+
EOF
104+
,
105+
$locale,
106+
var_export($catalogue->all(),true),
107+
$fallbackContent
108+
);
109+
110+
$cache->write($content,$catalogue->getResources());
111+
}
112+
113+
privatefunctiongetFallbackContent(MessageCatalogue$catalogue)
114+
{
115+
$fallbackContent ='';
116+
$current ='';
117+
$replacementPattern ='/[^a-z0-9_]/i';
118+
$fallbackCatalogue =$catalogue->getFallbackCatalogue();
119+
while ($fallbackCatalogue) {
120+
$fallback =$fallbackCatalogue->getLocale();
121+
$fallbackSuffix =ucfirst(preg_replace($replacementPattern,'_',$fallback));
122+
$currentSuffix =ucfirst(preg_replace($replacementPattern,'_',$current));
123+
124+
$fallbackContent .=sprintf(<<<EOF
125+
\$catalogue%s = new MessageCatalogue('%s', %s);
126+
\$catalogue%s->addFallbackCatalogue(\$catalogue%s);
127+
128+
EOF
129+
,
130+
$fallbackSuffix,
131+
$fallback,
132+
var_export($fallbackCatalogue->all(),true),
133+
$currentSuffix,
134+
$fallbackSuffix
135+
);
136+
$current =$fallbackCatalogue->getLocale();
137+
$fallbackCatalogue =$fallbackCatalogue->getFallbackCatalogue();
138+
}
139+
140+
return$fallbackContent;
141+
}
142+
143+
privatefunctiongetCatalogueCachePath($locale)
144+
{
145+
return$this->cacheDir.'/catalogue.'.$locale.'.'.sha1(serialize($this->translatorBag)).'.php';
146+
}
147+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
namespaceSymfony\Component\Translation\Loader;
13+
14+
/**
15+
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
16+
*/
17+
class ContainerAwareTranslatorBagextends TranslatorBag
18+
{
19+
/**
20+
* The container from where services are loaded.
21+
*
22+
* @var ContainerInterface
23+
*/
24+
private$container;
25+
26+
/**
27+
* @var array
28+
*/
29+
private$loaderIds;
30+
31+
/**
32+
* @param ContainerInterface $container A ContainerInterface instance
33+
* @param array $loaderIds
34+
*/
35+
publicfunction__construct(ContainerInterface$container,$loaderIds)
36+
{
37+
$this->container =$container;
38+
$this->loaderIds =$loaderIds;
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
publicfunctiongetLoaders()
45+
{
46+
foreach ($this->loaderIdsas$id =>$aliases) {
47+
foreach ($aliasesas$alias) {
48+
$this->addLoader($alias,$this->container->get($id));
49+
}
50+
}
51+
52+
returnparent::getLoaders();
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
publicfunctiongetResources($locale)
59+
{
60+
if ($this->container->hasParameter('translator_resources_'.$locale)) {
61+
return$this->container->getParameter('translator_resources_'.$locale);
62+
}
63+
64+
returnarray();
65+
}
66+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
namespaceSymfony\Component\Translation;
13+
14+
/**
15+
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
16+
*/
17+
class FallbackTranslatorBagimplements TranslatorBagInterface
18+
{
19+
private$translatorBag;
20+
21+
/**
22+
* @var array
23+
*/
24+
private$fallbackLocales =array();
25+
26+
publicfunction__construct($fallbackLocales =array(),TranslatorBagInterface$translatorBag =null)
27+
{
28+
$this->translatorBag =$translatorBag ?:newTranslatorBag();
29+
$this->fallbackLocales =$fallbackLocales;
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
publicfunctiongetCatalogue($locale =null)
36+
{
37+
$catalogue =$this->translatorBag->getCatalogue($locale);
38+
$this->loadFallbackCatalogues($catalogue,$locale);
39+
40+
return$catalogue;
41+
}
42+
43+
privatefunctionloadFallbackCatalogues($catalogue,$locale)
44+
{
45+
$current =$catalogue;
46+
47+
foreach ($this->computeFallbackLocales($locale)as$fallback) {
48+
$catalogue =$this->translatorBag->getCatalogue($fallback);
49+
50+
$current->addFallbackCatalogue($catalogue);
51+
$current =$catalogue;
52+
}
53+
}
54+
55+
privatefunctioncomputeFallbackLocales($locale)
56+
{
57+
$locales =array();
58+
foreach ($this->fallbackLocalesas$fallback) {
59+
if ($fallback ===$locale) {
60+
continue;
61+
}
62+
63+
$locales[] =$fallback;
64+
}
65+
66+
if (strrchr($locale,'_') !==false) {
67+
array_unshift($locales,substr($locale,0, -strlen(strrchr($locale,'_'))));
68+
}
69+
70+
returnarray_unique($locales);
71+
}
72+
73+
/**
74+
* Sets the fallback locales.
75+
*
76+
* @param array $locales The fallback locales
77+
*
78+
* @throws \InvalidArgumentException If a locale contains invalid characters
79+
*
80+
* @api
81+
*/
82+
publicfunctionsetFallbackLocales(array$locales)
83+
{
84+
$this->fallbackLocales =$locales;
85+
}
86+
87+
/**
88+
* Gets the fallback locales.
89+
*
90+
* @return array $locales The fallback locales
91+
*
92+
* @api
93+
*/
94+
publicfunctiongetFallbackLocales()
95+
{
96+
return$this->fallbackLocales;
97+
}
98+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
namespaceSymfony\Component\Translation\Tests;
13+
14+
useSymfony\Component\Translation\Translator;
15+
16+
/**
17+
* @group legacy
18+
*/
19+
class LegacyTranslatorCacheTestextends TranslatorCacheTest
20+
{
21+
protectedfunctiongetTranslator($locale,$debug,$loaders =array(),$resources =array(),$fallbackLocales =array())
22+
{
23+
$translator =newTranslator($locale,null,$this->tmpDir,$debug);
24+
$translator->setFallbackLocales($fallbackLocales);
25+
foreach ($loadersas$format =>$loader) {
26+
$translator->addLoader($format,$loader);
27+
}
28+
29+
foreach ($resourcesas$resource) {
30+
$translator->addResource($resource[0],$resource[1],$resource[2],isset($resource[3]) ?$resource[3] :null);
31+
}
32+
33+
return$translator;
34+
}
35+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp