Commit 0a6e4639 authored by alexpott's avatar alexpott

Issue #2911606 by andypost, jludwig, msankhala, brathbone, imalabya,...

Issue #2911606 by andypost, jludwig, msankhala, brathbone, imalabya, RajeevChoudhary, benjifisher: Replace usage of the deprecated LOCALE_PLURAL_DELIMITER constant
parent bcfce36c
......@@ -8,6 +8,7 @@
* a cached page are instead located in bootstrap.inc.
*/
use Drupal\Component\Gettext\PoItem;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Bytes;
use Drupal\Component\Utility\Html;
......@@ -18,7 +19,6 @@
use Drupal\Core\Render\Element\Link;
use Drupal\Core\Render\Markup;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\Element;
......@@ -127,10 +127,9 @@
* The delimiter used to split plural strings.
*
* @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
* Use \Drupal\Core\StringTranslation\PluralTranslatableMarkup::DELIMITER
* instead.
* Use Drupal\Component\Gettext\PoItem::DELIMITER instead.
*/
const LOCALE_PLURAL_DELIMITER = PluralTranslatableMarkup::DELIMITER;
const LOCALE_PLURAL_DELIMITER = PoItem::DELIMITER;
/**
* Prepares a 'destination' URL query parameter.
......
......@@ -10,6 +10,15 @@
*/
class PoItem {
/**
* 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 language code this translation is in.
*
......@@ -186,9 +195,9 @@ public function setFromArray(array $values = []) {
$this->setComment($values['comment']);
}
if (isset($this->source) &&
strpos($this->source, LOCALE_PLURAL_DELIMITER) !== FALSE) {
$this->setSource(explode(LOCALE_PLURAL_DELIMITER, $this->source));
$this->setTranslation(explode(LOCALE_PLURAL_DELIMITER, $this->translation));
strpos($this->source, self::DELIMITER) !== FALSE) {
$this->setSource(explode(self::DELIMITER, $this->source));
$this->setTranslation(explode(self::DELIMITER, $this->translation));
$this->setPlural(count($this->source) > 1);
}
}
......
......@@ -26,8 +26,8 @@ public function __construct() {
*/
public function writeItem(PoItem $item) {
if (is_array($item->getSource())) {
$item->setSource(implode(LOCALE_PLURAL_DELIMITER, $item->getSource()));
$item->setTranslation(implode(LOCALE_PLURAL_DELIMITER, $item->getTranslation()));
$item->setSource(implode(PoItem::DELIMITER, $item->getSource()));
$item->setTranslation(implode(PoItem::DELIMITER, $item->getTranslation()));
}
$context = $item->getContext();
$this->items[$context != NULL ? $context : ''][$item->getSource()] = $item->getTranslation();
......
......@@ -2,6 +2,8 @@
namespace Drupal\Core\StringTranslation;
use Drupal\Component\Gettext\PoItem;
/**
* A class to hold plural translatable markup.
*/
......@@ -13,8 +15,11 @@ class PluralTranslatableMarkup extends TranslatableMarkup {
* 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.7.x, will be removed before Drupal 9.0.0.
* Use Drupal\Component\Gettext\PoItem::DELIMITER instead.
*/
const DELIMITER = "\03";
const DELIMITER = PoItem::DELIMITER;
/**
* The item count to display.
......@@ -62,7 +67,7 @@ class PluralTranslatableMarkup extends TranslatableMarkup {
*/
public function __construct($count, $singular, $plural, array $args = [], array $options = [], TranslationInterface $string_translation = NULL) {
$this->count = $count;
$translatable_string = implode(static::DELIMITER, [$singular, $plural]);
$translatable_string = implode(PoItem::DELIMITER, [$singular, $plural]);
parent::__construct($translatable_string, $args, $options, $string_translation);
}
......@@ -112,7 +117,7 @@ public function render() {
$arguments = $this->getArguments();
$arguments['@count'] = $this->count;
$translated_array = explode(static::DELIMITER, $this->translatedString);
$translated_array = explode(PoItem::DELIMITER, $this->translatedString);
if ($this->count == 1) {
return $this->placeholderFormat($translated_array[0], $arguments);
......
......@@ -2,6 +2,7 @@
namespace Drupal\config_translation\FormElement;
use Drupal\Component\Gettext\PoItem;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Config\Config;
use Drupal\Core\Language\LanguageInterface;
......@@ -17,7 +18,7 @@ class PluralVariants extends FormElementBase {
*/
protected function getSourceElement(LanguageInterface $source_language, $source_config) {
$plurals = $this->getNumberOfPlurals($source_language->getId());
$values = explode(LOCALE_PLURAL_DELIMITER, $source_config);
$values = explode(PoItem::DELIMITER, $source_config);
$element = [
'#type' => 'fieldset',
'#title' => new FormattableMarkup('@label <span class="visually-hidden">(@source_language)</span>', [
......@@ -46,7 +47,7 @@ protected function getSourceElement(LanguageInterface $source_language, $source_
*/
protected function getTranslationElement(LanguageInterface $translation_language, $source_config, $translation_config) {
$plurals = $this->getNumberOfPlurals($translation_language->getId());
$values = explode(LOCALE_PLURAL_DELIMITER, $translation_config);
$values = explode(PoItem::DELIMITER, $translation_config);
$element = [
'#type' => 'fieldset',
'#title' => new FormattableMarkup('@label <span class="visually-hidden">(@translation_language)</span>', [
......@@ -72,7 +73,7 @@ protected function getTranslationElement(LanguageInterface $translation_language
* {@inheritdoc}
*/
public function setConfig(Config $base_config, LanguageConfigOverride $config_translation, $config_values, $base_key = NULL) {
$config_values = implode(LOCALE_PLURAL_DELIMITER, $config_values);
$config_values = implode(PoItem::DELIMITER, $config_values);
parent::setConfig($base_config, $config_translation, $config_values, $base_key);
}
......
......@@ -10,6 +10,7 @@
* object files are supported.
*/
use Drupal\Component\Gettext\PoItem;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\UrlHelper;
......@@ -1184,7 +1185,7 @@ function _locale_parse_js_file($filepath) {
// Add string from Drupal.formatPlural().
foreach ($plural_matches[1] as $key => $string) {
$matches[] = [
'source' => _locale_strip_quotes($string) . LOCALE_PLURAL_DELIMITER . _locale_strip_quotes($plural_matches[2][$key]),
'source' => _locale_strip_quotes($string) . PoItem::DELIMITER . _locale_strip_quotes($plural_matches[2][$key]),
'context' => _locale_strip_quotes($plural_matches[3][$key]),
];
}
......
......@@ -2,6 +2,7 @@
namespace Drupal\locale\Form;
use Drupal\Component\Gettext\PoItem;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\locale\SourceString;
......@@ -186,7 +187,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
// Plural translations are saved in a delimited string. To be able to
// compare the new strings with the existing strings a string in the same
// format is created.
$new_translation_string_delimited = implode(LOCALE_PLURAL_DELIMITER, $new_translation['translations']);
$new_translation_string_delimited = implode(PoItem::DELIMITER, $new_translation['translations']);
// Generate an imploded string without delimiter, to be able to run
// empty() on it.
......
......@@ -2,12 +2,12 @@
namespace Drupal\locale;
use Drupal\Component\Gettext\PoItem;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\CacheCollector;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Symfony\Component\HttpFoundation\RequestStack;
/**
......@@ -173,7 +173,7 @@ protected function resolveCacheMiss($offset) {
}
}
if (is_string($value) && strpos($value, PluralTranslatableMarkup::DELIMITER) !== FALSE) {
if (is_string($value) && strpos($value, PoItem::DELIMITER) !== FALSE) {
// Community translations imported from localize.drupal.org as well as
// migrated translations may contain @count[number].
$value = preg_replace('!@count\[\d+\]!', '@count', $value);
......
......@@ -188,8 +188,8 @@ public function setHeader(PoHeader $header) {
*/
public function writeItem(PoItem $item) {
if ($item->isPlural()) {
$item->setSource(implode(LOCALE_PLURAL_DELIMITER, $item->getSource()));
$item->setTranslation(implode(LOCALE_PLURAL_DELIMITER, $item->getTranslation()));
$item->setSource(implode(PoItem::DELIMITER, $item->getSource()));
$item->setTranslation(implode(PoItem::DELIMITER, $item->getTranslation()));
}
$this->importString($item);
}
......
......@@ -2,6 +2,8 @@
namespace Drupal\locale;
use Drupal\Component\Gettext\PoItem;
/**
* Defines the locale string base class.
*
......@@ -95,14 +97,14 @@ public function setVersion($version) {
* {@inheritdoc}
*/
public function getPlurals() {
return explode(LOCALE_PLURAL_DELIMITER, $this->getString());
return explode(PoItem::DELIMITER, $this->getString());
}
/**
* {@inheritdoc}
*/
public function setPlurals($plurals) {
$this->setString(implode(LOCALE_PLURAL_DELIMITER, $plurals));
$this->setString(implode(PoItem::DELIMITER, $plurals));
return $this;
}
......
......@@ -72,7 +72,8 @@ public function getPlurals();
/**
* Sets this string using array of plural values.
*
* Serializes plural variants in one string glued by LOCALE_PLURAL_DELIMITER.
* Serializes plural variants in one string glued by
* \Drupal\Component\Gettext\PoItem::DELIMITER.
*
* @param array $plurals
* Array of strings with plural variants.
......
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\locale\Functional;
use Drupal\Component\Gettext\PoItem;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\Component\Render\FormattableMarkup;
......@@ -51,7 +52,7 @@ public function testFileParsing() {
$source_strings[$string->source] = $string->context;
}
$etx = LOCALE_PLURAL_DELIMITER;
$etx = PoItem::DELIMITER;
// List of all strings that should be in the file.
$test_strings = [
'Standard Call t' => '',
......
......@@ -2,7 +2,7 @@
namespace Drupal\Tests\locale\Functional;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\Component\Gettext\PoItem;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
......@@ -93,7 +93,7 @@ public function testFixOldPluralStyle($translation_value, $expected) {
public function providerTestFixOldPluralStyle() {
return [
'non-plural translation' => ['@count[2] non-plural test', '@count[2] non-plural test'],
'plural translation' => ['@count[2] plural test' . PluralTranslatableMarkup::DELIMITER, '@count plural test'],
'plural translation' => ['@count[2] plural test' . PoItem::DELIMITER, '@count plural test'],
];
}
......
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\locale\Functional;
use Drupal\Component\Gettext\PoItem;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\Tests\BrowserTestBase;
......@@ -139,7 +140,7 @@ public function testGetPluralFormat() {
// 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' . PluralTranslatableMarkup::DELIMITER . '@count hours', [], ['langcode' => $langcode]);
$translated_string = \Drupal::translation()->translate('1 hour' . PoItem::DELIMITER . '@count hours', [], ['langcode' => $langcode]);
$plural = PluralTranslatableMarkup::createFromTranslatedString($count, $translated_string, [], ['langcode' => $langcode]);
$this->assertIdentical($plural->render(), $expected_plural_string);
}
......@@ -186,7 +187,7 @@ public function testPluralEditDateFormatter() {
// not save our source string for performance optimization if we do not ask
// specifically for a language.
\Drupal::translation()->formatPlural(1, '1 second', '@count seconds', [], ['langcode' => 'fr'])->render();
$lid = db_query("SELECT lid FROM {locales_source} WHERE source = :source AND context = ''", [':source' => "1 second" . LOCALE_PLURAL_DELIMITER . "@count seconds"])->fetchField();
$lid = db_query("SELECT lid FROM {locales_source} WHERE source = :source AND context = ''", [':source' => "1 second" . PoItem::DELIMITER . "@count seconds"])->fetchField();
// Look up editing page for this plural string and check fields.
$search = [
'string' => '1 second',
......@@ -270,7 +271,7 @@ public function testPluralEditExport() {
$this->assertText('@count sati');
// Edit langcode hr translations and see if that took effect.
$lid = db_query("SELECT lid FROM {locales_source} WHERE source = :source AND context = ''", [':source' => "1 hour" . LOCALE_PLURAL_DELIMITER . "@count hours"])->fetchField();
$lid = db_query("SELECT lid FROM {locales_source} WHERE source = :source AND context = ''", [':source' => "1 hour" . PoItem::DELIMITER . "@count hours"])->fetchField();
$edit = [
"strings[$lid][translations][1]" => '@count sata edited',
];
......@@ -296,7 +297,7 @@ public function testPluralEditExport() {
// not save our source string for performance optimization if we do not ask
// specifically for a language.
\Drupal::translation()->formatPlural(1, '1 day', '@count days', [], ['langcode' => 'fr'])->render();
$lid = db_query("SELECT lid FROM {locales_source} WHERE source = :source AND context = ''", [':source' => "1 day" . LOCALE_PLURAL_DELIMITER . "@count days"])->fetchField();
$lid = db_query("SELECT lid FROM {locales_source} WHERE source = :source AND context = ''", [':source' => "1 day" . PoItem::DELIMITER . "@count days"])->fetchField();
// Look up editing page for this plural string and check fields.
$search = [
'string' => '1 day',
......
......@@ -2,8 +2,8 @@
namespace Drupal\Tests\locale\Unit;
use Drupal\Component\Gettext\PoItem;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\locale\LocaleLookup;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
......@@ -314,11 +314,11 @@ public function providerFixOldPluralTranslationProvider() {
$translations = [
'by' => [
'word1' => '@count[2] word-by',
'word2' => implode(PluralTranslatableMarkup::DELIMITER, ['word-by', '@count[2] word-by']),
'word2' => implode(PoItem::DELIMITER, ['word-by', '@count[2] word-by']),
],
'ru' => [
'word3' => '@count[2] word-ru',
'word4' => implode(PluralTranslatableMarkup::DELIMITER, ['word-ru', '@count[2] word-ru']),
'word4' => implode(PoItem::DELIMITER, ['word-ru', '@count[2] word-ru']),
],
];
return [
......
......@@ -5,6 +5,7 @@
* Configuration system that lets administrators modify the workings of the site.
*/
use Drupal\Component\Gettext\PoItem;
use Drupal\Core\Extension\Dependency;
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Component\Utility\UrlHelper;
......@@ -747,7 +748,7 @@ function system_js_settings_alter(&$settings, AttachedAssetsInterface $assets) {
}
}
if (!isset($settings['pluralDelimiter'])) {
$settings['pluralDelimiter'] = LOCALE_PLURAL_DELIMITER;
$settings['pluralDelimiter'] = PoItem::DELIMITER;
}
// Add the theme token to ajaxPageState, ensuring the database is available
// before doing so. Also add the loaded libraries to ajaxPageState.
......
......@@ -2,6 +2,7 @@
namespace Drupal\views\Plugin\views\field;
use Drupal\Component\Gettext\PoItem;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\views\ResultRow;
......@@ -30,7 +31,7 @@ protected function defineOptions() {
$options['decimal'] = ['default' => '.'];
$options['separator'] = ['default' => ','];
$options['format_plural'] = ['default' => FALSE];
$options['format_plural_string'] = ['default' => '1' . LOCALE_PLURAL_DELIMITER . '@count'];
$options['format_plural_string'] = ['default' => '1' . PoItem::DELIMITER . '@count'];
$options['prefix'] = ['default' => ''];
$options['suffix'] = ['default' => ''];
......@@ -93,7 +94,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
'#default_value' => $this->options['format_plural_string'],
];
$plural_array = explode(LOCALE_PLURAL_DELIMITER, $this->options['format_plural_string']);
$plural_array = explode(PoItem::DELIMITER, $this->options['format_plural_string']);
$plurals = $this->getNumberOfPlurals($this->view->storage->get('langcode'));
for ($i = 0; $i < $plurals; $i++) {
$form['format_plural_values'][$i] = [
......@@ -139,7 +140,7 @@ public function submitOptionsForm(&$form, FormStateInterface $form_state) {
// Merge plural format options into one string and drop the individual
// option values.
$options = &$form_state->getValue('options');
$options['format_plural_string'] = implode(LOCALE_PLURAL_DELIMITER, $options['format_plural_values']);
$options['format_plural_string'] = implode(PoItem::DELIMITER, $options['format_plural_values']);
unset($options['format_plural_values']);
parent::submitOptionsForm($form, $form_state);
}
......
......@@ -3,6 +3,7 @@
namespace Drupal\Tests\views\Functional\Plugin;
use Drupal\Component\Gettext\PoHeader;
use Drupal\Component\Gettext\PoItem;
use Drupal\file\Entity\File;
use Drupal\Tests\views\Functional\ViewTestBase;
......@@ -45,7 +46,7 @@ public function testNumericFormatPlural() {
$config = $this->config('views.view.numeric_test');
$field_config_prefix = 'display.default.display_options.fields.count.';
$this->assertEqual($config->get($field_config_prefix . 'format_plural'), TRUE);
$this->assertEqual($config->get($field_config_prefix . 'format_plural_string'), '1' . LOCALE_PLURAL_DELIMITER . '@count');
$this->assertEqual($config->get($field_config_prefix . 'format_plural_string'), '1' . PoItem::DELIMITER . '@count');
// Assert that the value is displayed.
$this->drupalGet('numeric-test');
......@@ -64,7 +65,7 @@ public function testNumericFormatPlural() {
$config = $this->config('views.view.numeric_test');
$field_config_prefix = 'display.default.display_options.fields.count.';
$this->assertEqual($config->get($field_config_prefix . 'format_plural'), TRUE);
$this->assertEqual($config->get($field_config_prefix . 'format_plural_string'), '1 time' . LOCALE_PLURAL_DELIMITER . '@count times');
$this->assertEqual($config->get($field_config_prefix . 'format_plural_string'), '1 time' . PoItem::DELIMITER . '@count times');
// Assert that the value is displayed with some sample values.
$numbers = [0, 1, 2, 3, 4, 42];
......@@ -107,7 +108,7 @@ public function testNumericFormatPlural() {
$config = $this->config('views.view.numeric_test');
$field_config_prefix = 'display.default.display_options.fields.count.';
$this->assertEqual($config->get($field_config_prefix . 'format_plural'), TRUE);
$this->assertEqual($config->get($field_config_prefix . 'format_plural_string'), implode(LOCALE_PLURAL_DELIMITER, array_values($edit)));
$this->assertEqual($config->get($field_config_prefix . 'format_plural_string'), implode(PoItem::DELIMITER, array_values($edit)));
// The view should now use the new plural configuration.
$this->drupalGet('sl/numeric-test');
......@@ -120,7 +121,7 @@ public function testNumericFormatPlural() {
// Add an English configuration translation with English plurals.
$english = \Drupal::languageManager()->getLanguageConfigOverride('en', 'views.view.numeric_test');
$english->set('display.default.display_options.fields.count.format_plural_string', '1 time' . LOCALE_PLURAL_DELIMITER . '@count times')->save();
$english->set('display.default.display_options.fields.count.format_plural_string', '1 time' . PoItem::DELIMITER . '@count times')->save();
// The view displayed in English should use the English translation.
$this->drupalGet('numeric-test');
......
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