public function testGetCatalogue() { $translator = new Translator('en'); $this->assertEquals(new MessageCatalogue('en'), $translator->getCatalogue()); $translator->setLocale('fr'); $this->assertEquals(new MessageCatalogue('fr'), $translator->getCatalogue('fr')); }
/** * {@inheritdoc} */ public function getCatalogue($locale = null) { $catalogue = parent::getCatalogue($locale); foreach ($catalogue->all() as $domain => $messages) { foreach ($messages as $id => $translation) { $catalogue->set($id, $this->getTranslation($catalogue, $id, $domain), $domain); } } return $catalogue; }
/** * Executes the export by loading the catalogue and writing messages * from the defined domains to the defined file. */ public function execute() { $messages = []; /** @var MessageCatalogueInterface $catalogue */ $catalogue = $this->translator->getCatalogue($this->getLocale()); if ($this->backend) { $messages = array_merge($messages, $this->getMessagesForDomain($catalogue, self::BACKEND_DOMAIN)); } if ($this->frontend) { $messages = array_merge($messages, $this->getMessagesForDomain($catalogue, self::FRONTEND_DOMAIN)); } $this->writeMessagesFile($messages); }
/** * {@inheritdoc} */ public function getCatalogue($locale = null) { if (null === $locale) { $locale = $this->getLocale(); } else { $this->assertValidLocale($locale); } $parse = !isset($this->catalogues[$locale]); $catalogue = parent::getCatalogue($locale); if ($parse) { $this->parseCatalogue($catalogue); } return $catalogue; }
protected static function createInstance() { if (config('app.debug') && !config('trans.cache_on_debug')) { \Cache::forget('po_cache'); } return \Cache::rememberForever('po_cache', function () { $basePath = config('trans.translations_path'); $locales = config('trans.supported_locales'); $translator = new SymfonyTranslator(config('app.locale')); $translator->addLoader('po', new PoFileLoader()); $translator->setFallbackLocales([config('app.locale')]); foreach ($locales as $locale) { $path = base_path($basePath . DIRECTORY_SEPARATOR . $locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES'); $file = $path . DIRECTORY_SEPARATOR . 'messages.po'; if (file_exists($file)) { $translator->addResource('po', $file, $locale); $translator->getCatalogue($locale); } } return $translator; }); }
public function testPrimaryAndFallbackCataloguesContainTheSameMessagesRegardlessOfCaching() { /* * As a safeguard against potential BC breaks, make sure that primary and fallback * catalogues (reachable via getFallbackCatalogue()) always contain the full set of * messages provided by the loader. This must also be the case when these catalogues * are (internally) read from a cache. * * Optimizations inside the translator must not change this behaviour. */ /* * Create a translator that loads two catalogues for two different locales. * The catalogues contain distinct sets of messages. */ $translator = new Translator('a', null, $this->tmpDir); $translator->setFallbackLocales(array('b')); $translator->addLoader('array', new ArrayLoader()); $translator->addResource('array', array('foo' => 'foo (a)'), 'a'); $translator->addResource('array', array('foo' => 'foo (b)'), 'b'); $translator->addResource('array', array('bar' => 'bar (b)'), 'b'); $catalogue = $translator->getCatalogue('a'); $this->assertFalse($catalogue->defines('bar')); // Sure, the "a" catalogue does not contain that message. $fallback = $catalogue->getFallbackCatalogue(); $this->assertTrue($fallback->defines('foo')); // "foo" is present in "a" and "b" /* * Now, repeat the same test. * Behind the scenes, the cache is used. But that should not matter, right? */ $translator = new Translator('a', null, $this->tmpDir); $translator->setFallbackLocales(array('b')); $translator->addLoader('array', new ArrayLoader()); $translator->addResource('array', array('foo' => 'foo (a)'), 'a'); $translator->addResource('array', array('foo' => 'foo (b)'), 'b'); $translator->addResource('array', array('bar' => 'bar (b)'), 'b'); $catalogue = $translator->getCatalogue('a'); $this->assertFalse($catalogue->defines('bar')); $fallback = $catalogue->getFallbackCatalogue(); $this->assertTrue($fallback->defines('foo')); }
public function testGetCatalogueReturnsConsolidatedCatalogue() { /* * This will be useful once we refactor so that different domains will be loaded lazily (on-demand). * In that case, getCatalogue() will probably have to load all missing domains in order to return * one complete catalogue. */ $locale = 'whatever'; $translator = new Translator($locale); $translator->addLoader('loader-a', new ArrayLoader()); $translator->addLoader('loader-b', new ArrayLoader()); $translator->addResource('loader-a', array('foo' => 'foofoo'), $locale, 'domain-a'); $translator->addResource('loader-b', array('bar' => 'foobar'), $locale, 'domain-b'); /* * Test that we get a single catalogue comprising messages * from different loaders and different domains */ $catalogue = $translator->getCatalogue($locale); $this->assertTrue($catalogue->defines('foo', 'domain-a')); $this->assertTrue($catalogue->defines('bar', 'domain-b')); }
/** * Creates a new translator instance * * @return SymfonyTranslator */ protected function createTranslator() { $translator = new SymfonyTranslator($this->getLocale()); $translator->addLoader('po', new PoFileLoader()); $file = $this->fileSystem->makePOFilePath($this->getLocale(), $this->getDomain()); $translator->addResource('po', $file, $this->getLocale(), $this->getDomain()); $translator->getCatalogue($this->getLocale()); return $translator; }
public function testFallbackCatalogueResources() { $translator = new Translator('en_GB', new MessageSelector()); $translator->addLoader('yml', new \Symfony\Component\Translation\Loader\YamlFileLoader()); $translator->addResource('yml', __DIR__ . '/fixtures/empty.yml', 'en_GB'); $translator->addResource('yml', __DIR__ . '/fixtures/resources.yml', 'en'); // force catalogue loading $this->assertEquals('bar', $translator->trans('foo', array())); $resources = $translator->getCatalogue('en')->getResources(); $this->assertCount(1, $resources); $this->assertContains(__DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'resources.yml', $resources); $resources = $translator->getCatalogue('en_GB')->getResources(); $this->assertCount(2, $resources); $this->assertContains(__DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'empty.yml', $resources); $this->assertContains(__DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'resources.yml', $resources); }