Commit deba295b authored by catch's avatar catch

Issue #2600118 by aerozeppelin, anil280988, stefan.r, dawehner, andypost,...

Issue #2600118 by aerozeppelin, anil280988, stefan.r, dawehner, andypost, sdstyles, ramirez.gerardo: Return TranslationWrapper in t() and $this->t() directly
parent 7d029cb7
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Site\Settings; use Drupal\Core\Site\Settings;
use Drupal\Core\Utility\Error; use Drupal\Core\Utility\Error;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/** /**
* Minimum supported version of PHP. * Minimum supported version of PHP.
...@@ -292,7 +293,7 @@ function drupal_get_path($type, $name) { ...@@ -292,7 +293,7 @@ function drupal_get_path($type, $name) {
* @ingroup sanitization * @ingroup sanitization
*/ */
function t($string, array $args = array(), array $options = array()) { function t($string, array $args = array(), array $options = array()) {
return \Drupal::translation()->translate($string, $args, $options); return new TranslatableMarkup($string, $args, $options);
} }
/** /**
......
...@@ -71,7 +71,7 @@ trait StringTranslationTrait { ...@@ -71,7 +71,7 @@ trait StringTranslationTrait {
* @ingroup sanitization * @ingroup sanitization
*/ */
protected function t($string, array $args = array(), array $options = array()) { protected function t($string, array $args = array(), array $options = array()) {
return $this->getStringTranslation()->translate($string, $args, $options); return new TranslatableMarkup($string, $args, $options, $this->getStringTranslation());
} }
/** /**
...@@ -80,7 +80,7 @@ protected function t($string, array $args = array(), array $options = array()) { ...@@ -80,7 +80,7 @@ protected function t($string, array $args = array(), array $options = array()) {
* @see \Drupal\Core\StringTranslation\TranslationInterface::formatPlural() * @see \Drupal\Core\StringTranslation\TranslationInterface::formatPlural()
*/ */
protected function formatPlural($count, $singular, $plural, array $args = array(), array $options = array()) { protected function formatPlural($count, $singular, $plural, array $args = array(), array $options = array()) {
return $this->getStringTranslation()->formatPlural($count, $singular, $plural, $args, $options); return new PluralTranslatableMarkup($count, $singular, $plural, $args, $options, $this->getStringTranslation());
} }
/** /**
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
use Drupal\breakpoint\Breakpoint; use Drupal\breakpoint\Breakpoint;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/** /**
* @coversDefaultClass \Drupal\breakpoint\Breakpoint * @coversDefaultClass \Drupal\breakpoint\Breakpoint
...@@ -65,12 +66,8 @@ protected function setupBreakpoint() { ...@@ -65,12 +66,8 @@ protected function setupBreakpoint() {
*/ */
public function testGetLabel() { public function testGetLabel() {
$this->pluginDefinition['label'] = 'Test label'; $this->pluginDefinition['label'] = 'Test label';
$this->stringTranslation->expects($this->once())
->method('translate')
->with($this->pluginDefinition['label'], array(), array('context' => 'breakpoint'))
->will($this->returnValue('Test label translated'));
$this->setupBreakpoint(); $this->setupBreakpoint();
$this->assertEquals('Test label translated', $this->breakpoint->getLabel()); $this->assertEquals(new TranslatableMarkup('Test label', array(), array('context' => 'breakpoint'), $this->stringTranslation), $this->breakpoint->getLabel());
} }
/** /**
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/** /**
* @coversDefaultClass \Drupal\Core\Controller\TitleResolver * @coversDefaultClass \Drupal\Core\Controller\TitleResolver
...@@ -55,13 +56,7 @@ protected function setUp() { ...@@ -55,13 +56,7 @@ protected function setUp() {
public function testStaticTitle() { public function testStaticTitle() {
$request = new Request(); $request = new Request();
$route = new Route('/test-route', array('_title' => 'static title')); $route = new Route('/test-route', array('_title' => 'static title'));
$this->assertEquals(new TranslatableMarkup('static title', array(), array(), $this->translationManager), $this->titleResolver->getTitle($request, $route));
$this->translationManager->expects($this->once())
->method('translate')
->with('static title', array(), array())
->will($this->returnValue('translated title'));
$this->assertEquals('translated title', $this->titleResolver->getTitle($request, $route));
} }
/** /**
...@@ -72,13 +67,7 @@ public function testStaticTitle() { ...@@ -72,13 +67,7 @@ public function testStaticTitle() {
public function testStaticTitleWithContext() { public function testStaticTitleWithContext() {
$request = new Request(); $request = new Request();
$route = new Route('/test-route', array('_title' => 'static title', '_title_context' => 'context')); $route = new Route('/test-route', array('_title' => 'static title', '_title_context' => 'context'));
$this->assertEquals(new TranslatableMarkup('static title', array(), array('context' => 'context'), $this->translationManager), $this->titleResolver->getTitle($request, $route));
$this->translationManager->expects($this->once())
->method('translate')
->with('static title', array(), array('context' => 'context'))
->will($this->returnValue('translated title with context'));
$this->assertEquals('translated title with context', $this->titleResolver->getTitle($request, $route));
} }
/** /**
...@@ -94,20 +83,14 @@ public function testStaticTitleWithParameter($title, $expected_title) { ...@@ -94,20 +83,14 @@ public function testStaticTitleWithParameter($title, $expected_title) {
$request->attributes->set('_raw_variables', $raw_variables); $request->attributes->set('_raw_variables', $raw_variables);
$route = new Route('/test-route', array('_title' => $title)); $route = new Route('/test-route', array('_title' => $title));
$this->translationManager->expects($this->once())
->method('translate')
->with($title, $this->logicalOr($this->arrayHasKey('@test'), $this->arrayHasKey('%test'), $this->arrayHasKey('!test')), array())
->will($this->returnValue('static title value'));
$this->assertEquals($expected_title, $this->titleResolver->getTitle($request, $route)); $this->assertEquals($expected_title, $this->titleResolver->getTitle($request, $route));
} }
public function providerTestStaticTitleWithParameter() { public function providerTestStaticTitleWithParameter() {
$translation_manager = $this->getMock('\Drupal\Core\StringTranslation\TranslationInterface');
return array( return array(
array('static title @test', 'static title value'), array('static title @test', new TranslatableMarkup('static title @test', ['@test' => 'value', '%test' => 'value', '@test2' => 'value2', '%test2' => 'value2'], array(), $translation_manager)),
array('static title !test', 'static title value'), array('static title %test', new TranslatableMarkup('static title %test', ['@test' => 'value', '%test' => 'value', '@test2' => 'value2', '%test2' => 'value2'], array(), $translation_manager)),
array('static title %test', 'static title value'),
); );
} }
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/** /**
* @coversDefaultClass \Drupal\Core\Datetime\DateFormatter * @coversDefaultClass \Drupal\Core\Datetime\DateFormatter
...@@ -97,11 +98,10 @@ protected function setUp() { ...@@ -97,11 +98,10 @@ protected function setUp() {
public function testFormatInterval($interval, $granularity, $expected, $langcode = NULL) { public function testFormatInterval($interval, $granularity, $expected, $langcode = NULL) {
// Mocks a simple formatPlural implementation. // Mocks a simple formatPlural implementation.
$this->stringTranslation->expects($this->any()) $this->stringTranslation->expects($this->any())
->method('formatPlural') ->method('translateString')
->with($this->anything(), $this->anything(), $this->anything(), array(), array('langcode' => $langcode)) ->willReturnCallback(function (TranslatableMarkup $arg) {
->will($this->returnCallback(function($count, $one, $multiple) { return $arg->getUntranslatedString();
return $count == 1 ? $one : str_replace('@count', $count, $multiple); });
}));
// Check if the granularity is specified. // Check if the granularity is specified.
if ($granularity) { if ($granularity) {
...@@ -111,7 +111,7 @@ public function testFormatInterval($interval, $granularity, $expected, $langcode ...@@ -111,7 +111,7 @@ public function testFormatInterval($interval, $granularity, $expected, $langcode
$result = $this->dateFormatter->formatInterval($interval); $result = $this->dateFormatter->formatInterval($interval);
} }
$this->assertEquals($expected, $result); $this->assertEquals(new TranslatableMarkup($expected, [], ['langcode' => $langcode], $this->stringTranslation), $result);
} }
/** /**
...@@ -153,14 +153,8 @@ public function providerTestFormatInterval() { ...@@ -153,14 +153,8 @@ public function providerTestFormatInterval() {
* Tests the formatInterval method for 0 second. * Tests the formatInterval method for 0 second.
*/ */
public function testFormatIntervalZeroSecond() { public function testFormatIntervalZeroSecond() {
$this->stringTranslation->expects($this->once())
->method('translate')
->with('0 sec', array(), array('langcode' => 'xxx-lolspeak'))
->will($this->returnValue('0 sec'));
$result = $this->dateFormatter->formatInterval(0, 1, 'xxx-lolspeak'); $result = $this->dateFormatter->formatInterval(0, 1, 'xxx-lolspeak');
$this->assertEquals(new TranslatableMarkup('0 sec', array(), array('langcode' => 'xxx-lolspeak'), $this->stringTranslation), $result);
$this->assertEquals('0 sec', $result);
} }
/** /**
...@@ -264,24 +258,20 @@ public function testFormatTimeDiffSince() { ...@@ -264,24 +258,20 @@ public function testFormatTimeDiffSince() {
* @covers ::formatDiff * @covers ::formatDiff
*/ */
public function testformatDiff($expected, $max_age, $timestamp1, $timestamp2, $options = array()) { public function testformatDiff($expected, $max_age, $timestamp1, $timestamp2, $options = array()) {
// Mocks a simple translateString implementation.
// Mocks a simple formatPlural implementation.
$this->stringTranslation->expects($this->any()) $this->stringTranslation->expects($this->any())
->method('formatPlural') ->method('translateString')
->with($this->anything(), $this->anything(), $this->anything(), array(), array('langcode' => isset($options['langcode']) ? $options['langcode'] : NULL)) ->willReturnCallback(function (TranslatableMarkup $arg) {
->will($this->returnCallback(function($count, $one, $multiple) { return $arg->getUntranslatedString();
return $count == 1 ? $one : str_replace('@count', $count, $multiple); });
}));
// Mocks a simple translate implementation. if (isset($options['langcode'])) {
$this->stringTranslation->expects($this->any()) $expected_markup = new TranslatableMarkup($expected, [], ['langcode' => $options['langcode']], $this->stringTranslation);
->method('translate') }
->with($this->anything()) else {
->will($this->returnCallback(function($string, $args, $options) { $expected_markup = new TranslatableMarkup($expected, [], [], $this->stringTranslation);
return $string; }
})); $this->assertEquals($expected_markup, $this->dateFormatter->formatDiff($timestamp1, $timestamp2, $options));
$this->assertEquals($expected, $this->dateFormatter->formatDiff($timestamp1, $timestamp2, $options));
$options['return_as_object'] = TRUE; $options['return_as_object'] = TRUE;
$expected_object = new FormattedDateDiff($expected, $max_age); $expected_object = new FormattedDateDiff($expected, $max_age);
......
...@@ -311,15 +311,7 @@ public function testGetGroupLabel() { ...@@ -311,15 +311,7 @@ public function testGetGroupLabel() {
$this->assertSame($default_label, $entity_type->getGroupLabel()); $this->assertSame($default_label, $entity_type->getGroupLabel());
$default_label = new TranslatableMarkup('Other', array(), array('context' => 'Entity type group')); $default_label = new TranslatableMarkup('Other', array(), array('context' => 'Entity type group'));
$entity_type = $this->setUpEntityType([]); $entity_type = $this->setUpEntityType(array('group_label' => $default_label));
$string_translation = $this->getMock(TranslationInterface::class);
$string_translation->expects($this->atLeastOnce())
->method('translate')
->with('Other', array(), array('context' => 'Entity type group'))
->willReturn($default_label);
$entity_type->setStringTranslation($string_translation);
$this->assertSame($default_label, $entity_type->getGroupLabel()); $this->assertSame($default_label, $entity_type->getGroupLabel());
} }
......
...@@ -7,7 +7,11 @@ ...@@ -7,7 +7,11 @@
namespace Drupal\Tests\Core\StringTranslation; namespace Drupal\Tests\Core\StringTranslation;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
/** /**
* @coversDefaultClass \Drupal\Core\StringTranslation\StringTranslationTrait * @coversDefaultClass \Drupal\Core\StringTranslation\StringTranslationTrait
...@@ -35,11 +39,13 @@ class StringTranslationTraitTest extends UnitTestCase { ...@@ -35,11 +39,13 @@ class StringTranslationTraitTest extends UnitTestCase {
*/ */
protected function setUp() { protected function setUp() {
$this->translation = $this->getObjectForTrait('\Drupal\Core\StringTranslation\StringTranslationTrait'); $this->translation = $this->getObjectForTrait('\Drupal\Core\StringTranslation\StringTranslationTrait');
$stub = $this->getStringTranslationStub(); $mock = $this->prophesize(TranslationInterface::class);
$stub->expects($this->any()) $mock->translate(Argument::cetera())->shouldNotBeCalled();
->method('formatPlural') $mock->formatPlural(Argument::cetera())->shouldNotBeCalled();
->will($this->returnArgument(2)); $mock->translateString(Argument::cetera())->will(function ($args) {
$this->translation->setStringTranslation($stub); return $args[0]->getUntranslatedString();
});
$this->translation->setStringTranslation($mock->reveal());
$this->reflection = new \ReflectionClass(get_class($this->translation)); $this->reflection = new \ReflectionClass(get_class($this->translation));
} }
...@@ -50,7 +56,9 @@ public function testT() { ...@@ -50,7 +56,9 @@ public function testT() {
$method = $this->reflection->getMethod('t'); $method = $this->reflection->getMethod('t');
$method->setAccessible(TRUE); $method->setAccessible(TRUE);
$this->assertEquals('something', $method->invoke($this->translation, 'something')); $result = $method->invoke($this->translation, 'something');
$this->assertInstanceOf(TranslatableMarkup::class, $result);
$this->assertEquals('something', $result);
} }
/** /**
...@@ -60,7 +68,9 @@ public function testFormatPlural() { ...@@ -60,7 +68,9 @@ public function testFormatPlural() {
$method = $this->reflection->getMethod('formatPlural'); $method = $this->reflection->getMethod('formatPlural');
$method->setAccessible(TRUE); $method->setAccessible(TRUE);
$this->assertEquals('apples', $method->invoke($this->translation, 2, 'apple', 'apples')); $result = $method->invoke($this->translation, 2, 'apple', 'apples');
$this->assertInstanceOf(PluralTranslatableMarkup::class, $result);
$this->assertEquals('apples', $result);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment