Skip to content

Commit

Permalink
Add check for existence of NumberFormatter and numfmt_create
Browse files Browse the repository at this point in the history
  • Loading branch information
maxschmeling committed Jan 22, 2025
1 parent eba3d2c commit c6797c1
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions inc/Formatting/FieldFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ final class FieldFormatter {
* @psalm-suppress UndefinedClass
*/
public static function format_currency( mixed $value, ?string $iso_4127_currency_code = null, ?string $locale = null ): string {
// The PHP 'intl' extension is not available in WordPress Playground.
if ( ! class_exists( 'NumberFormatter' ) || ! function_exists( 'numfmt_create' ) ) {
return strval($value);
}

$format = numfmt_create( $locale ?? get_locale(), NumberFormatter::CURRENCY );
$currency_code = $iso_4127_currency_code ?? $format->getTextAttribute( NumberFormatter::CURRENCY_CODE );
return numfmt_format_currency( $format, (float) $value, $currency_code );
Expand Down

0 comments on commit c6797c1

Please sign in to comment.