twig/intl-extra
Twig Intl Extra adds internationalization helpers to Twig: look up country, currency, language, locale and timezone names, list country timezones, and format numbers, currencies, dates and times using ICU/Intl-style formatting.
twig/intl-extra can integrate with Laravel via Twig bridge or standalone Twig templates (e.g., emails, APIs). Laravel’s Blade does not natively support Twig extensions, requiring workarounds (e.g., custom directives or hybrid templating).intl extension for locale-aware operations, which is enabled by default in Laravel but may require explicit enabling in some environments (e.g., Docker, serverless).App::setLocale()). Risk of inconsistent locale handling if templates override app-wide settings.@if($count == 1) ... @endif) with {{ count|pluralize }}.Carbon/Str::of() work to templates (e.g., {{ price|currency('EUR') }}).twig/bridge to integrate Twig with Laravel’s service container (complex, may introduce conflicts).AppServiceProvider).public function register()
{
$this->app->singleton(\Twig\Environment::class, function ($app) {
$loader = new \Twig\Loader\FilesystemLoader($app['path.base'].'/resources/views');
$twig = new \Twig\Environment($loader);
$twig->addExtension(new \Twig\Extra\Intl\IntlExtension());
return $twig;
});
}
Twig\Test\IntegrationTestCase).{{ 'fr_FR'|locale }} vs. App::setLocale('en')). Risk of inconsistent formatting.{{ complex_date|date('full') }} in loops) can slow rendering.twig/twig (~1MB) and twig/intl-extra to the stack.Str::of(), Carbon)?App::getLocale() vs. hardcoded Twig filters)?locale('xh') for Xhosa)?Str::of() or Carbon handle 80% of needs with less overhead?voku/pluralrules a lighter alternative?// app/Providers/BladeServiceProvider.php
Blade::directive('intlDate', function ($expression) {
return "<?php echo (new \Twig\Extra\Intl\IntlExtension())->getDateFilter()->format(\$expression); ?>";
});
Usage:
@intlDate('2024-05-20', 'full', 'en_US')
twig/bridge to integrate Twig with Laravel’s service container (complex, may introduce conflicts).composer require twig/twig twig/intl-extra
AppServiceProvider:
public function boot()
{
$this->app->singleton(\Twig\Environment::class, function ($app) {
$loader = new \Twig\Loader\FilesystemLoader($app['path.base'].'/resources/views/twig');
$twig = new \Twig\Environment($loader);
$twig->addExtension(new \Twig\Extra\Intl\IntlExtension());
return $twig;
});
}
spatie/laravel-notification-channels).twig/mailable.@if($count == 1) with {{ count|pluralize }}.$twig->addFunction(new \Twig\TwigFunction('app_locale', function() {
return app()->getLocale();
}));
{{ '202
How can I help you explore Laravel packages today?