padmission/number-no-intl

A Laravel package that automatically replaces the default Laravel Number class with a version that doesn't require the intl extension.

v0.1 2025-05-06 22:31 UTC

This package is auto-updated.

Last update: 2025-05-06 22:35:45 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A Laravel package that automatically replaces the default Laravel Number class with a version that doesn't require the PHP intl extension. This is particularly useful for environments where the intl extension cannot be installed or is unavailable.

Important Note

This package only fully supports the English ('en') locale. When using other locales, the formatting will still work, but text-based methods like spell() and spellOrdinal() will fall back to the English implementation or basic number formatting. This is a limitation of the implementation without the intl extension.

Features

  • Zero Configuration: Once installed, automatically replaces Laravel's Number class when the intl extension is not available
  • Fully Compatible: Implements all the methods from Laravel's original Number class
  • Smart Detection: Only activates when needed (when intl extension is not available)
  • Symfony Polyfill: Uses the Symfony polyfill for intl-icu to provide compatibility
  • English-Only: Full functionality is available in the English locale, with limited support for other locales

Installation

You can install the package via composer:

composer require padmission/number-no-intl

That's it! No additional configuration is needed. The package will automatically detect if the intl extension is missing and replace Laravel's Number class accordingly.

Usage

Use Laravel's Number class as you normally would. The package will transparently replace it if the intl extension is not available:

use Illuminate\Support\Number;

// Format a number
echo Number::format(1234.56, 2); // "1,234.56"

// Format as currency
echo Number::currency(1234.56, 'USD'); // "$1,234.56"

// Format as percentage
echo Number::percentage(75.5); // "76%"

// Format for humans
echo Number::forHumans(1234567); // "1.23 million"

// Format abbreviated
echo Number::abbreviate(1234567); // "1.23M"

All other methods from Laravel's Number class are also available:

  • spell(): Spell out a number in words (e.g., "one hundred and twenty-three") - English only
  • ordinal(): Convert a number to its ordinal form (e.g., 1st, 2nd, 3rd) - English only
  • spellOrdinal(): Spell out a number in ordinal form (e.g., "first", "second", "third") - English only
  • fileSize(): Convert bytes to a human-readable file size
  • clamp(): Clamp a number between min and max values
  • pairs(): Split a number into pairs of min/max values
  • trim(): Remove trailing zeros from a number
  • withLocale(): Execute a callback with a specific locale
  • withCurrency(): Execute a callback with a specific currency
  • useLocale(): Set the default locale
  • useCurrency(): Set the default currency
  • defaultLocale(): Get the current default locale
  • defaultCurrency(): Get the current default currency

Locale Support Limitations

While the package allows you to set different locales using useLocale() or withLocale(), be aware of these limitations:

  1. Text-based methods (spell(), ordinal(), spellOrdinal()) only fully work in English
  2. For non-English locales, text-based methods will either:
    • Fall back to the English implementation
    • Or return basic formatted numbers instead of text
  3. Basic formatting functions (format(), currency(), percentage(), etc.) work for all locales, but use standard English number formatting conventions (comma as thousands separator, period as decimal separator)

If you need full multi-language support, consider:

  1. Installing the PHP intl extension if possible
  2. Or implementing language-specific formatters for your required languages

Configuration

While the package works out of the box with zero configuration, you can publish the configuration file to customize its behavior:

php artisan vendor:publish --tag="number-no-intl-config"

This will create a config/number-no-intl.php file with the following options:

return [
    // Default locale to use for formatting (only 'en' is fully supported)
    'locale' => env('NUMBER_NO_INTL_LOCALE', 'en'),

    // Default currency to use
    'currency' => env('NUMBER_NO_INTL_CURRENCY', 'USD'),

    // Whether to automatically replace Laravel's Number class
    // when the intl extension is not available
    'auto_replace' => env('NUMBER_NO_INTL_AUTO_REPLACE', true),
];

How It Works

The package uses the following approach:

  1. When your application boots, it checks if the intl extension is installed
  2. If intl is available, by default the package does nothing and allows Laravel to use its built-in Number class
  3. If intl is not available, the package automatically binds a polyfill implementation that uses the Symfony polyfill for intl-icu

This ensures that your application works seamlessly across environments, regardless of whether the intl extension is installed or not.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.