Loading core/core.services.yml +1 −1 Original line number Diff line number Diff line Loading @@ -1124,7 +1124,7 @@ services: - { name: string_translator, priority: 30 } string_translation: class: Drupal\Core\StringTranslation\TranslationManager arguments: ['@language.default'] autowire: true tags: - { name: service_collector, tag: string_translator, call: addTranslator } Drupal\Core\StringTranslation\TranslationInterface: '@string_translation' Loading core/lib/Drupal/Core/StringTranslation/PluralTranslatableMarkup.php +7 −43 Original line number Diff line number Diff line Loading @@ -90,54 +90,18 @@ public static function createFromTranslatedString($count, $translated_string, ar } /** * Renders the object as a string. * * @return string * The translated string. * {@inheritdoc} */ public function render() { if (!$this->translatedString) { $this->translatedString = $this->getStringTranslation()->translateString($this); } if ($this->translatedString === '') { return ''; } $arguments = $this->getArguments(); $arguments['@count'] = $this->count; $translated_array = explode(PoItem::DELIMITER, $this->translatedString); $index = $this->getPluralIndex(); if ($this->count == 1 || $index == 0 || count($translated_array) == 1) { // Singular form. $return = $translated_array[0]; } else { // Nth plural form, fallback to second plural form. $return = $translated_array[$index] ?? $translated_array[1]; } return $this->placeholderFormat($return, $arguments); protected function translateString(): string { $this->translatedString ??= parent::translateString(); return $this->getStringTranslation()->selectPluralForm($this->count, $this->translatedString, $this->getOptions()['langcode'] ?? NULL); } /** * Gets the plural index through the gettext formula. * * @return int * The numeric index of the plural variant to use for this language and * count combination. Defaults to -1 when the language was not found or does * not have a plural formula. * {@inheritdoc} */ protected function getPluralIndex() { // We have to test both if the function and the service exist since in // certain situations it is possible that locale code might be loaded but // the service does not exist. For example, where the parent test site has // locale installed but the child site does not. // @todo Refactor in https://www.drupal.org/node/2660338 so this code does // not depend on knowing that the Locale module exists. if (function_exists('locale_get_plural') && \Drupal::hasService('locale.plural.formula')) { return locale_get_plural($this->count, $this->getOption('langcode')); } return -1; public function getArguments(): array { return parent::getArguments() + ['@count' => $this->count]; } /** Loading core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php +11 −1 Original line number Diff line number Diff line Loading @@ -184,7 +184,7 @@ public function getArguments() { */ public function render() { if (!isset($this->translatedMarkup)) { $this->translatedMarkup = $this->getStringTranslation()->translateString($this); $this->translatedMarkup = $this->translateString(); } // Handle any replacements. Loading @@ -194,6 +194,16 @@ public function render() { return $this->translatedMarkup; } /** * Translates the string. * * @return string * The translated string. */ protected function translateString(): string { return $this->getStringTranslation()->translateString($this); } /** * Magic __sleep() method to avoid serializing the string translator. */ Loading core/lib/Drupal/Core/StringTranslation/TranslationInterface.php +18 −0 Original line number Diff line number Diff line Loading @@ -109,4 +109,22 @@ public function translateString(TranslatableMarkup $translated_string); */ public function formatPlural($count, $singular, $plural, array $args = [], array $options = []); /** * Selects the plural form for a given count. * * @param int|float $count * The item count to display. * @param string $string * The translated string with plural forms delimited by PoItem::DELIMITER. * @param string|null $langcode * The language code to translate to. If it is NULL then the current * language will be used. * * @return string * The plural form to use. * * @see \Drupal\Core\StringTranslation\TranslationManager::doTranslate() */ public function selectPluralForm(int|float $count, string $string, ?string $langcode = NULL): string; } core/lib/Drupal/Core/StringTranslation/TranslationManager.php +41 −6 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Drupal\Core\StringTranslation; use Drupal\Component\Gettext\PoItem; use Drupal\Core\Language\LanguageDefault; use Drupal\Core\StringTranslation\Translator\TranslatorInterface; Loading Loading @@ -43,12 +44,6 @@ class TranslationManager implements TranslationInterface, TranslatorInterface { */ protected $defaultLangcode; /** * Constructs a TranslationManager object. * * @param \Drupal\Core\Language\LanguageDefault $default_language * The default language. */ public function __construct(LanguageDefault $default_language) { $this->defaultLangcode = $default_language->get()->getId(); } Loading Loading @@ -113,6 +108,46 @@ public function translateString(TranslatableMarkup $translated_string) { return $this->doTranslate($translated_string->getUntranslatedString(), $translated_string->getOptions()); } /** * {@inheritdoc} */ public function selectPluralForm(int|float $count, string $string, ?string $langcode = NULL): string { $translated_array = explode(PoItem::DELIMITER, $string); if ($count == 1 || count($translated_array) == 1) { // Singular form. return $translated_array[0]; } // Plural formulas are defined over integers, so a fractional count is // truncated for the lookup only. The count itself is still displayed as // given via the @count placeholder. $index = $this->getPluralIndex((int) $count, $langcode); // Fallback to second plural form. return $translated_array[$index] ?? $translated_array[1]; } /** * Returns plural form index for a specific number. * * The base implementation only distinguishes between singular and plural, * so it always returns the second (index 1) plural form. Subclasses that * know a language's full plural rules should override this method. * * @param int $count * Number to return plural for. * @param string|null $langcode * (optional) Language code to translate to a language other than what is * used to display the page. * * @return int * The numeric index of the plural variant to use for this $langcode and * $count combination. */ protected function getPluralIndex(int $count, ?string $langcode = NULL): int { return 1; } /** * Translates a string to the current language or to a given language. * Loading Loading
core/core.services.yml +1 −1 Original line number Diff line number Diff line Loading @@ -1124,7 +1124,7 @@ services: - { name: string_translator, priority: 30 } string_translation: class: Drupal\Core\StringTranslation\TranslationManager arguments: ['@language.default'] autowire: true tags: - { name: service_collector, tag: string_translator, call: addTranslator } Drupal\Core\StringTranslation\TranslationInterface: '@string_translation' Loading
core/lib/Drupal/Core/StringTranslation/PluralTranslatableMarkup.php +7 −43 Original line number Diff line number Diff line Loading @@ -90,54 +90,18 @@ public static function createFromTranslatedString($count, $translated_string, ar } /** * Renders the object as a string. * * @return string * The translated string. * {@inheritdoc} */ public function render() { if (!$this->translatedString) { $this->translatedString = $this->getStringTranslation()->translateString($this); } if ($this->translatedString === '') { return ''; } $arguments = $this->getArguments(); $arguments['@count'] = $this->count; $translated_array = explode(PoItem::DELIMITER, $this->translatedString); $index = $this->getPluralIndex(); if ($this->count == 1 || $index == 0 || count($translated_array) == 1) { // Singular form. $return = $translated_array[0]; } else { // Nth plural form, fallback to second plural form. $return = $translated_array[$index] ?? $translated_array[1]; } return $this->placeholderFormat($return, $arguments); protected function translateString(): string { $this->translatedString ??= parent::translateString(); return $this->getStringTranslation()->selectPluralForm($this->count, $this->translatedString, $this->getOptions()['langcode'] ?? NULL); } /** * Gets the plural index through the gettext formula. * * @return int * The numeric index of the plural variant to use for this language and * count combination. Defaults to -1 when the language was not found or does * not have a plural formula. * {@inheritdoc} */ protected function getPluralIndex() { // We have to test both if the function and the service exist since in // certain situations it is possible that locale code might be loaded but // the service does not exist. For example, where the parent test site has // locale installed but the child site does not. // @todo Refactor in https://www.drupal.org/node/2660338 so this code does // not depend on knowing that the Locale module exists. if (function_exists('locale_get_plural') && \Drupal::hasService('locale.plural.formula')) { return locale_get_plural($this->count, $this->getOption('langcode')); } return -1; public function getArguments(): array { return parent::getArguments() + ['@count' => $this->count]; } /** Loading
core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php +11 −1 Original line number Diff line number Diff line Loading @@ -184,7 +184,7 @@ public function getArguments() { */ public function render() { if (!isset($this->translatedMarkup)) { $this->translatedMarkup = $this->getStringTranslation()->translateString($this); $this->translatedMarkup = $this->translateString(); } // Handle any replacements. Loading @@ -194,6 +194,16 @@ public function render() { return $this->translatedMarkup; } /** * Translates the string. * * @return string * The translated string. */ protected function translateString(): string { return $this->getStringTranslation()->translateString($this); } /** * Magic __sleep() method to avoid serializing the string translator. */ Loading
core/lib/Drupal/Core/StringTranslation/TranslationInterface.php +18 −0 Original line number Diff line number Diff line Loading @@ -109,4 +109,22 @@ public function translateString(TranslatableMarkup $translated_string); */ public function formatPlural($count, $singular, $plural, array $args = [], array $options = []); /** * Selects the plural form for a given count. * * @param int|float $count * The item count to display. * @param string $string * The translated string with plural forms delimited by PoItem::DELIMITER. * @param string|null $langcode * The language code to translate to. If it is NULL then the current * language will be used. * * @return string * The plural form to use. * * @see \Drupal\Core\StringTranslation\TranslationManager::doTranslate() */ public function selectPluralForm(int|float $count, string $string, ?string $langcode = NULL): string; }
core/lib/Drupal/Core/StringTranslation/TranslationManager.php +41 −6 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Drupal\Core\StringTranslation; use Drupal\Component\Gettext\PoItem; use Drupal\Core\Language\LanguageDefault; use Drupal\Core\StringTranslation\Translator\TranslatorInterface; Loading Loading @@ -43,12 +44,6 @@ class TranslationManager implements TranslationInterface, TranslatorInterface { */ protected $defaultLangcode; /** * Constructs a TranslationManager object. * * @param \Drupal\Core\Language\LanguageDefault $default_language * The default language. */ public function __construct(LanguageDefault $default_language) { $this->defaultLangcode = $default_language->get()->getId(); } Loading Loading @@ -113,6 +108,46 @@ public function translateString(TranslatableMarkup $translated_string) { return $this->doTranslate($translated_string->getUntranslatedString(), $translated_string->getOptions()); } /** * {@inheritdoc} */ public function selectPluralForm(int|float $count, string $string, ?string $langcode = NULL): string { $translated_array = explode(PoItem::DELIMITER, $string); if ($count == 1 || count($translated_array) == 1) { // Singular form. return $translated_array[0]; } // Plural formulas are defined over integers, so a fractional count is // truncated for the lookup only. The count itself is still displayed as // given via the @count placeholder. $index = $this->getPluralIndex((int) $count, $langcode); // Fallback to second plural form. return $translated_array[$index] ?? $translated_array[1]; } /** * Returns plural form index for a specific number. * * The base implementation only distinguishes between singular and plural, * so it always returns the second (index 1) plural form. Subclasses that * know a language's full plural rules should override this method. * * @param int $count * Number to return plural for. * @param string|null $langcode * (optional) Language code to translate to a language other than what is * used to display the page. * * @return int * The numeric index of the plural variant to use for this $langcode and * $count combination. */ protected function getPluralIndex(int $count, ?string $langcode = NULL): int { return 1; } /** * Translates a string to the current language or to a given language. * Loading