Skip to content

[2.3] [Translator][FrameworkBundle] Allow remote message sources #6978

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Loader\LoaderInterface;
use Symfony\Component\Translation\Loader\RemoteLoaderInterface;

/**
* TranslationLoader loads translation messages from translation files.
Expand Down Expand Up @@ -48,6 +49,27 @@ public function addLoader($format, LoaderInterface $loader)
public function loadMessages($directory, MessageCatalogue $catalogue)
{
foreach ($this->loaders as $format => $loader) {
// load data from remote providers
if ($loader instanceof RemoteLoaderInterface) {
$resources = $loader->getRemoteResources();

foreach ($resources as $resource) {
$locales = $loader->getLocalesForResource($resource);

foreach ($locales as $locale) {
$domains = $loader->getDomainsForLocale($resource, $locale);

foreach ($domains as $domain) {
$catalogue->addCatalogue(
$loader->load($resource, $locale, $domain)
);
}
}
}

continue;
}

// load any existing translation files
$finder = new Finder();
$extension = $catalogue->getLocale().'.'.$format;
Expand Down
47 changes: 47 additions & 0 deletions src/Symfony/Component/Translation/Loader/RemoteLoaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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\Loader;

/**
* RemoteLoaderInterface is the interface implemented by all translation
* loaders, that do not use files located in project resources.
*
* @author Dariusz Górecki <darek.krk@gmail.com>
*/
interface RemoteLoaderInterface extends LoaderInterface
{
/**
* Returns array of available resources handled by this loader.
* eg: array of Entity managers, or database connections to use,
* this resources will be passed to load method.
*
* @return mixed List of resources
*/
function getRemoteResources();

/**
* Returns list of available locales in given resource.
*
* @param mixed $resource Resource to scan for message domains
* @return mixed List of found locales
*/
function getLocalesForResource($resource);

/**
* Returns list of available translation domains in given resource.
*
* @param mixed $resource Resource to scan for message domains
* @param string $locale Locale to scan for message domains
* @return mixed List of found translation domains
*/
function getDomainsForLocale($resource, $locale);
}
169 changes: 169 additions & 0 deletions src/Symfony/Component/Translation/Tests/Loader/RemoteLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?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\Loader;

use Symfony\Component\Translation\Tests\fixtures\RemoteLoader;

class RemoteLoaderTest extends \PHPUnit_Framework_TestCase
{
public function testEmptyLoader()
{
$loader = new RemoteLoader(array());

$this->assertEquals(array(), $loader->getRemoteResources());
$this->assertEquals(array(), $loader->getLocalesForResource('foo'));
$this->assertEquals(array(), $loader->getDomainsForLocale('foo', 'bar'));
$this->assertEquals(array(), $loader->load('foo', 'bar', 'baz'));
}

public function testEmptyResources()
{
$loader = new RemoteLoader(array(
'foo' => array(),
));

$this->assertEquals(array('foo'), $loader->getRemoteResources());
$this->assertEquals(array(), $loader->getLocalesForResource('foo'));
$this->assertEquals(array(), $loader->getDomainsForLocale('foo', 'bar'));
$this->assertEquals(array(), $loader->load('foo', 'bar', 'baz'));
}

public function testEmptyLocales()
{
$loader = new RemoteLoader(array(
'foo' => array(
'pl_PL' => array(),
),
));

$this->assertEquals(array('foo'), $loader->getRemoteResources());
$this->assertEquals(array('pl_PL'), $loader->getLocalesForResource('foo'));
$this->assertEquals(array(), $loader->getDomainsForLocale('foo', 'bar'));
$this->assertEquals(array(), $loader->load('foo', 'bar', 'baz'));
}

public function testEmptyDomains()
{
$loader = new RemoteLoader(array(
'foo' => array(
'pl_PL' => array(
'messages' => array(),
),
),
));

$this->assertEquals(array('foo'), $loader->getRemoteResources());
$this->assertEquals(array('pl_PL'), $loader->getLocalesForResource('foo'));
$this->assertEquals(array('messages'), $loader->getDomainsForLocale('foo', 'pl_PL'));
$this->assertEquals(array(), $loader->load('foo', 'pl_PL', 'messages'));
}

public function testLoad()
{
$loader = new RemoteLoader(array(
'foo' => array(
'pl_PL' => array(
'messages' => array(
'key1' => 'value1',
'key2' => 'value2',
),
),
),
));

$this->assertEquals(array('foo'), $loader->getRemoteResources());
$this->assertEquals(array('pl_PL'), $loader->getLocalesForResource('foo'));
$this->assertEquals(array('messages'), $loader->getDomainsForLocale('foo', 'pl_PL'));
$this->assertEquals(array('key1' => 'value1', 'key2' => 'value2'), $loader->load('foo', 'pl_PL', 'messages'));
}

public function testLoadMultipleLocales()
{
$loader = new RemoteLoader(array(
'foo' => array(
'pl_PL' => array(
'messages' => array(
'key1' => 'value1',
'key2' => 'value2',
),
),
'en_US' => array(
'messages' => array(
'key3' => 'value3',
'key4' => 'value4',
),
),
),
));

$this->assertEquals(array('foo'), $loader->getRemoteResources());
$this->assertEquals(array('pl_PL', 'en_US'), $loader->getLocalesForResource('foo'));
$this->assertEquals(array('messages'), $loader->getDomainsForLocale('foo', 'pl_PL'));
$this->assertEquals(array('messages'), $loader->getDomainsForLocale('foo', 'en_US'));
$this->assertEquals(array('key1' => 'value1', 'key2' => 'value2'), $loader->load('foo', 'pl_PL', 'messages'));
$this->assertEquals(array('key3' => 'value3', 'key4' => 'value4'), $loader->load('foo', 'en_US', 'messages'));
}

public function testLoadMultipleResources()
{
$loader = new RemoteLoader(array(
'foo' => array(
'pl_PL' => array(
'messages' => array(
'key1' => 'value1',
'key2' => 'value2',
),
),
),
'bar' => array(
'en_US' => array(
'messages' => array(
'key3' => 'value3',
'key4' => 'value4',
),
),
),
));

$this->assertEquals(array('foo', 'bar'), $loader->getRemoteResources());
$this->assertEquals(array('pl_PL'), $loader->getLocalesForResource('foo'));
$this->assertEquals(array('en_US'), $loader->getLocalesForResource('bar'));
$this->assertEquals(array('messages'), $loader->getDomainsForLocale('foo', 'pl_PL'));
$this->assertEquals(array('messages'), $loader->getDomainsForLocale('bar', 'en_US'));
$this->assertEquals(array('key1' => 'value1', 'key2' => 'value2'), $loader->load('foo', 'pl_PL', 'messages'));
$this->assertEquals(array('key3' => 'value3', 'key4' => 'value4'), $loader->load('bar', 'en_US', 'messages'));
}

public function testLoadMultipleDomains()
{
$loader = new RemoteLoader(array(
'foo' => array(
'pl_PL' => array(
'messages' => array(
'key1' => 'value1',
'key2' => 'value2',
),
'site' => array(
'key3' => 'value3',
'key4' => 'value4',
),
),
),
));

$this->assertEquals(array('foo'), $loader->getRemoteResources());
$this->assertEquals(array('pl_PL'), $loader->getLocalesForResource('foo'));
$this->assertEquals(array('messages', 'site'), $loader->getDomainsForLocale('foo', 'pl_PL'));
$this->assertEquals(array('key1' => 'value1', 'key2' => 'value2'), $loader->load('foo', 'pl_PL', 'messages'));
$this->assertEquals(array('key3' => 'value3', 'key4' => 'value4'), $loader->load('foo', 'pl_PL', 'site'));
}
}
62 changes: 62 additions & 0 deletions src/Symfony/Component/Translation/Tests/fixtures/RemoteLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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\Fixtures;

use Symfony\Component\Translation\Loader\RemoteLoaderInterface;

class RemoteLoader implements RemoteLoaderInterface
{
private $source;

public function __construct(array $source)
{
$this->source = $source;
}

public function getRemoteResources()
{
return array_keys($this->source);
}

public function getLocalesForResource($resource)
{
if (array_key_exists($resource, $this->source)) {
return array_keys($this->source[$resource]);
}

return array();
}

public function getDomainsForLocale($resource, $locale)
{
if (array_key_exists($resource, $this->source)) {
if (array_key_exists($locale, $this->source[$resource])) {
return array_keys($this->source[$resource][$locale]);
}
}

return array();
}

public function load($resource, $locale, $domain = 'messages')
{
if (array_key_exists($resource, $this->source)) {
if (array_key_exists($locale, $this->source[$resource])) {
if (array_key_exists($domain, $this->source[$resource][$locale])) {
return $this->source[$resource][$locale][$domain];
}
}
}

return array();
}
}