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.
Requires
- php: ^8.3
- illuminate/contracts: ^10.0||^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
- symfony/polyfill-intl-icu: ^1.27
Requires (Dev)
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- spatie/laravel-ray: ^1.35
README
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 onlyordinal()
: Convert a number to its ordinal form (e.g., 1st, 2nd, 3rd) - English onlyspellOrdinal()
: Spell out a number in ordinal form (e.g., "first", "second", "third") - English onlyfileSize()
: Convert bytes to a human-readable file sizeclamp()
: Clamp a number between min and max valuespairs()
: Split a number into pairs of min/max valuestrim()
: Remove trailing zeros from a numberwithLocale()
: Execute a callback with a specific localewithCurrency()
: Execute a callback with a specific currencyuseLocale()
: Set the default localeuseCurrency()
: Set the default currencydefaultLocale()
: Get the current default localedefaultCurrency()
: 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:
- Text-based methods (
spell()
,ordinal()
,spellOrdinal()
) only fully work in English - For non-English locales, text-based methods will either:
- Fall back to the English implementation
- Or return basic formatted numbers instead of text
- 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:
- Installing the PHP intl extension if possible
- 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:
- When your application boots, it checks if the intl extension is installed
- If intl is available, by default the package does nothing and allows Laravel to use its built-in Number class
- 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.