Skip to content

Commit 7a062ea

Browse files
committed
fix(Misc): parse i18n file sizes with Unicode thousands separators
Handle size_format() output that uses NBSP/NNBSP and other Zs chars from number_format_i18n() (e.g. fr_FR). Use UTF-8 regex with a lazy value group, early return on no match, and strip group separators for _n() pluralization.
1 parent 4204621 commit 7a062ea

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

inc/Helpers/Misc.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,16 @@ function get_file_detail( string $file_name, string $file_ext, string $file_size
8484
* @return string
8585
*/
8686
function get_accessible_file_size_label( string $file_size ): string {
87-
// Extract value and unit from file size (e.g., "7ko" → "7" + "ko").
88-
preg_match( '/^([\d.,]+)\s*([a-zA-Z]+)$/', $file_size, $matches );
89-
$value = $matches[1] ?? '';
90-
$int_value = (int) $value; // Cast to int for _n() pluralization.
91-
$unit = strtolower( $matches[2] ?? '' );
87+
// Extract value and unit (e.g. "7ko" or "1\u{00A0}000 KB" from i18n thousands separators).
88+
// UTF-8 mode: allow NBSP/NNBSP inside the value; a non-possessive +? so the last \s* is the gap before the unit, not the thousands separator.
89+
if ( 1 !== preg_match( '/^([\d\.,\p{Zs}]+?)\s*+([a-zA-Z]+)$/u', $file_size, $matches ) ) {
90+
return $file_size;
91+
}
92+
93+
$value = $matches[1] ?? '';
94+
$unit = strtolower( $matches[2] ?? '' );
95+
// Strip group separators (ASCII space, NBSP, NNBSP) for _n() plural; (int) leaves decimals as floor (e.g. 1.5 -> 1).
96+
$int_value = (int) str_replace( [ ' ', "\u{00A0}", "\u{202F}" ], '', $value );
9297

9398
/* translators: file size units (byte, kilobyte, megabyte, etc.) */
9499
$unit_label = match ( $unit ) {

0 commit comments

Comments
 (0)