-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle][Translation] moved cache to Translation component (new PR) #11373
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
2.6.0 | ||
----- | ||
|
||
* added possibility to cache catalogues | ||
|
||
2.5.0 | ||
----- | ||
|
||
|
231 changes: 231 additions & 0 deletions
231
src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
<?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; | ||
|
||
use Symfony\Component\Translation\Translator; | ||
use Symfony\Component\Translation\MessageCatalogue; | ||
use Symfony\Component\Translation\MessageSelector; | ||
|
||
class TranslatorCacheTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected $tmpDir; | ||
|
||
protected function setUp() | ||
{ | ||
$this->tmpDir = sys_get_temp_dir().'/sf2_translation'; | ||
$this->deleteTmpDir(); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
$this->deleteTmpDir(); | ||
} | ||
|
||
protected function deleteTmpDir() | ||
{ | ||
if (!file_exists($dir = $this->tmpDir)) { | ||
return; | ||
} | ||
|
||
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->tmpDir), \RecursiveIteratorIterator::CHILD_FIRST); | ||
foreach ($iterator as $path) { | ||
if (preg_match('#[/\\\\]\.\.?$#', $path->__toString())) { | ||
continue; | ||
} | ||
if ($path->isDir()) { | ||
rmdir($path->__toString()); | ||
} else { | ||
unlink($path->__toString()); | ||
} | ||
} | ||
rmdir($this->tmpDir); | ||
} | ||
|
||
public function testTransWithoutCaching() | ||
{ | ||
$translator = $this->getTranslator($this->getLoader()); | ||
$translator->setLocale('fr'); | ||
$translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin')); | ||
|
||
$this->assertEquals('foo (FR)', $translator->trans('foo')); | ||
$this->assertEquals('bar (EN)', $translator->trans('bar')); | ||
$this->assertEquals('foobar (ES)', $translator->trans('foobar')); | ||
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0)); | ||
$this->assertEquals('no translation', $translator->trans('no translation')); | ||
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo')); | ||
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1)); | ||
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz')); | ||
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax')); | ||
} | ||
|
||
public function testTransWithCaching() | ||
{ | ||
// prime the cache | ||
$translator = $this->getTranslator($this->getLoader(), $this->tmpDir); | ||
$translator->setLocale('fr'); | ||
$translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin')); | ||
|
||
$this->assertEquals('foo (FR)', $translator->trans('foo')); | ||
$this->assertEquals('bar (EN)', $translator->trans('bar')); | ||
$this->assertEquals('foobar (ES)', $translator->trans('foobar')); | ||
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0)); | ||
$this->assertEquals('no translation', $translator->trans('no translation')); | ||
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo')); | ||
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1)); | ||
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz')); | ||
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax')); | ||
|
||
// do it another time as the cache is primed now | ||
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); | ||
$translator = $this->getTranslator($loader, $this->tmpDir); | ||
$translator->setLocale('fr'); | ||
$translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin')); | ||
|
||
$this->assertEquals('foo (FR)', $translator->trans('foo')); | ||
$this->assertEquals('bar (EN)', $translator->trans('bar')); | ||
$this->assertEquals('foobar (ES)', $translator->trans('foobar')); | ||
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0)); | ||
$this->assertEquals('no translation', $translator->trans('no translation')); | ||
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo')); | ||
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1)); | ||
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz')); | ||
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax')); | ||
} | ||
|
||
public function testTransWithCachingWithInvalidLocale() | ||
{ | ||
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); | ||
$translator = $this->getTranslator($loader, $this->tmpDir, 'Symfony\Component\Translation\Tests\TranslatorWithInvalidLocale'); | ||
|
||
$translator->setLocale('invalid locale'); | ||
|
||
try { | ||
$translator->trans('foo'); | ||
$this->fail(); | ||
} catch (\InvalidArgumentException $e) { | ||
$this->assertFalse(file_exists($this->tmpDir.'/catalogue.invalid locale.php')); | ||
} | ||
} | ||
|
||
public function testLoadCatalogueWithCachingWithInvalidLocale() | ||
{ | ||
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); | ||
$translator = $this->getTranslator($loader, $this->tmpDir, 'Symfony\Component\Translation\Tests\TranslatorWithInvalidLocale'); | ||
|
||
try { | ||
$translator->proxyLoadCatalogue('invalid locale'); | ||
$this->fail(); | ||
} catch (\InvalidArgumentException $e) { | ||
$this->assertFalse(file_exists($this->tmpDir.'/catalogue.invalid locale.php')); | ||
} | ||
} | ||
|
||
protected function getCatalogue($locale, $messages) | ||
{ | ||
$catalogue = new MessageCatalogue($locale); | ||
foreach ($messages as $key => $translation) { | ||
$catalogue->set($key, $translation); | ||
} | ||
|
||
return $catalogue; | ||
} | ||
|
||
protected function getLoader() | ||
{ | ||
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); | ||
$loader | ||
->expects($this->at(0)) | ||
->method('load') | ||
->will($this->returnValue($this->getCatalogue('fr', array( | ||
'foo' => 'foo (FR)', | ||
)))) | ||
; | ||
$loader | ||
->expects($this->at(1)) | ||
->method('load') | ||
->will($this->returnValue($this->getCatalogue('en', array( | ||
'foo' => 'foo (EN)', | ||
'bar' => 'bar (EN)', | ||
'choice' => '{0} choice 0 (EN)|{1} choice 1 (EN)|]1,Inf] choice inf (EN)', | ||
)))) | ||
; | ||
$loader | ||
->expects($this->at(2)) | ||
->method('load') | ||
->will($this->returnValue($this->getCatalogue('es', array( | ||
'foobar' => 'foobar (ES)', | ||
)))) | ||
; | ||
$loader | ||
->expects($this->at(3)) | ||
->method('load') | ||
->will($this->returnValue($this->getCatalogue('pt-PT', array( | ||
'foobarfoo' => 'foobarfoo (PT-PT)', | ||
)))) | ||
; | ||
$loader | ||
->expects($this->at(4)) | ||
->method('load') | ||
->will($this->returnValue($this->getCatalogue('pt_BR', array( | ||
'other choice' => '{0} other choice 0 (PT-BR)|{1} other choice 1 (PT-BR)|]1,Inf] other choice inf (PT-BR)', | ||
)))) | ||
; | ||
$loader | ||
->expects($this->at(5)) | ||
->method('load') | ||
->will($this->returnValue($this->getCatalogue('fr.UTF-8', array( | ||
'foobarbaz' => 'foobarbaz (fr.UTF-8)', | ||
)))) | ||
; | ||
$loader | ||
->expects($this->at(6)) | ||
->method('load') | ||
->will($this->returnValue($this->getCatalogue('sr@latin', array( | ||
'foobarbax' => 'foobarbax (sr@latin)', | ||
)))) | ||
; | ||
|
||
return $loader; | ||
} | ||
|
||
public function getTranslator($loader, $cacheDir = null, $translatorClass = '\Symfony\Component\Translation\Translator') | ||
{ | ||
$translator = new $translatorClass('fr', new MessageSelector(), $cacheDir); | ||
|
||
$translator->addLoader('loader', $loader); | ||
$translator->addResource('loader', 'foo', 'fr'); | ||
$translator->addResource('loader', 'foo', 'en'); | ||
$translator->addResource('loader', 'foo', 'es'); | ||
$translator->addResource('loader', 'foo', 'pt-PT'); // European Portuguese | ||
$translator->addResource('loader', 'foo', 'pt_BR'); // Brazilian Portuguese | ||
$translator->addResource('loader', 'foo', 'fr.UTF-8'); | ||
$translator->addResource('loader', 'foo', 'sr@latin'); // Latin Serbian | ||
|
||
return $translator; | ||
} | ||
} | ||
|
||
class TranslatorWithInvalidLocale extends Translator | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setLocale($locale) | ||
{ | ||
$this->locale = $locale; | ||
} | ||
|
||
public function proxyLoadCatalogue($locale) | ||
{ | ||
$this->loadCatalogue($locale); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing blank line below