[Translator] fixed merge with 2.0
This commit is contained in:
parent
885bb33791
commit
fa57912351
@ -14,6 +14,7 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Translation;
|
||||
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
|
||||
use Symfony\Component\Translation\MessageCatalogue;
|
||||
use Symfony\Component\HttpKernel\Util\Filesystem;
|
||||
use Symfony\Component\Translation\MessageSelector;
|
||||
|
||||
class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
@ -44,10 +45,12 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$translator = $this->getTranslator($this->getLoader());
|
||||
$translator->setLocale('fr');
|
||||
$translator->setFallbackLocale('en');
|
||||
$translator->setFallbackLocale(array('en', 'es'));
|
||||
|
||||
$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));
|
||||
}
|
||||
|
||||
public function testTransWithCaching()
|
||||
@ -55,19 +58,23 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
// prime the cache
|
||||
$translator = $this->getTranslator($this->getLoader(), array('cache_dir' => $this->tmpDir));
|
||||
$translator->setLocale('fr');
|
||||
$translator->setFallbackLocale('en');
|
||||
$translator->setFallbackLocale(array('en', 'es'));
|
||||
|
||||
$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));
|
||||
|
||||
// do it another time as the cache is primed now
|
||||
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
|
||||
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir));
|
||||
$translator->setLocale('fr');
|
||||
$translator->setFallbackLocale('en');
|
||||
$translator->setFallbackLocale(array('en', 'es'));
|
||||
|
||||
$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));
|
||||
}
|
||||
|
||||
protected function getCatalogue($locale, $messages)
|
||||
@ -86,12 +93,25 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
$loader
|
||||
->expects($this->at(0))
|
||||
->method('load')
|
||||
->will($this->returnValue($this->getCatalogue('fr', array('foo' => 'foo (FR)'))))
|
||||
->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)'))))
|
||||
->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)',
|
||||
))))
|
||||
;
|
||||
|
||||
return $loader;
|
||||
@ -113,13 +133,14 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$translator = new Translator(
|
||||
$this->getContainer($loader),
|
||||
$this->getMock('Symfony\Component\Translation\MessageSelector'),
|
||||
new MessageSelector(),
|
||||
array('loader' => 'loader'),
|
||||
$options
|
||||
);
|
||||
|
||||
$translator->addResource('loader', 'foo', 'fr');
|
||||
$translator->addResource('loader', 'foo', 'en');
|
||||
$translator->addResource('loader', 'foo', 'es');
|
||||
|
||||
return $translator;
|
||||
}
|
||||
|
@ -80,7 +80,7 @@ class Translator extends BaseTranslator
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doLoadCatalogue($locale)
|
||||
protected function loadCatalogue($locale)
|
||||
{
|
||||
if (isset($this->catalogues[$locale])) {
|
||||
return;
|
||||
@ -96,18 +96,25 @@ class Translator extends BaseTranslator
|
||||
if (!$cache->isFresh()) {
|
||||
$this->initialize();
|
||||
|
||||
parent::doLoadCatalogue($locale);
|
||||
parent::loadCatalogue($locale);
|
||||
|
||||
$fallbackContent = '';
|
||||
$fallback = $this->computeFallbackLocale($locale);
|
||||
if ($fallback && $fallback != $locale) {
|
||||
$fallbackContent = sprintf(<<<EOF
|
||||
\$catalogue->addFallbackCatalogue(new MessageCatalogue('%s', %s));
|
||||
$current = '';
|
||||
foreach ($this->computeFallbackLocales($locale) as $fallback) {
|
||||
$fallbackContent .= sprintf(<<<EOF
|
||||
\$catalogue%s = new MessageCatalogue('%s', %s);
|
||||
\$catalogue%s->addFallbackCatalogue(\$catalogue%s);
|
||||
|
||||
|
||||
EOF
|
||||
,
|
||||
ucfirst($fallback),
|
||||
$fallback,
|
||||
var_export($this->catalogues[$fallback]->all(), true)
|
||||
var_export($this->catalogues[$fallback]->all(), true),
|
||||
ucfirst($current),
|
||||
ucfirst($fallback)
|
||||
);
|
||||
$current = $fallback;
|
||||
}
|
||||
|
||||
$content = sprintf(<<<EOF
|
||||
@ -118,7 +125,6 @@ use Symfony\Component\Translation\MessageCatalogue;
|
||||
\$catalogue = new MessageCatalogue('%s', %s);
|
||||
|
||||
%s
|
||||
|
||||
return \$catalogue;
|
||||
|
||||
EOF
|
||||
|
@ -190,6 +190,18 @@ class MessageCatalogue implements MessageCatalogueInterface
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the fallback catalogue.
|
||||
*
|
||||
* @return MessageCatalogueInterface A MessageCatalogueInterface instance
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function getFallbackCatalogue()
|
||||
{
|
||||
return $this->fallbackCatalogue;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
|
@ -143,6 +143,15 @@ interface MessageCatalogueInterface
|
||||
*/
|
||||
function addFallbackCatalogue(MessageCatalogueInterface $catalogue);
|
||||
|
||||
/**
|
||||
* Gets the fallback catalogue.
|
||||
*
|
||||
* @return MessageCatalogueInterface A MessageCatalogueInterface instance
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
function getFallbackCatalogue();
|
||||
|
||||
/**
|
||||
* Returns an array of resources loaded to build this collection.
|
||||
*
|
||||
|
@ -145,17 +145,17 @@ class Translator implements TranslatorInterface
|
||||
|
||||
$id = (string) $id;
|
||||
|
||||
if (!$this->catalogues[$locale]->defines($id, $domain)) {
|
||||
// we will use the locale fallback
|
||||
foreach ($this->computeFallbackLocales($locale) as $fallback) {
|
||||
if ($this->catalogues[$fallback]->defines($id, $domain)) {
|
||||
$locale = $fallback;
|
||||
break;
|
||||
}
|
||||
$catalogue = $this->catalogues[$locale];
|
||||
while (!$catalogue->defines($id, $domain)) {
|
||||
if ($cat = $catalogue->getFallbackCatalogue()) {
|
||||
$catalogue = $cat;
|
||||
$locale = $catalogue->getLocale();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return strtr($this->selector->choose($this->catalogues[$locale]->get($id, $domain), (int) $number, $locale), $parameters);
|
||||
return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters);
|
||||
}
|
||||
|
||||
protected function loadCatalogue($locale)
|
||||
@ -164,7 +164,7 @@ class Translator implements TranslatorInterface
|
||||
$this->loadFallbackCatalogues($locale);
|
||||
}
|
||||
|
||||
protected function doLoadCatalogue($locale)
|
||||
private function doLoadCatalogue($locale)
|
||||
{
|
||||
$this->catalogues[$locale] = new MessageCatalogue($locale);
|
||||
|
||||
@ -192,7 +192,7 @@ class Translator implements TranslatorInterface
|
||||
}
|
||||
}
|
||||
|
||||
private function computeFallbackLocales($locale)
|
||||
protected function computeFallbackLocales($locale)
|
||||
{
|
||||
$locales = array();
|
||||
foreach ($this->fallbackLocales as $fallback) {
|
||||
@ -207,6 +207,6 @@ class Translator implements TranslatorInterface
|
||||
array_unshift($locales, substr($locale, 0, -strlen(strrchr($locale, '_'))));
|
||||
}
|
||||
|
||||
return $locales;
|
||||
return array_unique($locales);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user