diff --git a/core/includes/common.inc b/core/includes/common.inc index ece80d1..fc9c31b 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -33,6 +33,7 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Routing\GeneratorNotInitializedException; +use Drupal\Core\StringTranslation\PluralTranslatableString; use Drupal\Core\Template\Attribute; use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\Render\Element; @@ -142,11 +143,11 @@ /** * The delimiter used to split plural strings. * - * This is the ETX (End of text) character and is used as a minimal means to - * separate singular and plural variants in source and translation text. It - * was found to be the most compatible delimiter for the supported databases. + * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0. + * Use \Drupal\Core\StringTranslation\PluralTranslatableString::DELIMITER + * instead. */ -const LOCALE_PLURAL_DELIMITER = "\03"; +const LOCALE_PLURAL_DELIMITER = PluralTranslatableString::DELIMITER; /** * Prepares a 'destination' URL query parameter for use with url(). diff --git a/core/lib/Drupal/Core/StringTranslation/PluralTranslatableString.php b/core/lib/Drupal/Core/StringTranslation/PluralTranslatableString.php index cc97fe4..d21a143 100644 --- a/core/lib/Drupal/Core/StringTranslation/PluralTranslatableString.php +++ b/core/lib/Drupal/Core/StringTranslation/PluralTranslatableString.php @@ -8,6 +8,7 @@ namespace Drupal\Core\StringTranslation; use Drupal\Component\Utility\PlaceholderTrait; +use Drupal\Component\Utility\SafeMarkup; /** * A class to hold plural translatable strings. @@ -16,6 +17,15 @@ class PluralTranslatableString extends TranslationWrapper { use PlaceholderTrait; /** + * The delimiter used to split plural strings. + * + * This is the ETX (End of text) character and is used as a minimal means to + * separate singular and plural variants in source and translation text. It + * was found to be the most compatible delimiter for the supported databases. + */ + const DELIMITER = "\03"; + + /** * The item count to display. * * @var int @@ -66,9 +76,9 @@ class PluralTranslatableString extends TranslationWrapper { * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * (optional) The string translation service. */ - public function __construct($count, $singular, $plural, array $args = array(), array $options = array(), TranslationInterface $string_translation = NULL) { + public function __construct($count, $singular, $plural, array $args = [], array $options = [], TranslationInterface $string_translation = NULL) { $this->count = $count; - $translatable_string = implode(LOCALE_PLURAL_DELIMITER, array($singular, $plural)); + $translatable_string = implode(static::DELIMITER, array($singular, $plural)); parent::__construct($translatable_string, $args, $options, $string_translation); } @@ -87,13 +97,13 @@ public function __construct($count, $singular, $plural, array $args = array(), a * An associative array of replacements to make after translation. Instances * of any key in this array are replaced with the corresponding value. * Based on the first character of the key, the value is escaped and/or - * themed. See \Drupal\Component\Utility\SafeMarkup::format(). Note that you do - * not need to include @count in this array; this replacement is done + * themed. See \Drupal\Component\Utility\SafeMarkup::format(). Note that you + * do not need to include @count in this array; this replacement is done * automatically for the plural cases. * @param array $options * An associative array of additional options. See t() for allowed keys. */ - public static function createFromTranslatedString($count, $translated_string, array $args = array(), array $options = array()) { + public static function createFromTranslatedString($count, $translated_string, array $args = [], array $options = []) { $safe = TRUE; foreach (array_keys($args) as $arg_key) { // If the string has arguments that start with '!' we consider it unsafe @@ -127,7 +137,7 @@ public function render() { $arguments = $this->getArguments(); $arguments['@count'] = $this->count; - $translated_array = explode(LOCALE_PLURAL_DELIMITER, $this->translatedString); + $translated_array = explode(static::DELIMITER, $this->translatedString); if ($this->count == 1) { return $this->placeholderFormat($translated_array[0], $arguments); @@ -145,8 +155,8 @@ public function render() { } else { // If the index cannot be computed or there's no translation, use - // the second plural form as a fallback (which allows for most flexibility - // with the replaceable @count value). + // the second plural form as a fallback (which allows for most + // flexibility with the replaceable @count value). $return = $translated_array[1]; } } @@ -169,4 +179,4 @@ protected function getPluralIndex() { return -1; } -} \ No newline at end of file +} diff --git a/core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php b/core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php index 8816f63..26edb8e 100644 --- a/core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php +++ b/core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php @@ -332,7 +332,4 @@ function history_read() { return 0; } } - if (!defined('LOCALE_PLURAL_DELIMITER')) { - define('LOCALE_PLURAL_DELIMITER', "\03"); - } } diff --git a/core/modules/locale/src/Tests/LocalePluralFormatTest.php b/core/modules/locale/src/Tests/LocalePluralFormatTest.php index dccdd81..32eb3b8 100644 --- a/core/modules/locale/src/Tests/LocalePluralFormatTest.php +++ b/core/modules/locale/src/Tests/LocalePluralFormatTest.php @@ -133,10 +133,10 @@ public function testGetPluralFormat() { $expected_plural_string = str_replace('@count', $count, $plural_strings[$langcode][$expected_plural_index]); $this->assertIdentical(\Drupal::translation()->formatPlural($count, '1 hour', '@count hours', array(), array('langcode' => $langcode))->render(), $expected_plural_string, 'Plural translation of 1 hours / @count hours for count ' . $count . ' in ' . $langcode . ' is ' . $expected_plural_string); // DO NOT use translation to pass translated strings into - // PluralTranslatableString::createFromTranslatedString() this way. It is - // designed to be used with *already* translated text like settings from - // configuration. We use PHP translation here just because we have the - // expected result data in that format. + // PluralTranslatableString::createFromTranslatedString() this way. It + // is designed to be used with *already* translated text like settings + // from configuration. We use PHP translation here just because we have + // the expected result data in that format. $translated_string = \Drupal::translation()->translate('1 hour' . LOCALE_PLURAL_DELIMITER . '@count hours', array(), array('langcode' => $langcode)); $plural = PluralTranslatableString::createFromTranslatedString($count, $translated_string, array(), array('langcode' => $langcode)); $this->assertIdentical($plural->render(), $expected_plural_string); diff --git a/core/tests/Drupal/Tests/Core/Form/FormErrorHandlerTest.php b/core/tests/Drupal/Tests/Core/Form/FormErrorHandlerTest.php index 1c93e97..cd8e3b2 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormErrorHandlerTest.php +++ b/core/tests/Drupal/Tests/Core/Form/FormErrorHandlerTest.php @@ -5,7 +5,7 @@ * Contains \Drupal\Tests\Core\Form\FormErrorHandlerTest. */ -namespace Drupal\Tests\Core\Form { +namespace Drupal\Tests\Core\Form; use Drupal\Core\Form\FormState; use Drupal\Tests\UnitTestCase; @@ -130,11 +130,3 @@ public function testSetElementErrorsFromFormState() { } } - -} - -namespace { - if (!defined('LOCALE_PLURAL_DELIMITER')) { - define('LOCALE_PLURAL_DELIMITER', "\03"); - } -} \ No newline at end of file diff --git a/core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitTest.php b/core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitTest.php index c496aaf..c8000bf 100644 --- a/core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitTest.php +++ b/core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitTest.php @@ -5,7 +5,7 @@ * Contains \Drupal\Tests\Core\StringTranslation\StringTranslationTraitTest. */ -namespace Drupal\Tests\Core\StringTranslation { +namespace Drupal\Tests\Core\StringTranslation; use Drupal\Tests\UnitTestCase; @@ -64,11 +64,3 @@ public function testFormatPlural() { } } - -} - -namespace { - if (!defined('LOCALE_PLURAL_DELIMITER')) { - define('LOCALE_PLURAL_DELIMITER', "\03"); - } -} diff --git a/core/tests/Drupal/Tests/Core/StringTranslation/TranslationManagerTest.php b/core/tests/Drupal/Tests/Core/StringTranslation/TranslationManagerTest.php index 9ababde..75372a2 100644 --- a/core/tests/Drupal/Tests/Core/StringTranslation/TranslationManagerTest.php +++ b/core/tests/Drupal/Tests/Core/StringTranslation/TranslationManagerTest.php @@ -5,7 +5,7 @@ * Contains \Drupal\Tests\Core\StringTranslation\TranslationManagerTest. */ -namespace Drupal\Tests\Core\StringTranslation { +namespace Drupal\Tests\Core\StringTranslation; use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\SafeStringInterface; @@ -40,8 +40,7 @@ public function providerTestFormatPlural() { // @todo support locale_get_plural array(2, 'Singular', '@count @arg', array('@arg' => '