2011-09-27 16:17:34 +01:00
|
|
|
<?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\Bundle\FrameworkBundle\Tests\Translation;
|
|
|
|
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
|
|
|
|
use Symfony\Component\Translation\MessageCatalogue;
|
2011-12-22 18:36:46 +00:00
|
|
|
use Symfony\Component\Filesystem\Filesystem;
|
2011-09-28 15:46:33 +01:00
|
|
|
use Symfony\Component\Translation\MessageSelector;
|
2011-09-27 16:17:34 +01:00
|
|
|
|
|
|
|
class TranslatorTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
protected $tmpDir;
|
|
|
|
|
2012-03-28 14:43:52 +01:00
|
|
|
protected function setUp()
|
2011-09-27 16:17:34 +01:00
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fs = new Filesystem();
|
|
|
|
$fs->remove($dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTransWithoutCaching()
|
|
|
|
{
|
|
|
|
$translator = $this->getTranslator($this->getLoader());
|
|
|
|
$translator->setLocale('fr');
|
2011-09-28 15:46:33 +01:00
|
|
|
$translator->setFallbackLocale(array('en', 'es'));
|
2011-09-27 16:17:34 +01:00
|
|
|
|
|
|
|
$this->assertEquals('foo (FR)', $translator->trans('foo'));
|
|
|
|
$this->assertEquals('bar (EN)', $translator->trans('bar'));
|
2011-09-28 15:46:33 +01:00
|
|
|
$this->assertEquals('foobar (ES)', $translator->trans('foobar'));
|
|
|
|
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0));
|
2011-10-11 11:54:11 +01:00
|
|
|
$this->assertEquals('no translation', $translator->trans('no translation'));
|
2011-09-27 16:17:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTransWithCaching()
|
|
|
|
{
|
|
|
|
// prime the cache
|
|
|
|
$translator = $this->getTranslator($this->getLoader(), array('cache_dir' => $this->tmpDir));
|
|
|
|
$translator->setLocale('fr');
|
2011-09-28 15:46:33 +01:00
|
|
|
$translator->setFallbackLocale(array('en', 'es'));
|
2011-09-27 16:17:34 +01:00
|
|
|
|
|
|
|
$this->assertEquals('foo (FR)', $translator->trans('foo'));
|
|
|
|
$this->assertEquals('bar (EN)', $translator->trans('bar'));
|
2011-09-28 15:46:33 +01:00
|
|
|
$this->assertEquals('foobar (ES)', $translator->trans('foobar'));
|
|
|
|
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0));
|
2011-10-11 11:54:11 +01:00
|
|
|
$this->assertEquals('no translation', $translator->trans('no translation'));
|
2011-09-27 16:17:34 +01:00
|
|
|
|
|
|
|
// 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');
|
2011-09-28 15:46:33 +01:00
|
|
|
$translator->setFallbackLocale(array('en', 'es'));
|
2011-09-27 16:17:34 +01:00
|
|
|
|
|
|
|
$this->assertEquals('foo (FR)', $translator->trans('foo'));
|
|
|
|
$this->assertEquals('bar (EN)', $translator->trans('bar'));
|
2011-09-28 15:46:33 +01:00
|
|
|
$this->assertEquals('foobar (ES)', $translator->trans('foobar'));
|
|
|
|
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0));
|
2011-10-11 11:54:11 +01:00
|
|
|
$this->assertEquals('no translation', $translator->trans('no translation'));
|
2011-09-27 16:17:34 +01:00
|
|
|
}
|
|
|
|
|
2012-06-25 10:49:17 +01:00
|
|
|
public function testGetLocale()
|
|
|
|
{
|
|
|
|
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
|
|
|
|
|
|
|
|
$request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getLocale')
|
|
|
|
->will($this->returnValue('en'))
|
|
|
|
;
|
|
|
|
|
|
|
|
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
|
|
|
|
|
|
|
|
$container
|
|
|
|
->expects($this->exactly(2))
|
|
|
|
->method('isScopeActive')
|
|
|
|
->with('request')
|
|
|
|
->will($this->onConsecutiveCalls(false, true))
|
|
|
|
;
|
|
|
|
|
|
|
|
$container
|
|
|
|
->expects($this->once())
|
|
|
|
->method('has')
|
|
|
|
->with('request')
|
|
|
|
->will($this->returnValue(true))
|
|
|
|
;
|
|
|
|
|
|
|
|
$container
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('request')
|
|
|
|
->will($this->returnValue($request))
|
|
|
|
;
|
|
|
|
|
|
|
|
$translator = new Translator($container, new MessageSelector());
|
|
|
|
|
|
|
|
$this->assertNull($translator->getLocale());
|
|
|
|
$this->assertSame('en', $translator->getLocale());
|
|
|
|
}
|
|
|
|
|
2011-09-27 16:17:34 +01:00
|
|
|
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')
|
2011-09-28 15:46:33 +01:00
|
|
|
->will($this->returnValue($this->getCatalogue('fr', array(
|
|
|
|
'foo' => 'foo (FR)',
|
|
|
|
))))
|
2011-09-27 16:17:34 +01:00
|
|
|
;
|
|
|
|
$loader
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('load')
|
2011-09-28 15:46:33 +01:00
|
|
|
->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)',
|
|
|
|
))))
|
2011-09-27 16:17:34 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
return $loader;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getContainer($loader)
|
|
|
|
{
|
|
|
|
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
|
|
|
|
$container
|
|
|
|
->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->will($this->returnValue($loader))
|
|
|
|
;
|
|
|
|
|
|
|
|
return $container;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTranslator($loader, $options = array())
|
|
|
|
{
|
|
|
|
$translator = new Translator(
|
|
|
|
$this->getContainer($loader),
|
2011-09-28 15:46:33 +01:00
|
|
|
new MessageSelector(),
|
2011-12-08 07:28:11 +00:00
|
|
|
array('loader' => array('loader')),
|
2011-09-27 16:17:34 +01:00
|
|
|
$options
|
|
|
|
);
|
|
|
|
|
|
|
|
$translator->addResource('loader', 'foo', 'fr');
|
|
|
|
$translator->addResource('loader', 'foo', 'en');
|
2011-09-28 15:46:33 +01:00
|
|
|
$translator->addResource('loader', 'foo', 'es');
|
2011-09-27 16:17:34 +01:00
|
|
|
|
|
|
|
return $translator;
|
|
|
|
}
|
|
|
|
}
|