symfony/polyfill-intl-icu
Fallback implementations for PHP’s Intl ICU features when the intl extension isn’t installed. Provides limited “en” locale support for intl error functions plus Collator, NumberFormatter, Locale, IntlDateFormatter and IntlListFormatter.
Install the Package:
composer require symfony/polyfill-intl-icu:^1.34.0
intl functionality.First Use Case: RTL Detection
Check if a locale is right-to-left (e.g., Arabic, Hebrew) without requiring the intl extension:
use Symfony\Component\Polyfill\Intl\Icu;
if (Icu\locale_is_right_to_left('ar')) {
// Apply RTL-specific CSS or layout adjustments
return view('rtl-layout');
}
First Use Case: Basic Formatting
Use NumberFormatter or IntlDateFormatter in environments without the intl extension:
use Symfony\Component\Polyfill\Intl\Icu;
$formatter = new Icu\NumberFormatter('en_US', Icu\NumberFormatter::CURRENCY);
echo $formatter->format(1234.56); // Outputs: $1,234.56 (English fallback)
Verify PHP 8.5 Compatibility
Test IntlListFormatter (added in v1.34.0) on PHP 8.5:
$formatter = new Icu\IntlListFormatter('en', Icu\IntlListFormatter::LONG);
echo $formatter->format(['Apple', 'Banana', 'Cherry']); // Outputs: "Apple, Banana, and Cherry"
// In a Laravel controller or Blade directive
public function getLayout($locale)
{
if (Icu\locale_is_right_to_left($locale)) {
return view('layouts.rtl', ['direction' => 'rtl']);
}
return view('layouts.ltr', ['direction' => 'ltr']);
}
App::setLocale()) and frontend frameworks like Tailwind (rtl classes).intl Environmentsintl extension calls with polyfill equivalents in CI/CD, shared hosting, or legacy systems.// Instead of:
// $formatter = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY);
// Use:
$formatter = new Icu\NumberFormatter('en_US', Icu\NumberFormatter::CURRENCY);
IntlListFormatter for localized list patterns (e.g., "A, B, and C") in PHP 8.5.$items = ['Apple', 'Banana', 'Cherry'];
$formatter = new Icu\IntlListFormatter('en', Icu\IntlListFormatter::LONG);
$formatted = $formatter->format($items); // "Apple, Banana, and Cherry"
Collator for simple sorting (e.g., search filters, admin panels) where intl is unavailable.$collator = new Icu\Collator('en');
$sorted = $collator->sort(['banana', 'apple', 'cherry']);
Collator instantiations in sorting logic (e.g., Eloquent queries, collections).intl extension upgrades.if (!extension_loaded('intl')) {
Log::info('Using ICU polyfill for Intl functionality');
}
Laravel Service Providers Bind ICU classes to the container for dependency injection:
// In AppServiceProvider
$this->app->bind(Icu\NumberFormatter::class, function ($app) {
return new Icu\NumberFormatter(...);
});
Blade Directives Create a Blade directive for RTL detection:
// In AppServiceProvider
Blade::directive('rtl', function ($locale) {
return "<?php if (\\Symfony\\Component\\Polyfill\\Intl\\Icu\\locale_is_right_to_left({$locale})): ?>";
});
Usage:
@rtl('ar')
<div class="rtl-class">...</div>
@endrtl
Testing Mock ICU classes in PHPUnit to test polyfill behavior:
$this->partialMockBuilder(Icu\NumberFormatter::class)
->disableOriginalConstructor()
->setMethods(['format'])
->getMock();
Performance Optimization
Cache Collator or IntlListFormatter instances if used frequently:
$collator = Cache::remember('collator_en', 3600, function () {
return new Icu\Collator('en');
});
Environment-Specific Configuration
Use Laravel’s config('app.env') to conditionally load ICU features:
if (app()->environment('production') && !extension_loaded('intl')) {
// Enable polyfill features
}
English-Only Fallbacks
NumberFormatter, IntlDateFormatter, and IntlListFormatter. Non-English locales (e.g., th_TH, hi_IN) will produce incorrect results.ext-intl upgrades for full i18n support.RTL Detection Limitations
locale_is_right_to_left() may not handle all edge cases (e.g., mixed-script content like Arabic + Latin).ar, he, fa) and validate UI responses (e.g., CSS classes, form alignment).Performance Overhead
Collator::compare() and IntlListFormatter are significantly slower than native intl (3–5x overhead).PHP 8.5-Specific Issues
IntlListFormatter is supported in v1.34.0, ensure compatibility with Laravel’s PHP version constraints (e.g., 8.1+).Non-Exhaustive Coverage
Locale Validation
xx_XX).$locale = preg_replace('/[^a-zA-Z_-]/', '', $locale);
Check for Polyfill Usage Log when the polyfill is active to identify areas for optimization:
if (!extension_loaded('intl')) {
Log::debug('ICU polyfill active: ', [
'class' => Icu\NumberFormatter::class,
'method' => 'format',
]);
}
Validate RTL Detection Test with known RTL locales:
assert(Icu\locale_is_right_to_left('ar') === true);
assert(Icu\locale_is_right_to_left('en') === false);
Compare Polyfill vs. Native Performance Benchmark critical operations:
$data = range(1, 1000);
$start = microtime(true);
usort($data, fn($a, $b) => (new Icu\Collator('en'))->compare($a, $b));
$time = microtime(true) - $start;
Log::info("Polyfill sort time: {$time}s");
Inspect Formatting Outputs
Verify NumberFormatter and IntlDateFormatter outputs match expectations:
$formatter = new Icu\NumberFormatter('en_US', Icu\NumberFormatter
How can I help you explore Laravel packages today?