8000 [Translation] Add TranslatorFallbackInterface, fixes #25356 by Warxcell · Pull Request #25549 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translation] Add TranslatorFallbackInterface, fixes #25356 #25549

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 1 commit 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
[Translation] fixes #25356, add TranslatorFallbackInterface
  • Loading branch information
Warxcell authored and Bozhidar Hristov committed Dec 21, 2017
commit 6ca51c366e97bde0d64ace9d3ed442b1a8b4a73d
16 changes: 11 additions & 5 deletions src/Symfony/Component/Translation/DataCollectorTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
*/
class DataCollectorTranslator implements TranslatorInterface, TranslatorBagInterface
class DataCollectorTranslator implements TranslatorFallbackInterface, TranslatorBagInterface
{
const MESSAGE_DEFINED = 0;
const MESSAGE_MISSING = 1;
Expand Down Expand Up @@ -88,13 +88,19 @@ public function getCatalogue($locale = null)
}

/**
* Gets the fallback locales.
*
* @return array $locales The fallback locales
* {@inheritdoc}
*/
public function getFallbackLocales()
{
if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) {
$implementsFallback = $this->translator instanceof TranslatorFallbackInterface;
if ($implementsFallback || method_exists($this->translator, 'getFallbackLocales')) {
if (!$implementsFallback) {
@trigger_error(
sprintf('Having `getFallbackLocales` in %s without implementing %s is deprecated', get_class($this->translator), TranslatorFallbackInterface::class),
E_USER_DEPRECATED
);
}

return $this->translator->getFallbackLocales();
}

Expand Down
16 changes: 11 additions & 5 deletions src/Symfony/Component/Translation/LoggingTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
*/
class LoggingTranslator implements TranslatorInterface, TranslatorBagInterface
class LoggingTranslator implements TranslatorFallbackInterface, TranslatorBagInterface
{
/**
* @var TranslatorInterface|TranslatorBagInterface
Expand Down Expand Up @@ -87,13 +87,19 @@ public function getCatalogue($locale = null)
}

/**
* Gets the fallback locales.
*
* @return array $locales The fallback locales
* {@inheritdoc}
*/
public function getFallbackLocales()
{
if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) {
$implementsFallback = $this->translator instanceof TranslatorFallbackInterface;
if ($implementsFallback || method_exists($this->translator, 'getFallbackLocales')) {
if (!$implementsFallback) {
@trigger_error(
sprintf('Having `getFallbackLocales` in %s without implementing %s is deprecated', get_class($this->translator), TranslatorFallbackInterface::class),
E_USER_DEPRECATED
);
}

return $this->translator->getFallbackLocales();
}

Expand Down
10000
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
namespace Symfony\Component\Translation\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\DataCollectorTranslator;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\TranslatorFallbackInterface;

class DataCollectorTranslatorTest extends TestCase
{
Expand All @@ -31,49 +32,49 @@ public function testCollectMessages()

$expectedMessages = array();
$expectedMessages[] = array(
'id' => 'foo',
'translation' => 'foo (en)',
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_DEFINED,
'parameters' => array(),
'transChoiceNumber' => null,
'id' => 'foo',
'translation' => 'foo (en)',
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_DEFINED,
'parameters' => array(),
'transChoiceNumber' => null,
);
$expectedMessages[] = array(
'id' => 'bar',
'translation' => 'bar (fr)',
'locale' => 'fr',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
'parameters' => array(),
'transChoiceNumber' => null,
'id' => 'bar',
'translation' => 'bar (fr)',
'locale' => 'fr',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
'parameters' => array(),
'transChoiceNumber' => null,
);
$expectedMessages[] = array(
'id' => 'choice',
'translation' => 'choice',
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_MISSING,
'parameters' => array(),
'transChoiceNumber' => 0,
'id' => 'choice',
'translation' => 'choice',
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_MISSING,
'parameters' => array(),
'transChoiceNumber' => 0,
);
$expectedMessages[] = array(
'id' => 'bar_ru',
'translation' => 'bar (ru)',
'locale' => 'ru',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
'parameters' => array(),
'transChoiceNumber' => null,
'id' => 'bar_ru',
'translation' => 'bar (ru)',
'locale' => 'ru',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
'parameters' => array(),
'transChoiceNumber' => null,
);
$expectedMessages[] = array(
'id' => 'bar_ru',
'translation' => 'bar (ru)',
'locale' => 'ru',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
'parameters' => array('foo' => 'bar'),
'transChoiceNumber' => null,
'id' => 'bar_ru',
'translation' => 'bar (ru)',
'locale' => 'ru',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
'parameters' => array('foo' => 'bar'),
'transChoiceNumber' => null,
);

$this->assertEquals($expectedMessages, $collector->getCollectedMessages());
Expand All @@ -89,4 +90,49 @@ private function createCollector()

return new DataCollectorTranslator($translator);
}

public function testFallbackLocalesReturned()
{
$internalTranslator = new Translator('en');
$internalTranslator->setFallbackLocales(array('bg'));

$translator = new DataCollectorTranslator($internalTranslator);

$this->assertInstanceOf(TranslatorFallbackInterface::class, $translator);
$fallbackLocales = $translator->getFallbackLocales();
$this->assertCount(1, $fallbackLocales);
$this->assertEquals('bg', $fallbackLocales[0]);
}

public function testWrappedTranslatorWithFallbackLocalesWithoutImplementingFallbackInterface()
{
set_error_handler(
function () {
return false;
}
);
$e = error_reporting(0);
trigger_error('', E_USER_NOTICE);

$internalTranslator = new DummyTranslator('en', array('bg'));

$translator = new DataCollectorTranslator($internalTranslator);

$fallbackLocales = $translator->getFallbackLocales();
$this->assertCount(1, $fallbackLocales);
$this->assertEquals('bg', $fallbackLocales[0]);

error_reporting($e);
restore_error_handler();

$lastError = error_get_last();
unset($lastError['file'], $lastError['line']);

$expected = array(
'type' => E_USER_DEPRECATED,
'message' => sprintf('Having `getFallbackLocales` in %s without implementing %s is deprecated', get_class($internalTranslator), TranslatorFallbackInterface::class),
);

$this->assertSame($expected, $lastError);
}
}
58 changes: 58 additions & 0 deletions src/Symfony/Component/Translation/Tests/DummyTranslator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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;

use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Component\Translation\TranslatorInterface;

class DummyTranslator implements TranslatorInterface, TranslatorBagInterface
{
private $locale;
private $fallbackLocales = array();

/**
* @param $locale
* @param string[] $fallbackLocales
*/
public function __construct($locale = null, array $fallbackLocales = array())
{
$this->locale = $locale;
$this->fallbackLocales = $fallbackLocales;
}

public function getCatalogue($locale = null)
{
}

public function trans($id, array $parameters = array(), $domain = null, $locale = null)
{
}

public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
{
}

public function setLocale($locale)
{
$this->locale = $locale;
}

public function getLocale()
{
return $this->locale;
}

public function getFallbackLocales()
{
return $this->fallbackLocales;
}
}
53 changes: 51 additions & 2 deletions src/Symfony/Component/Translation/Tests/LoggingTranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
namespace Symfony\Component\Translation\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\LoggingTranslator;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\LoggingTranslator;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\TranslatorFallbackInterface;

class LoggingTranslatorTe 741A st extends TestCase
{
Expand Down Expand Up @@ -47,4 +48,52 @@ public function testTransChoiceFallbackIsLogged()
$loggableTranslator = new LoggingTranslator($translator, $logger);
$loggableTranslator->transChoice('some_message2', 10, array('%count%' => 10));
}

public function testFallbackLocalesReturned()
{
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();

$internalTranslator = new Translator('en');
$internalTranslator->setFallbackLocales(array('bg'));

$translator = new LoggingTranslator($internalTranslator, $logger);

$this->assertInstanceOf(TranslatorFallbackInterface::class, $translator);
$fallbackLocales = $translator->getFallbackLocales();
$this->assertCount(1, $fallbackLocales);
$this->assertEquals('bg', $fallbackLocales[0]);
}

public function testWrappedTranslatorWithFallbackLocalesWithoutImplementingFallbackInterface()
{
set_error_handler(
function () {
return false;
}
);
$e = error_reporting(0);
trigger_error('', E_USER_NOTICE);

$internalTranslator = new DummyTranslator('en', array('bg'));

$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
$translator = new LoggingTranslator($internalTranslator, $logger);

$fallbackLocales = $translator->getFallbackLocales();
$this->assertCount(1, $fallbackLocales);
$this->assertEquals('bg', $fallbackLocales[0]);

error_reporting($e);
restore_error_handler();

$lastError = error_get_last();
unset($lastError['file'], $lastError['line']);

$expected = array(
'type' => E_USER_DEPRECATED,
'message' => sprintf('Having `getFallbackLocales` in %s without implementing %s is deprecated', get_class($internalTranslator), TranslatorFallbackInterface::class),
);

$this->assertSame($expected, $lastError);
}
}
12 changes: 4 additions & 8 deletions src/Symfony/Component/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class Translator implements TranslatorInterface, TranslatorBagInterface
class Translator implements TranslatorFallbackInterface, TranslatorBagInterface
{
/**
* @var MessageCatalogueInterface[]
Expand Down Expand Up @@ -150,11 +150,9 @@ public function getLocale()
}

/**
* Sets the fallback locales.
* @param string[] $locales
*
* @param array $locales The fallback locales
*
* @throws InvalidArgumentException If a locale contains invalid characters
* @throws \InvalidArgumentException If a locale contains invalid characters
*/
public function setFallbackLocales(array $locales)
{
Expand All @@ -169,9 +167,7 @@ public function setFallbackLocales(array $locales)
}

/**
* Gets the fallback locales.
*
* @return array $locales The fallback locales
* {@inheritdoc}
*/
public function getFallbackLocales()
{
Expand Down
23 changes: 23 additions & 0 deletions src/Symfony/Component/Translation/TranslatorFallbackInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the fu 58BB ll copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation;

/**
* @author Bozhidar Hristov <warxcell@gmail.com>
*/
interface TranslatorFallbackInterface extends TranslatorInterface
{
/**
* @return string[] $locales
*/
public function getFallbackLocales();
}
0