diff --git src/wp-includes/class-wp-locale.php src/wp-includes/class-wp-locale.php
index 287914e880..10e4201ad3 100644
|
|
class WP_Locale { |
198 | 198 | /* translators: $thousands_sep argument for https://secure.php.net/number_format, default is , */ |
199 | 199 | $thousands_sep = __( 'number_format_thousands_sep' ); |
200 | 200 | |
201 | | if ( version_compare( PHP_VERSION, '5.4', '>=' ) ) { |
202 | | // Replace space with a non-breaking space to avoid wrapping. |
203 | | $thousands_sep = str_replace( ' ', ' ', $thousands_sep ); |
204 | | } else { |
205 | | // PHP < 5.4.0 does not support multiple bytes in thousands separator. |
206 | | $thousands_sep = str_replace( array( ' ', ' ' ), ' ', $thousands_sep ); |
207 | | } |
208 | | |
209 | 201 | $this->number_format['thousands_sep'] = ( 'number_format_thousands_sep' === $thousands_sep ) ? ',' : $thousands_sep; |
210 | 202 | |
211 | 203 | /* translators: $dec_point argument for https://secure.php.net/number_format, default is . */ |
diff --git src/wp-includes/functions.php src/wp-includes/functions.php
index 8ae58299d6..ad911ab930 100644
|
|
function number_format_i18n( $number, $decimals = 0 ) { |
262 | 262 | global $wp_locale; |
263 | 263 | |
264 | 264 | if ( isset( $wp_locale ) ) { |
265 | | $formatted = number_format( $number, absint( $decimals ), $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] ); |
| 265 | $decimal_point = $wp_locale->number_format['decimal_point']; |
| 266 | $thousands_sep = $wp_locale->number_format['thousands_sep']; |
| 267 | |
| 268 | if ( version_compare( PHP_VERSION, '5.4', '<' ) && ( 1 !== strlen( $decimal_point ) || 1 !== strlen( $thousands_sep ) ) ) { |
| 269 | $formatted = strtr( number_format( $number, absint( $decimals ), '.', ',' ), array( |
| 270 | '.' => $decimal_point, |
| 271 | ',' => $thousands_sep, |
| 272 | ) ); |
| 273 | } else { |
| 274 | $formatted = number_format( $number, absint( $decimals ), $decimal_point, $thousands_sep ); |
| 275 | } |
266 | 276 | } else { |
267 | 277 | $formatted = number_format( $number, absint( $decimals ) ); |
268 | 278 | } |
diff --git tests/phpunit/tests/functions/numberFormatI18n.php tests/phpunit/tests/functions/numberFormatI18n.php
index 8c514b319c..612a5d70b5 100644
|
|
class Tests_Functions_Number_Format_I18n extends WP_UnitTestCase { |
46 | 46 | $this->assertEquals( '123,457', number_format_i18n( 123456.789, 0 ) ); |
47 | 47 | $this->assertEquals( '123,456.7890', number_format_i18n( 123456.789, -4 ) ); |
48 | 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Test multiybte format of locale. |
| 52 | * |
| 53 | * Multibyte decimal point and thousands separator in `number_format` are suported since PHP 5.4. |
| 54 | * @link http://php.net/number_format |
| 55 | * |
| 56 | * Therefor this test will fail on PHP < 5.4 with incorrect implemented `number_format_i18n` function. |
| 57 | */ |
| 58 | public function test_multibyte_format_of_locale() { |
| 59 | $decimal_point = $GLOBALS['wp_locale']->number_format['decimal_point']; |
| 60 | $thousands_sep = $GLOBALS['wp_locale']->number_format['thousands_sep']; |
| 61 | |
| 62 | $GLOBALS['wp_locale']->number_format['decimal_point'] = 'decimal_point'; |
| 63 | $GLOBALS['wp_locale']->number_format['thousands_sep'] = 'thousands_sep'; |
| 64 | |
| 65 | $actual_1 = number_format_i18n( 123456.789, 0 ); |
| 66 | $actual_2 = number_format_i18n( 123456.789, 4 ); |
| 67 | |
| 68 | $GLOBALS['wp_locale']->number_format['decimal_point'] = $decimal_point; |
| 69 | $GLOBALS['wp_locale']->number_format['thousands_sep'] = $thousands_sep; |
| 70 | |
| 71 | $this->assertEquals( '123thousands_sep457', $actual_1 ); |
| 72 | $this->assertEquals( '123thousands_sep456decimal_point7890', $actual_2 ); |
| 73 | } |
49 | 74 | } |