diff --git a/core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php b/core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php index 8b4902a0ad75a2465a857b88b19c618b31dab4be..82b76a0da84154470d0a9b03bd869b55105d36c8 100644 --- a/core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php +++ b/core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php @@ -72,9 +72,16 @@ class TranslatableMarkup extends FormattableMarkup { * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * (optional) The string translation service. * + * @throws \InvalidArgumentException + * Exception thrown when $string is not a string. + * * @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat() */ public function __construct($string, array $arguments = array(), array $options = array(), TranslationInterface $string_translation = NULL) { + if (!is_string($string)) { + $message = $string instanceof TranslatableMarkup ? '$string ("' . $string->getUntranslatedString() . '") must be a string.' : '$string ("' . (string) $string . '") must be a string.'; + throw new \InvalidArgumentException($message); + } $this->string = $string; $this->arguments = $arguments; $this->options = $options; diff --git a/core/lib/Drupal/Core/Validation/DrupalTranslator.php b/core/lib/Drupal/Core/Validation/DrupalTranslator.php index c7db71cd34d31eec8e34019ad0e1e61130c45545..da9c11a9e4ba30db846ac8146df7ebc58f91a410 100644 --- a/core/lib/Drupal/Core/Validation/DrupalTranslator.php +++ b/core/lib/Drupal/Core/Validation/DrupalTranslator.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Validation; use Drupal\Component\Render\MarkupInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; /** * Translates strings using Drupal's translation system. @@ -27,8 +28,9 @@ class DrupalTranslator implements TranslatorInterface { * Implements \Symfony\Component\Translation\TranslatorInterface::trans(). */ public function trans($id, array $parameters = array(), $domain = NULL, $locale = NULL) { - - return t($id, $this->processParameters($parameters), $this->getOptions($domain, $locale)); + // If a TranslatableMarkup object is passed in as $id, return it since the + // message has already been translated. + return $id instanceof TranslatableMarkup ? $id : t($id, $this->processParameters($parameters), $this->getOptions($domain, $locale)); } /** diff --git a/core/modules/field/src/Tests/FormTest.php b/core/modules/field/src/Tests/FormTest.php index 9e70b3821cec0b40d76a1981fd3dd17e98584c9a..ce0001171848dc6c8e33cd525f124ef624b162a1 100644 --- a/core/modules/field/src/Tests/FormTest.php +++ b/core/modules/field/src/Tests/FormTest.php @@ -23,9 +23,11 @@ class FormTest extends FieldTestBase { /** * Modules to enable. * + * Locale is installed so that TranslatableMarkup actually does something. + * * @var array */ - public static $modules = array('node', 'field_test', 'options', 'entity_test'); + public static $modules = array('node', 'field_test', 'options', 'entity_test', 'locale'); /** * An array of values defining a field single. diff --git a/core/modules/system/tests/modules/module_test/module_test.module b/core/modules/system/tests/modules/module_test/module_test.module index cead3299013fb6d1ade04d191325cb90cfe5ab3f..ff7af5742ba0f81297b14a8eb5256b259392083c 100644 --- a/core/modules/system/tests/modules/module_test/module_test.module +++ b/core/modules/system/tests/modules/module_test/module_test.module @@ -47,7 +47,7 @@ function module_test_system_info_alter(&$info, Extension $file, $type) { } } if ($file->getName() == 'seven' && $type == 'theme') { - $info['regions']['test_region'] = t('Test region'); + $info['regions']['test_region'] = 'Test region'; } } diff --git a/core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php b/core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php index 0b243ccd5e4871ac89575e789c2ea6f10668e88e..b038fd0cee416af2d3850620b30e46780d10e128 100644 --- a/core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php +++ b/core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\Core\StringTranslation; +use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Tests\UnitTestCase; @@ -85,4 +86,27 @@ public function testToString() { $this->assertRegExp('/Exception thrown while calling __toString on a .*Mock_TranslatableMarkup_.* object in .*TranslatableMarkupTest.php on line [0-9]+: Yes you may./', $this->lastErrorMessage); } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage $string ("foo") must be a string. + * + * @covers ::__construct + */ + public function testIsStringAssertion() { + $translation = $this->getStringTranslationStub(); + new TranslatableMarkup(new TranslatableMarkup('foo', [], [], $translation)); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage $string ("foo") must be a string. + * + * @covers ::__construct + */ + public function testIsStringAssertionWithFormattableMarkup() { + $translation = $this->getStringTranslationStub(); + $formattable_string = new FormattableMarkup('@bar', ['@bar' => 'foo']); + new TranslatableMarkup($formattable_string); + } + }