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

[Translation] [FrameworkBundle] Translation: Optimized loading (lazy loading) of domains#2570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
Translation domain loading memory optimalisation
Fixing CSFixing CSMinor optimalisationupdated unit tests for new dumpersresolve conflict with @api tagsfix CSTranslation domain loading memory optimalisationremoved unused variablefixed conflict
  • Loading branch information
@pulzarraider
pulzarraider committedApr 12, 2012
commitbb99755b9bf664f0b232077330a35f2a34717143
30 changes: 20 additions & 10 deletionssrc/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,6 +13,7 @@

use Symfony\Component\Translation\Translator as BaseTranslator;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Config\ConfigCache;

Expand DownExpand Up@@ -75,37 +76,40 @@ public function getLocale()
/**
* {@inheritdoc}
*/
protected function loadCatalogue($locale)
protected function loadCatalogue($locale, $domain)
{
if (isset($this->catalogues[$locale])) {
if (isset($this->catalogues[$locale]) && $this->catalogues[$locale]->hasDomain($domain)) {
return;
}

if (null === $this->options['cache_dir']) {
$this->initialize();

return parent::loadCatalogue($locale);
return parent::loadCatalogue($locale, $domain);
}

$cache = new ConfigCache($this->options['cache_dir'].'/catalogue.'.$locale.'.php', $this->options['debug']);
$cache = new ConfigCache($this->options['cache_dir'].'/'.$locale.'/'.$domain.'.php', $this->options['debug']);
if (!$cache->isFresh()) {
$this->initialize();

parent::loadCatalogue($locale);
parent::loadCatalogue($locale, $domain);

$fallbackContent = '';
$current = '';
foreach ($this->computeFallbackLocales($locale) as $fallback) {
$fallbackContent .= sprintf(<<<EOF
\$catalogue%s = new MessageCatalogue('%s', %s);
\$catalogue%s = new MessageCatalogue('%s', array(
'%s' => %s
));
\$catalogue%s->addFallbackCatalogue(\$catalogue%s);


EOF
,
ucfirst($fallback),
$fallback,
var_export($this->catalogues[$fallback]->all(), true),
$domain,
var_export($this->catalogues[$fallback]->all($domain), true),
ucfirst($current),
ucfirst($fallback)
);
Expand All@@ -117,15 +121,15 @@ protected function loadCatalogue($locale)

use Symfony\Component\Translation\MessageCatalogue;

\$catalogue = new MessageCatalogue('%s', %s);
\$catalogue->add(%s, '%s');

%s
return \$catalogue;

EOF
,
$locale,
var_export($this->catalogues[$locale]->all(), true),
var_export($this->catalogues[$locale]->all($domain), true),
$domain,
$fallbackContent
);

Expand All@@ -134,6 +138,12 @@ protected function loadCatalogue($locale)
return;
}

if(!isset($this->catalogues[$locale])){
$catalogue = new MessageCatalogue($locale);
} else {
$catalogue = $this->catalogues[$locale];
}

$this->catalogues[$locale] = include $cache;
}

Expand Down
7 changes: 6 additions & 1 deletionsrc/Symfony/Component/Translation/Dumper/FileDumper.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,7 +35,12 @@ public function dump(MessageCatalogue $messages, $options = array())

// save a file for each domain
foreach ($messages->getDomains() as $domain) {
$file = $domain.'.'.$messages->getLocale().'.'.$this->getExtension();

if(!file_exists($options['path'].'/'.$messages->getLocale())){
mkdir($options['path'].'/'.$messages->getLocale());
}

$file = $messages->getLocale().'/'.$domain.'.'.$this->getExtension();
// backup
if (file_exists($options['path'].$file)) {
copy($options['path'].$file, $options['path'].'/'.$file.'~');
Expand Down
12 changes: 12 additions & 0 deletionssrc/Symfony/Component/Translation/MessageCatalogue.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,6 +63,18 @@ public function getDomains()
return array_keys($this->messages);
}

/**
* Checks if catalogue has a given domain.
*
* @param string $domain
*
* @return Boolean
*/
public function hasDomain($domain)
{
return isset($this->messages[$domain]);
}

/**
* {@inheritdoc}
*
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,8 +26,9 @@ public function testDump()
$dumper = new CsvFileDumper();
$dumperString = $dumper->dump($catalogue, array('path' => $tempDir));

$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/valid.csv'), file_get_contents($tempDir.'/messages.en.csv'));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/valid.csv'), file_get_contents($tempDir.'/en/messages.csv'));

unlink($tempDir.'/messages.en.csv');
unlink($tempDir.'/en/messages.csv');
rmdir($tempDir.'/en');
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,8 +25,9 @@ public function testDump()
$dumper = new IniFileDumper();
$dumperString = $dumper->dump($catalogue, array('path' => $tempDir));

$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.ini'), file_get_contents($tempDir.'/messages.en.ini'));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.ini'), file_get_contents($tempDir.'/en/messages.ini'));

unlink($tempDir.'/messages.en.ini');
unlink($tempDir.'/en/messages.ini');
rmdir($tempDir.'/en');
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,8 +24,9 @@ public function testDump()
$tempDir = sys_get_temp_dir();
$dumper = new MoFileDumper();
$dumperString = $dumper->dump($catalogue, array('path' => $tempDir));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.mo'), file_get_contents($tempDir.'/messages.en.mo'));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.mo'), file_get_contents($tempDir.'/en/messages.mo'));

unlink($tempDir.'/messages.en.mo');
unlink($tempDir.'/en/messages.mo');
rmdir($tempDir.'/en');
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,8 +25,9 @@ public function testDump()
$dumper = new PhpFileDumper();
$dumperString = $dumper->dump($catalogue, array('path' => $tempDir));

$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.php'), file_get_contents($tempDir.'/messages.en.php'));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.php'), file_get_contents($tempDir.'/en/messages.php'));

unlink($tempDir.'/messages.en.php');
unlink($tempDir.'/en/messages.php');
rmdir($tempDir.'/en');
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,8 +24,9 @@ public function testDump()
$tempDir = sys_get_temp_dir();
$dumper = new PoFileDumper();
$dumperString = $dumper->dump($catalogue, array('path' => $tempDir));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.po'), file_get_contents($tempDir.'/messages.en.po'));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.po'), file_get_contents($tempDir.'/en/messages.po'));

unlink($tempDir.'/messages.en.po');
unlink($tempDir.'/en/messages.po');
rmdir($tempDir.'/en');
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,8 +25,9 @@ public function testDump()
$dumper = new QtFileDumper();
$dumperString = $dumper->dump($catalogue, array('path' => $tempDir));

$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.ts'), file_get_contents($tempDir.'/resources.en.ts'));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.ts'), file_get_contents($tempDir.'/en/resources.ts'));

unlink($tempDir.'/resources.en.ts');
unlink($tempDir.'/en/resources.ts');
rmdir($tempDir.'/en');
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
<?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\Component\Translation\Tests\Dumper;

use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Dumper\XliffFileDumper;

class XliffFileDumperTest extends \PHPUnit_Framework_TestCase
{
public function testDump()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar', 'key' => ''));

$tempDir = sys_get_temp_dir();
$dumper = new XliffFileDumper();
$dumperString = $dumper->dump($catalogue, array('path' => $tempDir));

$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources-clean.xlf'), file_get_contents($tempDir.'/messages.en.xlf'));

unlink($tempDir.'/messages.en.xlf');
}
}
<?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\Component\Translation\Tests\Dumper;

use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Dumper\XliffFileDumper;

class XliffFileDumperTest extends \PHPUnit_Framework_TestCase
{
public function testDump()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar', 'key' => ''));

$tempDir = sys_get_temp_dir();
$dumper = new XliffFileDumper();
$dumperString = $dumper->dump($catalogue, array('path' => $tempDir));

$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources-clean.xlf'), file_get_contents($tempDir.'/en/messages.xlf'));

unlink($tempDir.'/en/messages.xlf');
rmdir($tempDir.'/en');
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,8 +31,9 @@ public function testDump()
$dumper = new YamlFileDumper();
$dumperString = $dumper->dump($catalogue, array('path' => $tempDir));

$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.yml'), file_get_contents($tempDir.'/messages.en.yml'));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.yml'), file_get_contents($tempDir.'/en/messages.yml'));

unlink($tempDir.'/messages.en.yml');
unlink($tempDir.'/en/messages.yml');
rmdir($tempDir.'/en');
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,6 +29,14 @@ public function testGetDomains()
$this->assertEquals(array('domain1', 'domain2'), $catalogue->getDomains());
}

public function testHasDomain()
{
$catalogue = new MessageCatalogue('en', array('domain1' => array(), 'domain2' => array()));

$this->assertTrue($catalogue->hasDomain('domain1'));
$this->assertFalse($catalogue->hasDomain('domain3'));
}

public function testAll()
{
$catalogue = new MessageCatalogue('en', $messages = array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
Expand Down
34 changes: 20 additions & 14 deletionssrc/Symfony/Component/Translation/Translator.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -121,10 +121,10 @@ public function trans($id, array $parameters = array(), $domain = 'messages', $l
$locale = $this->getLocale();
}

if (!isset($this->catalogues[$locale])) {
$this->loadCatalogue($locale);
if (!isset($this->catalogues[$locale]) || (isset($this->catalogues[$locale]) && !$this->catalogues[$locale]->hasDomain($domain))) {
$this->loadCatalogue($locale, $domain);
}

return strtr($this->catalogues[$locale]->get((string) $id, $domain), $parameters);
}

Expand All@@ -139,8 +139,8 @@ public function transChoice($id, $number, array $parameters = array(), $domain =
$locale = $this->getLocale();
}

if (!isset($this->catalogues[$locale])) {
$this->loadCatalogue($locale);
if (!isset($this->catalogues[$locale]) || (isset($this->catalogues[$locale]) && !$this->catalogues[$locale]->hasDomain($domain))) {
$this->loadCatalogue($locale, $domain);
}

$id = (string) $id;
Expand All@@ -158,33 +158,39 @@ public function transChoice($id, $number, array $parameters = array(), $domain =
return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters);
}

protected function loadCatalogue($locale)
protected function loadCatalogue($locale, $domain)
{
$this->doLoadCatalogue($locale);
$this->loadFallbackCatalogues($locale);
$this->doLoadCatalogue($locale, $domain);
$this->loadFallbackCatalogues($locale, $domain);
}

private function doLoadCatalogue($locale)
private function doLoadCatalogue($locale, $domain)
{
$this->catalogues[$locale] = new MessageCatalogue($locale);
if(!isset($this->catalogues[$locale])) {
$this->catalogues[$locale] = new MessageCatalogue($locale);
}

if (isset($this->resources[$locale])) {
foreach ($this->resources[$locale] as $resource) {
if (!isset($this->loaders[$resource[0]])) {
throw new \RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0]));
}
$this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]));

//load only required domain
if($resource[2] == $domain) {
$this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]));
}
}
}
}

private function loadFallbackCatalogues($locale)
private function loadFallbackCatalogues($locale, $domain)
{
$current = $this->catalogues[$locale];

foreach ($this->computeFallbackLocales($locale) as $fallback) {
if (!isset($this->catalogues[$fallback])) {
$this->doLoadCatalogue($fallback);
if (!isset($this->catalogues[$fallback]) || (isset($this->catalogues[$locale]) && !$this->catalogues[$locale]->hasDomain($domain))) {
$this->doLoadCatalogue($fallback, $domain);
}

$current->addFallbackCatalogue($this->catalogues[$fallback]);
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp