diff --git a/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php b/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php index bc560cf..678c89e 100644 --- a/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php +++ b/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php @@ -41,7 +41,7 @@ public function translate($string, array $args = array(), array $options = array * * This function ensures that the string is pluralized correctly. Since t() is * called by this function, make sure not to pass already-localized strings to - * it. + * it. See formatPluralTranslated() for that. * * For example: * @code @@ -71,7 +71,8 @@ public function translate($string, array $args = array(), array $options = array * 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 format_string(). Note that you do not need to include @count - * in this array; this replacement is done automatically for the plural case. + * 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. * @@ -82,7 +83,42 @@ public function translate($string, array $args = array(), array $options = array * @see \Drupal\Component\Utility\String * @see t() * @see format_string() + * @see self::formatPluralTranslated */ public function formatPlural($count, $singular, $plural, array $args = array(), array $options = array()); + /** + * Formats an already translated string containing a count of items. + * + * This function ensures that the string is pluralized correctly. As opposed + * to the formatPlural() method, this method is designed to be invoked with + * a string already translated (such as with configuration translation). + * + * @param int $count + * The item count to display. + * @param string $translation + * The string containing the translation of a singular/plural pair. It may + * contain any number of possible variants (depending on the language + * translated to) separated by the value of the LOCALE_PLURAL_DELIMITER + * constant. + * @param array $args + * Associative array of replacements to make in the 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 format_string(). 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. t() defines all allowed keys. + * The 'context' key is not supported because the passed string is already + * translated. + * + * @return string + * The correct substring for the given $count with $args replaced. + * + * @see self::formatPlural + * @see format_string() + */ + public function formatPluralTranslated($count, $translation, array $args = array(), array $options = array()); + } diff --git a/core/lib/Drupal/Core/StringTranslation/TranslationManager.php b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php index cdfc2df..2d165bf 100644 --- a/core/lib/Drupal/Core/StringTranslation/TranslationManager.php +++ b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php @@ -157,8 +157,15 @@ public function formatPlural($count, $singular, $plural, array $args = array(), $translatable_string = implode(LOCALE_PLURAL_DELIMITER, array($singular, $plural)); // Translate as usual. $translated_strings = $this->translate($translatable_string, $args, $options); + return $this->formatPluralTranslated($count, $translated_strings, $args, $options); + } + + /** + * {@inheritdoc} + */ + public function formatPluralTranslated($count, $translation, array $args = array(), array $options = array()) { // Split joined translation strings into array. - $translated_array = explode(LOCALE_PLURAL_DELIMITER, $translated_strings); + $translated_array = explode(LOCALE_PLURAL_DELIMITER, $translation); if ($count == 1) { return SafeMarkup::set($translated_array[0]); @@ -183,7 +190,13 @@ public function formatPlural($count, $singular, $plural, array $args = array(), $return = $translated_array[1]; } } - return SafeMarkup::set($return); + + if (empty($args)) { + return SafeMarkup::set($return); + } + else { + return String::format($return, $args); + } } /** diff --git a/core/modules/config_translation/src/FormElement/FormElementBase.php b/core/modules/config_translation/src/FormElement/FormElementBase.php index 7db01e1..19114ee 100644 --- a/core/modules/config_translation/src/FormElement/FormElementBase.php +++ b/core/modules/config_translation/src/FormElement/FormElementBase.php @@ -91,6 +91,9 @@ public function getTranslationBuild(LanguageInterface $source_language, Language * A render array for the source value. */ protected function getSourceElement(LanguageInterface $source_language, $source_config) { + // @todo Should be able to render source as singular+plurals. Similar + // to TranslateEditForm::buildForm(), but here the source may also be + // multi-plural. if ($source_config) { $value = '' . nl2br($source_config) . ''; } @@ -161,6 +164,8 @@ protected function getSourceElement(LanguageInterface $source_language, $source_ */ protected function getTranslationElement(LanguageInterface $translation_language, $source_config, $translation_config) { // Add basic properties that apply to all form elements. + // @todo Needs support for possibly singular+plurals input if the source + // was singular/plural. As in TranslateEditForm::buildForm(). return array( '#title' => $this->t('!label (!source_language)', array( '!label' => $this->t($this->definition['label']),