From a537a3098e3cfe000b465f8caee1cf6504566c00 Mon Sep 17 00:00:00 2001 From: effulgentsia Date: Mon, 21 Sep 2015 12:38:52 -0700 Subject: [PATCH] Issue #2571375 by alexpott, webflo, stefan.r, Xano, mr.baileys: Remove TranslationManager dependency from LanguageManager --- core/core.services.yml | 4 +- core/includes/install.core.inc | 5 +- .../Drupal/Core/Language/LanguageManager.php | 34 +---- .../Language/LanguageManagerInterface.php | 8 -- .../StringTranslationTrait.php | 11 +- .../TranslationInterface.php | 12 -- .../StringTranslation/TranslationManager.php | 57 +-------- .../src/ConfigurableLanguageManager.php | 3 +- core/modules/locale/locale.module | 17 ++- core/modules/locale/locale.services.yml | 3 + core/modules/locale/src/PluralFormula.php | 116 ++++++++++++++++++ .../locale/src/PluralFormulaInterface.php | 59 +++++++++ core/modules/locale/src/PoDatabaseWriter.php | 6 +- .../src/Tests/LocaleImportFunctionalTest.php | 12 +- .../simpletest/src/InstallerTestBase.php | 5 +- .../Installer/InstallerLanguagePageTest.php | 3 +- .../src/Tests/Update/UpdatePathTestBase.php | 5 +- .../Tests/Plugin/NumericFormatPluralTest.php | 2 +- .../TranslationStringTest.php | 74 +++++++++++ 19 files changed, 292 insertions(+), 144 deletions(-) create mode 100644 core/modules/locale/src/PluralFormula.php create mode 100644 core/modules/locale/src/PluralFormulaInterface.php create mode 100644 core/tests/Drupal/KernelTests/Core/StringTranslation/TranslationStringTest.php diff --git a/core/core.services.yml b/core/core.services.yml index 0a695417d95..c56c77872ff 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -671,9 +671,7 @@ services: - { name: string_translator, priority: 30 } string_translation: class: Drupal\Core\StringTranslation\TranslationManager - arguments: ['@language_manager', '@state'] - calls: - - [initLanguageManager] + arguments: ['@language.default'] tags: - { name: service_collector, tag: string_translator, call: addTranslator } database.replica: diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc index b25d28cb2a3..449b9e0b7b1 100644 --- a/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -345,12 +345,9 @@ function install_begin_request($class_loader, &$install_state) { $container ->register('language.default', 'Drupal\Core\Language\LanguageDefault') ->addArgument('%language.default_values%'); - $container - ->register('language_manager', 'Drupal\Core\Language\LanguageManager') - ->addArgument(new Reference('language.default')); $container ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager') - ->addArgument(new Reference('language_manager')); + ->addArgument(new Reference('language.default')); // Register the stream wrapper manager. $container diff --git a/core/lib/Drupal/Core/Language/LanguageManager.php b/core/lib/Drupal/Core/Language/LanguageManager.php index 8ab64cc3922..b8f5aeffb94 100644 --- a/core/lib/Drupal/Core/Language/LanguageManager.php +++ b/core/lib/Drupal/Core/Language/LanguageManager.php @@ -7,9 +7,7 @@ namespace Drupal\Core\Language; -use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\DependencyInjection\DependencySerializationTrait; -use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\StringTranslation\TranslatableString; use Drupal\Core\Url; @@ -19,13 +17,6 @@ use Drupal\Core\Url; class LanguageManager implements LanguageManagerInterface { use DependencySerializationTrait; - /** - * The string translation service. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translation; - /** * A static cache of translated language lists. * @@ -56,22 +47,6 @@ class LanguageManager implements LanguageManagerInterface { $this->defaultLanguage = $default_language; } - /** - * {@inheritdoc} - */ - public function setTranslation(TranslationInterface $translation) { - $this->translation = $translation; - } - - /** - * Translates a string to the current language or to a given language. - * - * @see \Drupal\Core\StringTranslation\TranslationInterface() - */ - protected function t($string, array $args = array(), array $options = array()) { - return $this->translation ? $this->translation->translate($string, $args, $options) : SafeMarkup::format($string, $args); - } - /** * {@inheritdoc} */ @@ -187,15 +162,15 @@ class LanguageManager implements LanguageManagerInterface { */ public function getLanguageName($langcode) { if ($langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) { - return $this->t('None'); + return new TranslatableString('None'); } if ($language = $this->getLanguage($langcode)) { return $language->getName(); } if (empty($langcode)) { - return $this->t('Unknown'); + return new TranslatableString('Unknown'); } - return $this->t('Unknown (@langcode)', array('@langcode' => $langcode)); + return new TranslatableString('Unknown (@langcode)', array('@langcode' => $langcode)); } /** @@ -377,7 +352,6 @@ class LanguageManager implements LanguageManagerInterface { return $this->getCurrentLanguage(); } - /** * Filters the full list of languages based on the value of the flag. * @@ -409,7 +383,7 @@ class LanguageManager implements LanguageManagerInterface { $default = new Language( array( 'id' => $defaultLanguage->getId(), - 'name' => $this->t("Site's default language (@lang_name)", + 'name' => new TranslatableString("Site's default language (@lang_name)", array('@lang_name' => $defaultLanguage->getName())), 'direction' => $defaultLanguage->getDirection(), 'weight' => $defaultLanguage->getWeight(), diff --git a/core/lib/Drupal/Core/Language/LanguageManagerInterface.php b/core/lib/Drupal/Core/Language/LanguageManagerInterface.php index b536a00c4ad..950fc698930 100644 --- a/core/lib/Drupal/Core/Language/LanguageManagerInterface.php +++ b/core/lib/Drupal/Core/Language/LanguageManagerInterface.php @@ -15,14 +15,6 @@ use Drupal\Core\Url; */ interface LanguageManagerInterface { - /** - * Injects the string translation service. - * - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation - * The string translation service. - */ - public function setTranslation(TranslationInterface $translation); - /** * Returns whether or not the site has more than one language added. * diff --git a/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php b/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php index fe7befcec5e..f4b4bd32649 100644 --- a/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php +++ b/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php @@ -70,12 +70,17 @@ trait StringTranslationTrait { /** * Returns the number of plurals supported by a given language. * - * See the - * \Drupal\Core\StringTranslation\TranslationInterface::getNumberOfPlurals() + * See the \Drupal\locale\PluralFormulaInterface::getNumberOfPlurals() * documentation for details. + * + * @see \Drupal\locale\PluralFormulaInterface::getNumberOfPlurals() */ protected function getNumberOfPlurals($langcode = NULL) { - return $this->getStringTranslation()->getNumberOfPlurals($langcode); + if (\Drupal::hasService('locale.plural.formula')) { + return \Drupal::service('locale.plural.formula')->getNumberOfPlurals($langcode); + } + // We assume 2 plurals if Locale's services are not available. + return 2; } /** diff --git a/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php b/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php index 0521612e925..f9ff9ef8fb0 100644 --- a/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php +++ b/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php @@ -102,16 +102,4 @@ interface TranslationInterface { */ public function formatPlural($count, $singular, $plural, array $args = array(), array $options = array()); - /** - * Returns the number of plurals supported by a given language. - * - * @param null|string $langcode - * (optional) The language code. If not provided, the current language - * will be used. - * - * @return int - * Number of plural variants supported by the given language. - */ - public function getNumberOfPlurals($langcode = NULL); - } diff --git a/core/lib/Drupal/Core/StringTranslation/TranslationManager.php b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php index ce4c6b36c97..c7bad70ee1f 100644 --- a/core/lib/Drupal/Core/StringTranslation/TranslationManager.php +++ b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php @@ -8,8 +8,7 @@ namespace Drupal\Core\StringTranslation; use Drupal\Component\Utility\SafeMarkup; -use Drupal\Core\Language\LanguageManagerInterface; -use Drupal\Core\State\StateInterface; +use Drupal\Core\Language\LanguageDefault; use Drupal\Core\StringTranslation\Translator\TranslatorInterface; /** @@ -17,13 +16,6 @@ use Drupal\Core\StringTranslation\Translator\TranslatorInterface; */ class TranslationManager implements TranslationInterface, TranslatorInterface { - /** - * The language manager. - * - * @var \Drupal\Core\Language\LanguageManagerInterface - */ - protected $languageManager; - /** * An array of active translators keyed by priority. * @@ -53,36 +45,14 @@ class TranslationManager implements TranslationInterface, TranslatorInterface { */ protected $defaultLangcode; - /** - * The state service. - * - * @var \Drupal\Core\State\StateInterface - */ - protected $state; - /** * Constructs a TranslationManager object. * - * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager - * The language manager. - * @param \Drupal\Core\State\StateInterface $state - * (optional) The state service. + * @param \Drupal\Core\Language\LanguageDefault $default_language + * The default language. */ - public function __construct(LanguageManagerInterface $language_manager, StateInterface $state = NULL) { - $this->languageManager = $language_manager; - $this->defaultLangcode = $language_manager->getDefaultLanguage()->getId(); - $this->state = $state; - } - - /** - * Initializes the injected language manager with the translation manager. - * - * This should be called right after instantiating the translation manager to - * make it available to the language manager without introducing a circular - * dependency. - */ - public function initLanguageManager() { - $this->languageManager->setTranslation($this); + public function __construct(LanguageDefault $default_language) { + $this->defaultLangcode = $default_language->get()->getId(); } /** @@ -229,21 +199,4 @@ class TranslationManager implements TranslationInterface, TranslatorInterface { } } - /** - * @inheritdoc. - */ - public function getNumberOfPlurals($langcode = NULL) { - // If the state service is not injected, we assume 2 plural variants are - // allowed. This may happen in the installer for simplicity. We also assume - // 2 plurals if there is no explicit information yet. - if (isset($this->state)) { - $langcode = $langcode ?: $this->languageManager->getCurrentLanguage()->getId(); - $plural_formulas = $this->state->get('locale.translation.plurals') ?: array(); - if (isset($plural_formulas[$langcode]['plurals'])) { - return $plural_formulas[$langcode]['plurals']; - } - } - return 2; - } - } diff --git a/core/modules/language/src/ConfigurableLanguageManager.php b/core/modules/language/src/ConfigurableLanguageManager.php index d51692f1a43..26c6c139ce4 100644 --- a/core/modules/language/src/ConfigurableLanguageManager.php +++ b/core/modules/language/src/ConfigurableLanguageManager.php @@ -13,6 +13,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Language\Language; use Drupal\Core\Language\LanguageDefault; use Drupal\Core\Language\LanguageManager; +use Drupal\Core\StringTranslation\TranslatableString; use Drupal\Core\Url; use Drupal\language\Config\LanguageConfigFactoryOverrideInterface; use Drupal\language\Entity\ConfigurableLanguage; @@ -469,7 +470,7 @@ class ConfigurableLanguageManager extends LanguageManager implements Configurabl unset($predefined[$key]); continue; } - $predefined[$key] = $this->t($value[0]); + $predefined[$key] = new TranslatableString($value[0]); } asort($predefined); return $predefined; diff --git a/core/modules/locale/locale.module b/core/modules/locale/locale.module index 290544e16b6..02ab2e7187b 100644 --- a/core/modules/locale/locale.module +++ b/core/modules/locale/locale.module @@ -276,8 +276,6 @@ function locale_translatable_language_list() { function locale_get_plural($count, $langcode = NULL) { $language_interface = \Drupal::languageManager()->getCurrentLanguage(); - // Used to locally cache the plural formulas for all languages. - $plural_formulas = &drupal_static(__FUNCTION__, array()); // Used to store precomputed plural indexes corresponding to numbers // individually for each language. $plural_indexes = &drupal_static(__FUNCTION__ . ':plurals', array()); @@ -286,18 +284,17 @@ function locale_get_plural($count, $langcode = NULL) { if (!isset($plural_indexes[$langcode][$count])) { // Retrieve and statically cache the plural formulas for all languages. - if (empty($plural_formulas)) { - $plural_formulas = \Drupal::state()->get('locale.translation.plurals') ?: array(); - } + $plural_formulas = \Drupal::service('locale.plural.formula')->getFormula($langcode); + // If there is a plural formula for the language, evaluate it for the given // $count and statically cache the result for the combination of language // and count, since the result will always be identical. - if (!empty($plural_formulas[$langcode])) { + if (!empty($plural_formulas)) { // Plural formulas are stored as an array for 0-199. 100 is the highest // modulo used but storing 0-99 is not enough because below 100 we often // find exceptions (1, 2, etc). $index = $count > 199 ? 100 + ($count % 100) : $count; - $plural_indexes[$langcode][$count] = isset($plural_formulas[$langcode]['formula'][$index]) ? $plural_formulas[$langcode]['formula'][$index] : $plural_formulas[$langcode]['formula']['default']; + $plural_indexes[$langcode][$count] = isset($plural_formulas[$index]) ? $plural_formulas[$index] : $plural_formulas['default']; } // In case there is no plural formula for English (no imported translation // for English), use a default formula. @@ -1272,9 +1269,9 @@ function _locale_rebuild_js($langcode = NULL) { 'strings' => $translations, ); - $locale_plurals = \Drupal::state()->get('locale.translation.plurals') ?: array(); - if (!empty($locale_plurals[$language->getId()]['formula'])) { - $data['pluralFormula'] = $locale_plurals[$language->getId()]['formula']; + $locale_plurals = \Drupal::service('locale.plural.formula')->getFormula($language->getId()); + if ($locale_plurals) { + $data['pluralFormula'] = $locale_plurals; } $data = 'window.drupalTranslations = ' . Json::encode($data) . ';'; diff --git a/core/modules/locale/locale.services.yml b/core/modules/locale/locale.services.yml index 393866bc074..29603ee22aa 100644 --- a/core/modules/locale/locale.services.yml +++ b/core/modules/locale/locale.services.yml @@ -14,6 +14,9 @@ services: locale.project: class: Drupal\locale\LocaleProjectStorage arguments: ['@keyvalue'] + locale.plural.formula: + class: Drupal\locale\PluralFormula + arguments: ['@language_manager', '@state'] string_translator.locale.lookup: class: Drupal\locale\LocaleTranslation arguments: ['@locale.storage', '@cache.default', '@lock', '@config.factory', '@language_manager', '@request_stack'] diff --git a/core/modules/locale/src/PluralFormula.php b/core/modules/locale/src/PluralFormula.php new file mode 100644 index 00000000000..938eb10fa4f --- /dev/null +++ b/core/modules/locale/src/PluralFormula.php @@ -0,0 +1,116 @@ + [ + * 'plurals' => 2, + * 'formula' => [ + * // @todo + * ] + * ], + * ] + * @endcode + * @var [] + */ + protected $formulae; + + /** + * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager + * @param \Drupal\Core\State\StateInterface $state + */ + public function __construct(LanguageManagerInterface $language_manager, StateInterface $state) { + $this->languageManager = $language_manager; + $this->state = $state; + } + + /** + * {@inheritdoc} + */ + public function setPluralFormula($langcode, $plural_count, array $formula) { + // Ensure that the formulae are loaded. + $this->loadFormulae(); + + $this->formulae[$langcode] = [ + 'plurals' => $plural_count, + 'formula' => $formula, + ]; + $this->state->set('locale.translation.formulae', $this->formulae); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getNumberOfPlurals($langcode = NULL) { + // Ensure that the formulae are loaded. + $this->loadFormulae(); + + // Set the langcode to use. + $langcode = $langcode ?: $this->languageManager->getCurrentLanguage()->getId(); + + // We assume 2 plurals if there is no explicit information yet. + if (!isset($this->formulae[$langcode]['plurals'])) { + return 2; + } + return $this->formulae[$langcode]['plurals']; + } + + /** + * {@inheritdoc} + */ + public function getFormula($langcode) { + $this->loadFormulae(); + return isset($this->formulae[$langcode]['formula']) ? $this->formulae[$langcode]['formula'] : FALSE; + } + + /** + * Loads the formulae and stores them on the PluralFormula object if not set. + * + * @return [] + */ + protected function loadFormulae() { + if (!isset($this->formulae)) { + $this->formulae = $this->state->get('locale.translation.formulae', []); + } + } + + /** + * {@inheritdoc} + */ + public function reset() { + $this->formulae = NULL; + return $this; + } + +} diff --git a/core/modules/locale/src/PluralFormulaInterface.php b/core/modules/locale/src/PluralFormulaInterface.php new file mode 100644 index 00000000000..c6d3ba0c7d9 --- /dev/null +++ b/core/modules/locale/src/PluralFormulaInterface.php @@ -0,0 +1,59 @@ +getPluralForms(); if (isset($plural) && $p = $header->parsePluralForms($plural)) { list($nplurals, $formula) = $p; - $locale_plurals[$langcode] = array( - 'plurals' => $nplurals, - 'formula' => $formula, - ); - \Drupal::state()->set('locale.translation.plurals', $locale_plurals); + \Drupal::service('locale.plural.formula')->setPluralFormula($langcode, $nplurals, $formula); } } } diff --git a/core/modules/locale/src/Tests/LocaleImportFunctionalTest.php b/core/modules/locale/src/Tests/LocaleImportFunctionalTest.php index 1c13517bbb5..ca8254ca3fa 100644 --- a/core/modules/locale/src/Tests/LocaleImportFunctionalTest.php +++ b/core/modules/locale/src/Tests/LocaleImportFunctionalTest.php @@ -76,8 +76,8 @@ class LocaleImportFunctionalTest extends WebTestBase { $this->assertRaw(t('One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.', array('%number' => 8, '%update' => 0, '%delete' => 0)), 'The translation file was successfully imported.'); // This import should have saved plural forms to have 2 variants. - $locale_plurals = \Drupal::state()->get('locale.translation.plurals') ?: array(); - $this->assert($locale_plurals['fr']['plurals'] == 2, 'Plural number initialized.'); + $locale_plurals = \Drupal::service('locale.plural.formula')->getNumberOfPlurals('fr'); + $this->assertEqual(2, $locale_plurals, 'Plural number initialized.'); // Ensure we were redirected correctly. $this->assertUrl(\Drupal::url('locale.translate_page', [], ['absolute' => TRUE]), [], 'Correct page redirection.'); @@ -151,8 +151,8 @@ class LocaleImportFunctionalTest extends WebTestBase { $this->assertText(t('No strings available.'), 'String not overwritten by imported string.'); // This import should not have changed number of plural forms. - $locale_plurals = \Drupal::state()->get('locale.translation.plurals') ?: array(); - $this->assert($locale_plurals['fr']['plurals'] == 2, 'Plural numbers untouched.'); + $locale_plurals = \Drupal::service('locale.plural.formula')->getNumberOfPlurals('fr'); + $this->assertEqual(2, $locale_plurals, 'Plural numbers untouched.'); // Try importing a .po file with overriding strings, and ensure existing // strings are overwritten. @@ -172,8 +172,8 @@ class LocaleImportFunctionalTest extends WebTestBase { $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter')); $this->assertNoText(t('No strings available.'), 'String overwritten by imported string.'); // This import should have changed number of plural forms. - $locale_plurals = \Drupal::state()->get('locale.translation.plurals') ?: array(); - $this->assert($locale_plurals['fr']['plurals'] == 3, 'Plural numbers changed.'); + $locale_plurals = \Drupal::service('locale.plural.formula')->reset()->getNumberOfPlurals('fr'); + $this->assertEqual(3, $locale_plurals, 'Plural numbers changed.'); // Importing a .po file and mark its strings as customized strings. $this->importPoFile($this->getCustomPoFile(), array( diff --git a/core/modules/simpletest/src/InstallerTestBase.php b/core/modules/simpletest/src/InstallerTestBase.php index 7207555f869..e6f7d7b47de 100644 --- a/core/modules/simpletest/src/InstallerTestBase.php +++ b/core/modules/simpletest/src/InstallerTestBase.php @@ -111,12 +111,9 @@ abstract class InstallerTestBase extends WebTestBase { $this->container ->register('language.default', 'Drupal\Core\Language\LanguageDefault') ->addArgument('%language.default_values%'); - $this->container - ->register('language_manager', 'Drupal\Core\Language\LanguageManager') - ->addArgument(new Reference('language.default')); $this->container ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager') - ->addArgument(new Reference('language_manager')); + ->addArgument(new Reference('language.default')); $this->container ->set('app.root', DRUPAL_ROOT); \Drupal::setContainer($this->container); diff --git a/core/modules/system/src/Tests/Installer/InstallerLanguagePageTest.php b/core/modules/system/src/Tests/Installer/InstallerLanguagePageTest.php index ce573ba6985..d46953247ab 100644 --- a/core/modules/system/src/Tests/Installer/InstallerLanguagePageTest.php +++ b/core/modules/system/src/Tests/Installer/InstallerLanguagePageTest.php @@ -7,6 +7,7 @@ namespace Drupal\system\Tests\Installer; +use Drupal\Core\Language\LanguageManager; use Drupal\simpletest\InstallerTestBase; /** @@ -26,7 +27,7 @@ class InstallerLanguagePageTest extends InstallerTestBase { // Check that all predefined languages show up with their native names. $this->drupalGet($GLOBALS['base_url'] . '/core/install.php'); - foreach (\Drupal::languageManager()->getStandardLanguageList() as $langcode => $names) { + foreach (LanguageManager::getStandardLanguageList() as $langcode => $names) { $this->assertOption('edit-langcode', $langcode); $this->assertRaw('>' . $names[1] . '<'); } diff --git a/core/modules/system/src/Tests/Update/UpdatePathTestBase.php b/core/modules/system/src/Tests/Update/UpdatePathTestBase.php index c6281d000ee..5fae5a012d8 100644 --- a/core/modules/system/src/Tests/Update/UpdatePathTestBase.php +++ b/core/modules/system/src/Tests/Update/UpdatePathTestBase.php @@ -283,12 +283,9 @@ abstract class UpdatePathTestBase extends WebTestBase { $container ->register('language.default', 'Drupal\Core\Language\LanguageDefault') ->addArgument('%language.default_values%'); - $container - ->register('language_manager', 'Drupal\Core\Language\LanguageManager') - ->addArgument(new Reference('language.default')); $container ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager') - ->addArgument(new Reference('language_manager')); + ->addArgument(new Reference('language.default')); \Drupal::setContainer($container); require_once __DIR__ . '/../../../../../includes/install.inc'; diff --git a/core/modules/views/src/Tests/Plugin/NumericFormatPluralTest.php b/core/modules/views/src/Tests/Plugin/NumericFormatPluralTest.php index 97b9bb24d47..8e75e4dab02 100644 --- a/core/modules/views/src/Tests/Plugin/NumericFormatPluralTest.php +++ b/core/modules/views/src/Tests/Plugin/NumericFormatPluralTest.php @@ -86,7 +86,7 @@ class NumericFormatPluralTest extends ViewTestBase { $formula = 'nplurals=4; plural=(n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3);'; $header = new PoHeader(); list($nplurals, $formula) = $header->parsePluralForms($formula); - \Drupal::state()->set('locale.translation.plurals', ['sl' => ['plurals' => $nplurals, 'formula' => $formula]]); + \Drupal::service('locale.plural.formula')->setPluralFormula('sl', $nplurals, $formula); // Change the view to Slovenian. $config = $this->config('views.view.numeric_test'); diff --git a/core/tests/Drupal/KernelTests/Core/StringTranslation/TranslationStringTest.php b/core/tests/Drupal/KernelTests/Core/StringTranslation/TranslationStringTest.php new file mode 100644 index 00000000000..012ae7b15c4 --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/StringTranslation/TranslationStringTest.php @@ -0,0 +1,74 @@ +save(); + } + + /** + * Tests that TranslatableString objects can be compared. + */ + public function testComparison() { + $this->rebootAndPrepareSettings(); + $a = \Drupal::service('string_translation')->translate('Example @number', ['@number' => 42], ['langcode' => 'de']); + + $this->rebootAndPrepareSettings(); + $b = \Drupal::service('string_translation')->translate('Example @number', ['@number' => 42], ['langcode' => 'de']); + $c = \Drupal::service('string_translation')->translate('Example @number', ['@number' => 43], ['langcode' => 'de']); + $d = \Drupal::service('string_translation')->translate('Example @number', ['@number' => 42], ['langcode' => 'en']); + + // The two objects have the same settings so == comparison will work. + $this->assertEquals($a, $b); + // The two objects are not the same object. + $this->assertNotSame($a, $b); + // TranslationWrappers which have different settings are not equal. + $this->assertNotEquals($a, $c); + $this->assertNotEquals($a, $d); + } + + /** + * Reboots the kernel to set custom translations in Settings. + */ + protected function rebootAndPrepareSettings() { + // Reboot the container so that different services are injected and the new + // settings are picked. + $kernel = $this->container->get('kernel'); + $kernel->shutdown(); + $kernel->boot(); + $settings = Settings::getAll(); + $settings['locale_custom_strings_de'] = ['' => ['Example @number' => 'Example @number translated']]; + // Recreate the settings static. + new Settings($settings); + } + +}