|
| 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 | +namespace Symfony\Bridge\Doctrine\Tests\Translation; |
| 13 | + |
| 14 | +use Symfony\Component\Translation\Translator; |
| 15 | +use Symfony\Component\Translation\MessageCatalogue; |
| 16 | +use Symfony\Component\Translation\MessageSelector; |
| 17 | +use Symfony\Bridge\Doctrine\Translation\DoctrineMessageCache; |
| 18 | +use Doctrine\Common\Cache\ArrayCache; |
| 19 | + |
| 20 | +class TranslatorDoctrineCacheTest extends \PHPUnit_Framework_TestCase |
| 21 | +{ |
| 22 | + protected function setUp() |
| 23 | + { |
| 24 | + if (!interface_exists('Doctrine\Common\Cache\Cache')) { |
| 25 | + $this->markTestSkipped('The "Doctrine Cache" is not available'); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + public function testTrans() |
| 30 | + { |
| 31 | + $cache = new DoctrineMessageCache(new ArrayCache()); |
| 32 | + |
| 33 | + // prime the cache |
| 34 | + $translator = $this->getTranslator($this->getLoader(), $cache); |
| 35 | + $translator->setLocale('fr'); |
| 36 | + $translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin')); |
| 37 | + |
| 38 | + $this->assertEquals('foo (FR)', $translator->trans('foo')); |
| 39 | + $this->assertEquals('bar (EN)', $translator->trans('bar')); |
| 40 | + $this->assertEquals('foobar (ES)', $translator->trans('foobar')); |
| 41 | + $this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0)); |
| 42 | + $this->assertEquals('no translation', $translator->trans('no translation')); |
| 43 | + $this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo')); |
| 44 | + $this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1)); |
| 45 | + $this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz')); |
| 46 | + $this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax')); |
| 47 | + |
| 48 | + // do it another time as the cache is primed now |
| 49 | + $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); |
| 50 | + $translator = $this->getTranslator($loader, $cache); |
| 51 | + $translator->setLocale('fr'); |
| 52 | + $translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin')); |
| 53 | + |
| 54 | + $this->assertEquals('foo (FR)', $translator->trans('foo')); |
| 55 | + $this->assertEquals('bar (EN)', $translator->trans('bar')); |
| 56 | + $this->assertEquals('foobar (ES)', $translator->trans('foobar')); |
| 57 | + $this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0)); |
| 58 | + $this->assertEquals('no translation', $translator->trans('no translation')); |
| 59 | + $this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo')); |
| 60 | + $this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1)); |
| 61 | + $this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz')); |
| 62 | + $this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax')); |
| 63 | + } |
| 64 | + |
| 65 | + public function testRefreshCacheWhenResourcesChange() |
| 66 | + { |
| 67 | + // prime the cache |
| 68 | + $cache = new DoctrineMessageCache(new ArrayCache(), true); |
| 69 | + $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); |
| 70 | + $loader |
| 71 | + ->method('load') |
| 72 | + ->will($this->returnValue($this->getCatalogue('fr', array( |
| 73 | + 'foo' => 'foo A', |
| 74 | + )))) |
| 75 | + ; |
| 76 | + |
| 77 | + $translator = new Translator('fr', new MessageSelector(), $cache); |
| 78 | + $translator->setLocale('fr'); |
| 79 | + $translator->addLoader('loader', $loader); |
| 80 | + $translator->addResource('loader', 'foo', 'fr'); |
| 81 | + |
| 82 | + $this->assertEquals('foo A', $translator->trans('foo')); |
| 83 | + |
| 84 | + // add a new resource to refresh the cache |
| 85 | + $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); |
| 86 | + $loader |
| 87 | + ->method('load') |
| 88 | + ->will($this->returnValue($this->getCatalogue('fr', array( |
| 89 | + 'foo' => 'foo B', |
| 90 | + )))) |
| 91 | + ; |
| 92 | + |
| 93 | + $tra
10000
nslator = new Translator('fr', new MessageSelector(), $cache); |
| 94 | + $translator->setLocale('fr'); |
| 95 | + $translator->addLoader('loader', $loader); |
| 96 | + $translator->addResource('loader', 'bar', 'fr'); |
| 97 | + |
| 98 | + $this->assertEquals('foo B', $translator->trans('foo')); |
| 99 | + } |
| 100 | + |
| 101 | + protected function getLoader() |
| 102 | + { |
| 103 | + $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); |
| 104 | + $loader |
| 105 | + ->expects($this->at(0)) |
| 106 | + ->method('load') |
| 107 | + ->will($this->returnValue($this->getCatalogue('fr', array( |
| 108 | + 'foo' => 'foo (FR)', |
| 109 | + )))) |
| 110 | + ; |
| 111 | + $loader |
| 112 | + ->expects($this->at(1)) |
| 113 | + ->method('load') |
| 114 | + ->will($this->returnValue($this->getCatalogue('en', array( |
| 115 | + 'foo' => 'foo (EN)', |
| 116 | + 'bar' => 'bar (EN)', |
| 117 | + 'choice' => '{0} choice 0 (EN)|{1} choice 1 (EN)|]1,Inf] choice inf (EN)', |
| 118 | + )))) |
| 119 | + ; |
| 120 | + $loader |
| 121 | + ->expects($this->at(2)) |
| 122 | + ->method('load') |
| 123 | + ->will($this->returnValue($this->getCatalogue('es', array( |
| 124 | + 'foobar' => 'foobar (ES)', |
| 125 | + )))) |
| 126 | + ; |
| 127 | + $loader |
| 128 | + ->expects($this->at(3)) |
| 129 | + ->method('load') |
| 130 | + ->will($this->returnValue($this->getCatalogue('pt-PT', array( |
| 131 | + 'foobarfoo' => 'foobarfoo (PT-PT)', |
| 132 | + )))) |
| 133 | + ; |
| 134 | + $loader |
| 135 | + ->expects($this->at(4)) |
| 136 | + ->method('load') |
| 137 | + ->will($this->returnValue($this->getCatalogue('pt_BR', array( |
| 138 | + 'other choice' => '{0} other choice 0 (PT-BR)|{1} other choice 1 (PT-BR)|]1,Inf] other choice inf (PT-BR)', |
| 139 | + )))) |
| 140 | + ; |
| 141 | + $loader |
| 142 | + ->expects($this->at(5)) |
| 143 | + ->method('load') |
| 144 | + ->will($this->returnValue($this->getCatalogue('fr.UTF-8', array( |
| 145 | + 'foobarbaz' => 'foobarbaz (fr.UTF-8)', |
| 146 | + )))) |
| 147 | + ; |
| 148 | + $loader |
| 149 | + ->expects($this->at(6)) |
| 150 | + ->method('load') |
| 151 | + ->will($this->returnValue($this->getCatalogue('sr@latin', array( |
| 152 | + 'foobarbax' => 'foobarbax (sr@latin)', |
| 153 | + )))) |
| 154 | + ; |
| 155 | + |
| 156 | + return $loader; |
| 157 | + } |
| 158 | + |
| 159 | + protected function getCatalogue($locale, $messages) |
| 160 | + { |
| 161 | + $catalogue = new MessageCatalogue($locale); |
| 162 | + foreach ($messages as $key => $translation) { |
| 163 | + $catalogue->set($key, $translation); |
| 164 | + }<
D7AE
/div> |
| 165 | + |
| 166 | + return $catalogue; |
| 167 | + } |
| 168 | + |
| 169 | + public function getTranslator($loader, $cache) |
| 170 | + { |
| 171 | + $translator = new Translator('fr', new MessageSelector(), $cache); |
| 172 | + |
| 173 | + $translator->addLoader('loader', $loader); |
| 174 | + $translator->addResource('loader', 'foo', 'fr'); |
| 175 | + $translator->addResource('loader', 'foo', 'en'); |
| 176 | + $translator->addResource('loader', 'foo', 'es'); |
| 177 | + $translator->addResource('loader', 'foo', 'pt-PT'); // European Portuguese |
| 178 | + $translator->addResource('loader', 'foo', 'pt_BR'); // Brazilian Portuguese |
| 179 | + $translator->addResource('loader', 'foo', 'fr.UTF-8'); |
| 180 | + $translator->addResource('loader', 'foo', 'sr@latin'); // Latin Serbian |
| 181 | + |
| 182 | + return $translator; |
| 183 | + } |
| 184 | +} |
0 commit comments