diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 86e12c7..094feb6 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -13,6 +13,7 @@ use Drupal\Core\Session\AccountInterface; use Drupal\Core\Site\Settings; use Drupal\Core\Utility\Error; +use Drupal\Core\StringTranslation\TranslatableMarkup; /** * Minimum supported version of PHP. @@ -292,7 +293,7 @@ function drupal_get_path($type, $name) { * @ingroup sanitization */ function t($string, array $args = array(), array $options = array()) { - return \Drupal::translation()->translate($string, $args, $options); + return new TranslatableMarkup($string, $args, $options); } /** diff --git a/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php b/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php index e67bf2b..fd91ed9 100644 --- a/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php +++ b/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php @@ -71,7 +71,7 @@ * @ingroup sanitization */ 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()) { * @see \Drupal\Core\StringTranslation\TranslationInterface::formatPlural() */ 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()); } /** diff --git a/core/modules/breakpoint/tests/src/Unit/BreakpointTest.php b/core/modules/breakpoint/tests/src/Unit/BreakpointTest.php index fea2d8f..1ea712c 100644 --- a/core/modules/breakpoint/tests/src/Unit/BreakpointTest.php +++ b/core/modules/breakpoint/tests/src/Unit/BreakpointTest.php @@ -9,6 +9,7 @@ use Drupal\breakpoint\Breakpoint; use Drupal\Tests\UnitTestCase; +use Drupal\Core\StringTranslation\TranslatableMarkup; /** * @coversDefaultClass \Drupal\breakpoint\Breakpoint @@ -65,12 +66,8 @@ protected function setupBreakpoint() { */ public function testGetLabel() { $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->assertEquals('Test label translated', $this->breakpoint->getLabel()); + $this->assertEquals(new TranslatableMarkup('Test label', array(), array('context' => 'breakpoint'), $this->stringTranslation), $this->breakpoint->getLabel()); } /** diff --git a/core/tests/Drupal/Tests/Core/Controller/TitleResolverTest.php b/core/tests/Drupal/Tests/Core/Controller/TitleResolverTest.php index 1d8c416..e2fca4b 100644 --- a/core/tests/Drupal/Tests/Core/Controller/TitleResolverTest.php +++ b/core/tests/Drupal/Tests/Core/Controller/TitleResolverTest.php @@ -12,6 +12,7 @@ use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Route; +use Drupal\Core\StringTranslation\TranslatableMarkup; /** * @coversDefaultClass \Drupal\Core\Controller\TitleResolver @@ -55,13 +56,7 @@ protected function setUp() { public function testStaticTitle() { $request = new Request(); $route = new Route('/test-route', array('_title' => 'static title')); - - $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)); + $this->assertEquals(new TranslatableMarkup('static title', array(), array(), $this->translationManager), $this->titleResolver->getTitle($request, $route)); } /** @@ -72,13 +67,7 @@ public function testStaticTitle() { public function testStaticTitleWithContext() { $request = new Request(); $route = new Route('/test-route', array('_title' => 'static title', '_title_context' => 'context')); - - $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)); + $this->assertEquals(new TranslatableMarkup('static title', array(), array('context' => 'context'), $this->translationManager), $this->titleResolver->getTitle($request, $route)); } /** @@ -94,20 +83,14 @@ public function testStaticTitleWithParameter($title, $expected_title) { $request->attributes->set('_raw_variables', $raw_variables); $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)); } public function providerTestStaticTitleWithParameter() { + $translation_manager = $this->getMock('\Drupal\Core\StringTranslation\TranslationInterface'); return array( - array('static title @test', 'static title value'), - array('static title !test', 'static title value'), - 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', new TranslatableMarkup('static title %test', ['@test' => 'value', '%test' => 'value', '@test2' => 'value2', '%test2' => 'value2'], array(), $translation_manager)), ); } diff --git a/core/tests/Drupal/Tests/Core/Datetime/DateTest.php b/core/tests/Drupal/Tests/Core/Datetime/DateTest.php index 9033e1d..d8abb20 100644 --- a/core/tests/Drupal/Tests/Core/Datetime/DateTest.php +++ b/core/tests/Drupal/Tests/Core/Datetime/DateTest.php @@ -12,6 +12,7 @@ use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Tests\UnitTestCase; use Symfony\Component\HttpFoundation\Request; +use Drupal\Core\StringTranslation\TranslatableMarkup; /** * @coversDefaultClass \Drupal\Core\Datetime\DateFormatter @@ -97,11 +98,10 @@ protected function setUp() { public function testFormatInterval($interval, $granularity, $expected, $langcode = NULL) { // Mocks a simple formatPlural implementation. $this->stringTranslation->expects($this->any()) - ->method('formatPlural') - ->with($this->anything(), $this->anything(), $this->anything(), array(), array('langcode' => $langcode)) - ->will($this->returnCallback(function($count, $one, $multiple) { - return $count == 1 ? $one : str_replace('@count', $count, $multiple); - })); + ->method('translateString') + ->willReturnCallback(function (TranslatableMarkup $arg) { + return $arg->getUntranslatedString(); + }); // Check if the granularity is specified. if ($granularity) { @@ -111,7 +111,7 @@ public function testFormatInterval($interval, $granularity, $expected, $langcode $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() { * Tests the formatInterval method for 0 second. */ 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'); - - $this->assertEquals('0 sec', $result); + $this->assertEquals(new TranslatableMarkup('0 sec', array(), array('langcode' => 'xxx-lolspeak'), $this->stringTranslation), $result); } /** @@ -267,21 +261,18 @@ public function testformatDiff($expected, $max_age, $timestamp1, $timestamp2, $o // Mocks a simple formatPlural implementation. $this->stringTranslation->expects($this->any()) - ->method('formatPlural') - ->with($this->anything(), $this->anything(), $this->anything(), array(), array('langcode' => isset($options['langcode']) ? $options['langcode'] : NULL)) - ->will($this->returnCallback(function($count, $one, $multiple) { - return $count == 1 ? $one : str_replace('@count', $count, $multiple); - })); - - // Mocks a simple translate implementation. - $this->stringTranslation->expects($this->any()) - ->method('translate') - ->with($this->anything()) - ->will($this->returnCallback(function($string, $args, $options) { - return $string; - })); + ->method('translateString') + ->willReturnCallback(function (TranslatableMarkup $arg) { + return $arg->getUntranslatedString(); + }); - $this->assertEquals($expected, $this->dateFormatter->formatDiff($timestamp1, $timestamp2, $options)); + if (isset($options['langcode'])) { + $expected_markup = new TranslatableMarkup($expected, [], ['langcode' => $options['langcode']], $this->stringTranslation); + } + else { + $expected_markup = new TranslatableMarkup($expected, [], [], $this->stringTranslation); + } + $this->assertEquals($expected_markup, $this->dateFormatter->formatDiff($timestamp1, $timestamp2, $options)); $options['return_as_object'] = TRUE; $expected_object = new FormattedDateDiff($expected, $max_age); diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php index 6fa1f9c..c970bcf 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php @@ -311,15 +311,7 @@ public function testGetGroupLabel() { $this->assertSame($default_label, $entity_type->getGroupLabel()); $default_label = new TranslatableMarkup('Other', array(), array('context' => 'Entity type group')); - $entity_type = $this->setUpEntityType([]); - - $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); - + $entity_type = $this->setUpEntityType(array('group_label' => $default_label)); $this->assertSame($default_label, $entity_type->getGroupLabel()); } diff --git a/core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitTest.php b/core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitTest.php index c8000bf..7985c8d 100644 --- a/core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitTest.php +++ b/core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitTest.php @@ -7,7 +7,11 @@ 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 Prophecy\Argument; /** * @coversDefaultClass \Drupal\Core\StringTranslation\StringTranslationTrait @@ -35,11 +39,13 @@ class StringTranslationTraitTest extends UnitTestCase { */ protected function setUp() { $this->translation = $this->getObjectForTrait('\Drupal\Core\StringTranslation\StringTranslationTrait'); - $stub = $this->getStringTranslationStub(); - $stub->expects($this->any()) - ->method('formatPlural') - ->will($this->returnArgument(2)); - $this->translation->setStringTranslation($stub); + $mock = $this->prophesize(TranslationInterface::class); + $mock->translate(Argument::cetera())->shouldNotBeCalled(); + $mock->formatPlural(Argument::cetera())->shouldNotBeCalled(); + $mock->translateString(Argument::cetera())->will(function ($args) { + return $args[0]->getUntranslatedString(); + }); + $this->translation->setStringTranslation($mock->reveal()); $this->reflection = new \ReflectionClass(get_class($this->translation)); } @@ -50,7 +56,9 @@ public function testT() { $method = $this->reflection->getMethod('t'); $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() { $method = $this->reflection->getMethod('formatPlural'); $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); } }