diff --git a/core/core.services.yml b/core/core.services.yml index 2b81197..cffc627 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -510,7 +510,7 @@ services: - { name: string_translator, priority: 30 } string_translation: class: Drupal\Core\StringTranslation\TranslationManager - arguments: ['@language_manager'] + arguments: ['@language_manager', '@state'] calls: - [initLanguageManager] tags: diff --git a/core/lib/Drupal/Core/StringTranslation/TranslationManager.php b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php index 83880e1..9820d4c 100644 --- a/core/lib/Drupal/Core/StringTranslation/TranslationManager.php +++ b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php @@ -10,6 +10,7 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\String; use Drupal\Core\Language\LanguageManagerInterface; +use Drupal\Core\State\StateInterface; use Drupal\Core\StringTranslation\Translator\TranslatorInterface; /** @@ -54,14 +55,24 @@ 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 + * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. + * @param \Drupal\Core\State\StateInterface $state + * (optional) The state service. */ - public function __construct(LanguageManagerInterface $language_manager) { + public function __construct(LanguageManagerInterface $language_manager, StateInterface $state = NULL) { $this->languageManager = $language_manager; $this->defaultLangcode = $language_manager->getDefaultLanguage()->getId(); + $this->state = $state; } /** @@ -152,7 +163,6 @@ public function translate($string, array $args = array(), array $options = array * {@inheritdoc} */ public function formatPlural($count, $singular, $plural, array $args = array(), array $options = array()) { - $args['@count'] = $count; // Join both forms to search a translation. $translatable_string = implode(LOCALE_PLURAL_DELIMITER, array($singular, $plural)); // Translate as usual. @@ -164,6 +174,7 @@ public function formatPlural($count, $singular, $plural, array $args = array(), * {@inheritdoc} */ public function formatPluralTranslated($count, $translation, array $args = array(), array $options = array()) { + $args['@count'] = $count; // Split joined translation strings into array. $translated_array = explode(LOCALE_PLURAL_DELIMITER, $translation); @@ -203,10 +214,14 @@ public function formatPluralTranslated($count, $translation, array $args = array * @inheritdoc. */ public function getNumberOfPlurals($langcode = NULL) { - $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']; + // If the state service is not injected, we assume 2 plural variants are + // allowed. This may happen in the installer for simplicity. + 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/file/config/install/views.view.files.yml b/core/modules/file/config/install/views.view.files.yml index ae330a9..526d9fd 100644 --- a/core/modules/file/config/install/views.view.files.yml +++ b/core/modules/file/config/install/views.view.files.yml @@ -529,7 +529,7 @@ display: decimal: . separator: ',' format_plural: true - format_plural_string: '1 place\u0003@count places' + format_plural_string: "1 place\x03@count places" prefix: '' suffix: '' plugin_id: numeric @@ -951,7 +951,7 @@ display: decimal: . separator: ',' format_plural: false - format_plural_string: '1\u0003@count' + format_plural_string: "1\x03@count" prefix: '' suffix: '' plugin_id: numeric diff --git a/core/modules/forum/tests/modules/forum_test_views/test_views/views.view.test_forum_index.yml b/core/modules/forum/tests/modules/forum_test_views/test_views/views.view.test_forum_index.yml index 9938089..32a9306 100644 --- a/core/modules/forum/tests/modules/forum_test_views/test_views/views.view.test_forum_index.yml +++ b/core/modules/forum/tests/modules/forum_test_views/test_views/views.view.test_forum_index.yml @@ -141,7 +141,7 @@ display: decimal: . separator: ',' format_plural: false - format_plural_string: '1\u0003@count' + format_plural_string: "1\x03@count" prefix: '' suffix: '' plugin_id: numeric diff --git a/core/modules/statistics/tests/modules/statistics_test_views/test_views/views.view.test_statistics_integration.yml b/core/modules/statistics/tests/modules/statistics_test_views/test_views/views.view.test_statistics_integration.yml index 6a42631..680648b 100644 --- a/core/modules/statistics/tests/modules/statistics_test_views/test_views/views.view.test_statistics_integration.yml +++ b/core/modules/statistics/tests/modules/statistics_test_views/test_views/views.view.test_statistics_integration.yml @@ -160,7 +160,7 @@ display: decimal: . separator: '' format_plural: false - format_plural_string: '1\u0003@count' + format_plural_string: "1\x03@count" prefix: '' suffix: '' plugin_id: numeric @@ -217,7 +217,7 @@ display: decimal: . separator: '' format_plural: false - format_plural_string: '1\u0003@count' + format_plural_string: "1\x03@count" prefix: '' suffix: '' plugin_id: numeric