diff --git a/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php b/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php
index fcc4d422a6..bd1b4cd77f 100644
--- a/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php
+++ b/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php
@@ -121,7 +121,7 @@ public function formatPlural($count, $singular, $plural, array $args = [], array
* @return \Drupal\Core\StringTranslation\TranslatableMarkup|\Drupal\Core\StringTranslation\PluralTranslatableMarkup
* A translated string representation of the size.
*
- * @see \Drupal\Component\Utility\Bytes::toString
+ * @see \Drupal\Component\Utility\Bytes::toString()
*/
public function formatSize($size, $langcode = NULL);
diff --git a/core/lib/Drupal/Core/StringTranslation/TranslationManager.php b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php
index 6dcd2b248f..29576246c3 100644
--- a/core/lib/Drupal/Core/StringTranslation/TranslationManager.php
+++ b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php
@@ -159,7 +159,7 @@ public function formatPlural($count, $singular, $plural, array $args = [], array
*/
public function formatSize($size, $langcode = NULL) {
$options = ['langcode' => $langcode];
- list($rounded_size, $unit) = explode(' ', Bytes::toString($size));
+ list($rounded_size, $unit) = explode(' ', Bytes::toString($size), 2);
$args = ['@size' => $rounded_size];
switch ($unit) {
case 'B':
diff --git a/core/modules/file/file.module b/core/modules/file/file.module
index 5b7427c4d2..7a815c6a63 100644
--- a/core/modules/file/file.module
+++ b/core/modules/file/file.module
@@ -379,15 +379,16 @@ function file_validate_extensions(FileInterface $file, $extensions) {
*/
function file_validate_size(FileInterface $file, $file_limit = 0, $user_limit = 0) {
$user = \Drupal::currentUser();
+ $translation = \Drupal::translation();
$errors = [];
if ($file_limit && $file->getSize() > $file_limit) {
- $errors[] = t('The file is %filesize exceeding the maximum file size of %maxsize.', ['%filesize' => \Drupal::translation()->formatSize($file->getSize()), '%maxsize' => \Drupal::translation()->formatSize($file_limit)]);
+ $errors[] = t('The file is %filesize exceeding the maximum file size of %maxsize.', ['%filesize' => $translation->formatSize($file->getSize()), '%maxsize' => $translation->formatSize($file_limit)]);
}
// Save a query by only calling spaceUsed() when a limit is provided.
if ($user_limit && (\Drupal::entityManager()->getStorage('file')->spaceUsed($user->id()) + $file->getSize()) > $user_limit) {
- $errors[] = t('The file is %filesize which would exceed your disk quota of %quota.', ['%filesize' => \Drupal::translation()->formatSize($file->getSize()), '%quota' => \Drupal::translation()->formatSize($user_limit)]);
+ $errors[] = t('The file is %filesize which would exceed your disk quota of %quota.', ['%filesize' => $translation->formatSize($file->getSize()), '%quota' => $translation->formatSize($user_limit)]);
}
return $errors;
@@ -860,6 +861,7 @@ function _file_save_upload_from_form(array $element, FormStateInterface $form_st
*/
function file_save_upload($form_field_name, $validators = [], $destination = FALSE, $delta = NULL, $replace = FILE_EXISTS_RENAME) {
$user = \Drupal::currentUser();
+ $translation = \Drupal::translation();
static $upload_cache;
$all_files = \Drupal::request()->files->get('files', []);
@@ -893,7 +895,7 @@ function file_save_upload($form_field_name, $validators = [], $destination = FAL
switch ($file_info->getError()) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
- \Drupal::messenger()->addError(t('The file %file could not be saved because it exceeds %maxsize, the maximum allowed size for uploads.', ['%file' => $file_info->getFilename(), '%maxsize' => \Drupal::translation()->formatSize(file_upload_max_size())]));
+ \Drupal::messenger()->addError(t('The file %file could not be saved because it exceeds %maxsize, the maximum allowed size for uploads.', ['%file' => $file_info->getFilename(), '%maxsize' => $translation->formatSize(file_upload_max_size())]));
$files[$i] = FALSE;
continue 2;
@@ -1101,6 +1103,7 @@ function file_file_predelete(File $file) {
*/
function file_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$token_service = \Drupal::token();
+ $translation = \Drupal::translation();
$url_options = ['absolute' => TRUE];
if (isset($options['langcode'])) {
@@ -1138,7 +1141,7 @@ function file_tokens($type, $tokens, array $data, array $options, BubbleableMeta
break;
case 'size':
- $replacements[$original] = \Drupal::translation()->formatSize($file->getSize());
+ $replacements[$original] = $translation->formatSize($file->getSize());
break;
case 'url':
diff --git a/core/modules/file/src/Controller/FileWidgetAjaxController.php b/core/modules/file/src/Controller/FileWidgetAjaxController.php
index bf1e4ac91c..ad60d08587 100644
--- a/core/modules/file/src/Controller/FileWidgetAjaxController.php
+++ b/core/modules/file/src/Controller/FileWidgetAjaxController.php
@@ -2,12 +2,14 @@
namespace Drupal\file\Controller;
+use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Defines a controller to respond to file widget AJAX requests.
*/
class FileWidgetAjaxController {
+ use StringTranslationTrait;
/**
* Returns the progress status for a file upload process.
@@ -28,14 +30,14 @@ public function progress($key) {
if ($implementation == 'uploadprogress') {
$status = uploadprogress_get_info($key);
if (isset($status['bytes_uploaded']) && !empty($status['bytes_total'])) {
- $progress['message'] = t('Uploading... (@current of @total)', ['@current' => \Drupal::translation()->formatSize($status['bytes_uploaded']), '@total' => \Drupal::translation()->formatSize($status['bytes_total'])]);
+ $progress['message'] = t('Uploading... (@current of @total)', ['@current' => $this->formatSize($status['bytes_uploaded']), '@total' => $this->formatSize($status['bytes_total'])]);
$progress['percentage'] = round(100 * $status['bytes_uploaded'] / $status['bytes_total']);
}
}
elseif ($implementation == 'apc') {
$status = apcu_fetch('upload_' . $key);
if (isset($status['current']) && !empty($status['total'])) {
- $progress['message'] = t('Uploading... (@current of @total)', ['@current' => \Drupal::translation()->formatSize($status['current']), '@total' => \Drupal::translation()->formatSize($status['total'])]);
+ $progress['message'] = t('Uploading... (@current of @total)', ['@current' => $this->formatSize($status['current']), '@total' => $this->formatSize($status['total'])]);
$progress['percentage'] = round(100 * $status['current'] / $status['total']);
}
}
diff --git a/core/modules/file/src/Plugin/Field/FieldFormatter/FileSize.php b/core/modules/file/src/Plugin/Field/FieldFormatter/FileSize.php
index 91a0a71361..0971478665 100644
--- a/core/modules/file/src/Plugin/Field/FieldFormatter/FileSize.php
+++ b/core/modules/file/src/Plugin/Field/FieldFormatter/FileSize.php
@@ -33,7 +33,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
- $elements[$delta] = ['#markup' => \Drupal::translation()->formatSize($item->value)];
+ $elements[$delta] = ['#markup' => $this->formatSize($item->value)];
}
return $elements;
diff --git a/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php b/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php
index ec94f8998d..2aaef72bf6 100644
--- a/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php
+++ b/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php
@@ -39,7 +39,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
],
],
],
- ['data' => \Drupal::translation()->formatSize($file->getSize())],
+ ['data' => $this->formatSize($file->getSize())],
];
}
diff --git a/core/modules/file/src/Plugin/Field/FieldType/FileItem.php b/core/modules/file/src/Plugin/Field/FieldType/FileItem.php
index d42cd7f096..635d884942 100644
--- a/core/modules/file/src/Plugin/Field/FieldType/FileItem.php
+++ b/core/modules/file/src/Plugin/Field/FieldType/FileItem.php
@@ -177,7 +177,7 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
'#type' => 'textfield',
'#title' => t('Maximum upload size'),
'#default_value' => $settings['max_filesize'],
- '#description' => t('Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the allowed file size. If left empty the file sizes will be limited only by PHP\'s maximum post and file upload sizes (current limit %limit).', ['%limit' => \Drupal::translation()->formatSize(file_upload_max_size())]),
+ '#description' => t('Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the allowed file size. If left empty the file sizes will be limited only by PHP\'s maximum post and file upload sizes (current limit %limit).', ['%limit' => $this->formatSize(file_upload_max_size())]),
'#size' => 10,
'#element_validate' => [[get_class($this), 'validateMaxFilesize']],
'#weight' => 5,
diff --git a/core/modules/file/tests/src/Functional/FileFieldValidateTest.php b/core/modules/file/tests/src/Functional/FileFieldValidateTest.php
index 367c2b824e..36c6719df4 100644
--- a/core/modules/file/tests/src/Functional/FileFieldValidateTest.php
+++ b/core/modules/file/tests/src/Functional/FileFieldValidateTest.php
@@ -83,6 +83,7 @@ public function testFileMaxSize() {
'1048576' => 1048576,
];
+ $translation = \Drupal::translation();
foreach ($sizes as $max_filesize => $file_limit) {
// Set the max file upload size.
$this->updateFileField($field_name, $type_name, ['max_filesize' => $max_filesize]);
@@ -92,13 +93,13 @@ public function testFileMaxSize() {
$node_storage->resetCache([$nid]);
$node = $node_storage->load($nid);
$node_file = File::load($node->{$field_name}->target_id);
- $this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) under the max limit (%maxsize).', ['%filesize' => \Drupal::translation()->formatSize($small_file->getSize()), '%maxsize' => $max_filesize]));
- $this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) under the max limit (%maxsize).', ['%filesize' => \Drupal::translation()->formatSize($small_file->getSize()), '%maxsize' => $max_filesize]));
+ $this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) under the max limit (%maxsize).', ['%filesize' => $translation->formatSize($small_file->getSize()), '%maxsize' => $max_filesize]));
+ $this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) under the max limit (%maxsize).', ['%filesize' => $translation->formatSize($small_file->getSize()), '%maxsize' => $max_filesize]));
// Check that uploading the large file fails (1M limit).
$this->uploadNodeFile($large_file, $field_name, $type_name);
- $error_message = t('The file is %filesize exceeding the maximum file size of %maxsize.', ['%filesize' => \Drupal::translation()->formatSize($large_file->getSize()), '%maxsize' => \Drupal::translation()->formatSize($file_limit)]);
- $this->assertRaw($error_message, format_string('Node save failed when file (%filesize) exceeded the max upload size (%maxsize).', ['%filesize' => \Drupal::translation()->formatSize($large_file->getSize()), '%maxsize' => $max_filesize]));
+ $error_message = t('The file is %filesize exceeding the maximum file size of %maxsize.', ['%filesize' => $translation->formatSize($large_file->getSize()), '%maxsize' => $translation->formatSize($file_limit)]);
+ $this->assertRaw($error_message, format_string('Node save failed when file (%filesize) exceeded the max upload size (%maxsize).', ['%filesize' => $translation->formatSize($large_file->getSize()), '%maxsize' => $max_filesize]));
}
// Turn off the max filesize.
@@ -109,8 +110,8 @@ public function testFileMaxSize() {
$node_storage->resetCache([$nid]);
$node = $node_storage->load($nid);
$node_file = File::load($node->{$field_name}->target_id);
- $this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) with no max limit.', ['%filesize' => \Drupal::translation()->formatSize($large_file->getSize())]));
- $this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) with no max limit.', ['%filesize' => \Drupal::translation()->formatSize($large_file->getSize())]));
+ $this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) with no max limit.', ['%filesize' => $translation->formatSize($large_file->getSize())]));
+ $this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) with no max limit.', ['%filesize' => $translation->formatSize($large_file->getSize())]));
}
/**
diff --git a/core/modules/file/tests/src/Functional/FileTokenReplaceTest.php b/core/modules/file/tests/src/Functional/FileTokenReplaceTest.php
index 505620466c..729e0643cc 100644
--- a/core/modules/file/tests/src/Functional/FileTokenReplaceTest.php
+++ b/core/modules/file/tests/src/Functional/FileTokenReplaceTest.php
@@ -21,6 +21,7 @@ public function testFileTokenReplacement() {
$node_storage = $this->container->get('entity.manager')->getStorage('node');
$token_service = \Drupal::token();
$language_interface = \Drupal::languageManager()->getCurrentLanguage();
+ $translation = \Drupal::translation();
// Create file field.
$type_name = 'article';
@@ -46,7 +47,7 @@ public function testFileTokenReplacement() {
$tests['[file:name]'] = Html::escape($file->getFilename());
$tests['[file:path]'] = Html::escape($file->getFileUri());
$tests['[file:mime]'] = Html::escape($file->getMimeType());
- $tests['[file:size]'] = \Drupal::translation()->formatSize($file->getSize());
+ $tests['[file:size]'] = $translation->formatSize($file->getSize());
$tests['[file:url]'] = Html::escape(file_create_url($file->getFileUri()));
$tests['[file:created]'] = format_date($file->getCreatedTime(), 'medium', '', NULL, $language_interface->getId());
$tests['[file:created:short]'] = format_date($file->getCreatedTime(), 'short', '', NULL, $language_interface->getId());
@@ -87,7 +88,7 @@ public function testFileTokenReplacement() {
$tests['[file:name]'] = $file->getFilename();
$tests['[file:path]'] = $file->getFileUri();
$tests['[file:mime]'] = $file->getMimeType();
- $tests['[file:size]'] = \Drupal::translation()->formatSize($file->getSize());
+ $tests['[file:size]'] = $translation->formatSize($file->getSize());
foreach ($tests as $input => $expected) {
$output = $token_service->replace($input, ['file' => $file], ['langcode' => $language_interface->getId(), 'sanitize' => FALSE]);
diff --git a/core/modules/migrate/tests/src/Unit/TestMigrateExecutable.php b/core/modules/migrate/tests/src/Unit/TestMigrateExecutable.php
index 312472d734..b234af5c74 100644
--- a/core/modules/migrate/tests/src/Unit/TestMigrateExecutable.php
+++ b/core/modules/migrate/tests/src/Unit/TestMigrateExecutable.php
@@ -122,11 +122,4 @@ public function setMemoryThreshold($threshold) {
$this->memoryThreshold = $threshold;
}
- /**
- * {@inheritdoc}
- */
- protected function formatSize($size) {
- return $size;
- }
-
}
diff --git a/core/modules/user/tests/src/Unit/PermissionHandlerTest.php b/core/modules/user/tests/src/Unit/PermissionHandlerTest.php
index 391e63087c..384f00cf6b 100644
--- a/core/modules/user/tests/src/Unit/PermissionHandlerTest.php
+++ b/core/modules/user/tests/src/Unit/PermissionHandlerTest.php
@@ -466,7 +466,7 @@ public function formatPlural($count, $singular, $plural, array $args = [], array
*/
public function formatSize($size, $langcode = NULL) {
$options = ['langcode' => $langcode];
- list($rounded_size, $unit) = explode(' ', Bytes::toString($size));
+ list($rounded_size, $unit) = explode(' ', Bytes::toString($size), 2);
$args = ['@size' => $rounded_size];
switch ($unit) {
case 'B':
diff --git a/core/modules/views/src/Plugin/views/field/FileSize.php b/core/modules/views/src/Plugin/views/field/FileSize.php
index 5a350218f8..ed744da8c5 100644
--- a/core/modules/views/src/Plugin/views/field/FileSize.php
+++ b/core/modules/views/src/Plugin/views/field/FileSize.php
@@ -51,7 +51,7 @@ public function render(ResultRow $values) {
return $value;
case 'formatted':
default:
- return \Drupal::translation()->formatSize($value);
+ return $this->formatSize($value);
}
}
else {