diff --git a/core/includes/common.inc b/core/includes/common.inc index c7e42f7ade..b91793e199 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -12,6 +12,7 @@ use Drupal\Component\Utility\SortArray; use Drupal\Core\Cache\Cache; use Drupal\Core\DrupalKernel; +use Drupal\Core\StringTranslation\ByteSizeMarkup; /** * @defgroup php_wrappers PHP wrapper functions @@ -141,7 +142,7 @@ */ function format_size($size, $langcode = NULL) { @trigger_error('format_size() is deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. Use \Drupal::translation()->formatByteSize($size, $langcode) instead. See https://www.drupal.org/node/2999981', E_USER_DEPRECATED); - return \Drupal::translation()->formatByteSize($size, $langcode); + return new ByteSizeMarkup($size, ['langcode' => $langcode]); } /** diff --git a/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php b/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php index 0c73fc8f8a..61f37190bc 100644 --- a/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php +++ b/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php @@ -89,13 +89,11 @@ protected function formatPlural($count, $singular, $plural, array $args = [], ar * (optional) Language code to translate to a language other than what is * used to display the page. * - * @return \Drupal\Core\StringTranslation\TranslatableMarkup + * @return \Drupal\Core\StringTranslation\ByteSizeMarkup * A translated string representation of the size. - * - * @see \Drupal\Core\StringTranslation\TranslationInterface::formatByteSize() */ - protected function formatByteSize($size, ?string $langcode = NULL): TranslatableMarkup { - return $this->getStringTranslation()->formatByteSize($size, $langcode); + protected function formatByteSize($size, ?string $langcode = NULL): ByteSizeMarkup { + return new ByteSizeMarkup($size, ['langcode' => $langcode], $this->getStringTranslation()); } /** diff --git a/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php b/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php index c49b44cd3f..7d9de54339 100644 --- a/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php +++ b/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php @@ -125,9 +125,9 @@ public function formatPlural($count, $singular, $plural, array $args = [], array * (optional) Language code to translate to a language other than what is * used to display the page. * - * @return \Drupal\Core\StringTranslation\TranslatableMarkup + * @return \Drupal\Core\StringTranslation\ByteSizeMarkup * A translated string representation of the size. */ - public function formatByteSize($size, ?string $langcode = NULL): TranslatableMarkup; + public function formatByteSize($size, ?string $langcode = NULL): ByteSizeMarkup; } diff --git a/core/lib/Drupal/Core/StringTranslation/TranslationManager.php b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php index 1e4f0eb93a..8f1784fe3c 100644 --- a/core/lib/Drupal/Core/StringTranslation/TranslationManager.php +++ b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php @@ -157,50 +157,8 @@ public function formatPlural($count, $singular, $plural, array $args = [], array /** * {@inheritdoc} */ - public function formatByteSize($size, ?string $langcode = NULL): TranslatableMarkup { - $options = ['langcode' => $langcode]; - - $absolute_size = abs($size); - if ($absolute_size < Bytes::KILOBYTE) { - return new PluralTranslatableMarkup($size, '1 byte', '@count bytes', [], $options, $this); - } - // Create a multiplier to preserve the sign of $size. - $sign = $absolute_size / $size; - foreach (['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] as $unit) { - $absolute_size /= Bytes::KILOBYTE; - $rounded_size = round($absolute_size, 2); - if ($rounded_size < Bytes::KILOBYTE) { - break; - } - } - - $args = ['@size' => $rounded_size * $sign]; - switch ($unit) { - case 'KB': - return new TranslatableMarkup('@size KB', $args, $options, $this); - - case 'MB': - return new TranslatableMarkup('@size MB', $args, $options, $this); - - case 'GB': - return new TranslatableMarkup('@size GB', $args, $options, $this); - - case 'TB': - return new TranslatableMarkup('@size TB', $args, $options, $this); - - case 'PB': - return new TranslatableMarkup('@size PB', $args, $options, $this); - - case 'EB': - return new TranslatableMarkup('@size EB', $args, $options, $this); - - case 'ZB': - return new TranslatableMarkup('@size ZB', $args, $options, $this); - - case 'YB': - return new TranslatableMarkup('@size YB', $args, $options, $this); - - } + public function formatByteSize($size, ?string $langcode = NULL): ByteSizeMarkup { + return new ByteSizeMarkup($size, ['langcode' => $langcode], $this); } /** diff --git a/core/lib/Drupal/Core/StringTranslation/ByteSizeMarkup.php b/core/lib/Drupal/Core/StringTranslation/ByteSizeMarkup.php new file mode 100644 index 0000000000..83d41fc830 --- /dev/null +++ b/core/lib/Drupal/Core/StringTranslation/ByteSizeMarkup.php @@ -0,0 +1,125 @@ +size = $size; + parent::__construct('', [], $options, $string_translation); + } + + /** + * Renders the object as a string. + * + * @return string + * The translated string. + */ + public function render() { + if (!isset($this->markup)) { + $absolute_size = abs($this->size); + if ($absolute_size < Bytes::KILOBYTE) { + $this->markup = new PluralTranslatableMarkup($this->size, '1 byte', '@count bytes', [], $this->options, $this->stringTranslation); + } + else { + // Create a multiplier to preserve the sign of $size. + $sign = $absolute_size / $this->size; + foreach (['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] as $unit) { + $absolute_size /= Bytes::KILOBYTE; + $rounded_size = round($absolute_size, 2); + if ($rounded_size < Bytes::KILOBYTE) { + break; + } + } + + $args = ['@size' => $rounded_size * $sign]; + switch ($unit) { + case 'KB': + $this->markup = new TranslatableMarkup('@size KB', $args, $this->options, $this->stringTranslation); + break; + + case 'MB': + $this->markup = new TranslatableMarkup('@size MB', $args, $this->options, $this->stringTranslation); + break; + + case 'GB': + $this->markup = new TranslatableMarkup('@size GB', $args, $this->options, $this->stringTranslation); + break; + + case 'TB': + $this->markup = new TranslatableMarkup('@size TB', $args, $this->options, $this->stringTranslation); + break; + + case 'PB': + $this->markup = new TranslatableMarkup('@size PB', $args, $this->options, $this->stringTranslation); + break; + + case 'EB': + $this->markup = new TranslatableMarkup('@size EB', $args, $this->options, $this->stringTranslation); + break; + + case 'ZB': + $this->markup = new TranslatableMarkup('@size ZB', $args, $this->options, $this->stringTranslation); + break; + + case 'YB': + $this->markup = new TranslatableMarkup('@size YB', $args, $this->options, $this->stringTranslation); + break; + + } + } + } + if (!$this->markup) { + // Unsupported byte-size. + return $this->markup = ''; + } + + return $this->markup->render(); + } + + /** + * {@inheritdoc} + */ + public function __sleep() { + return array_merge(parent::__sleep(), ['size']); + } + +}