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.
Installation:
composer require twig/intl-extra
Register the extension in your Laravel Twig environment (e.g., in AppServiceProvider):
use Twig\Extra\Intl\IntlExtension;
public function boot()
{
$twig = $this->app['twig'];
$twig->addExtension(new IntlExtension());
}
First Use Case:
Format a currency in a Twig template (e.g., resources/views/pricing.blade.twig):
{{ 1000.50|format_currency('USD') }} {# Outputs: $1,000.50 #}
Or use it in a Blade template via Twig bridge (if configured):
@twig
{{ 1000.50|format_currency('EUR') }}
@endtwig
Where to Look First:
twig/bridge or standalone Twig.app()->getLocale() aligns with template needs (e.g., config/app.php).intl is enabled (php -m | grep intl).{{ 1234567.89|format_currency('JPY') }} {# ¥1,234,568 #}
{{ 1234567.89|number_format(2, 'de_DE') }} {# 1.234.567,89 #}
{{ '2024-12-25'|date('full', 'fr_FR') }} {# mercredi 25 décembre 2024 #}
{{ now|relative_date('en_US') }} {# "2 hours ago" #}
// In service provider
$twig->addFunction(new \Twig\TwigFunction('app_locale', function() {
return app()->getLocale();
}));
{{ 'Hello'|trans({ 'locale': app_locale() }) }}
{{ 1|pluralize('apple', 'apples') }} {# apple #}
{{ 5|pluralize('apple', 'apples') }} {# apples #}
{{ 'user'|select('male', 'female', 'other') }}
{{ 'US'|country_name('en_US') }} {# United States #}
{{ 'America/New_York'|timezone_name('en_US') }} {# Eastern Time #}
{{ 'US'|country_timezones('en_US') }} {# ['America/New_York', 'America/Chicago', ...] #}
Carbon instance to Twig:
return view('twig-template', ['event' => Carbon::now()]);
{{ event|format_datetime('full', 'es_ES') }}
{{ form_errors|trans({'locale': request.locale}) }}
$twig->setCache(new \Twig\Cache\FilesystemCache('/path/to/cache'));
$formattedPrice = number_format($price, 2, '.', ',');
return view('twig-template', compact('formattedPrice'));
$twig = new \Twig\Environment($loader);
$twig->addExtension(new IntlExtension());
$twig->setLocale('fr_FR'); // Force French for tests
{{ 'ZZ'|country_name('en_US') }} {# Should handle invalid codes #}
return view('product', [
'price' => 999.99,
'currency' => 'EUR',
]);
<h1>{{ product.name }}</h1>
<p>Price: {{ price|format_currency(currency) }}</p>
<p>Formatted: {{ price|number_format(2, app_locale()) }}</p>
$request->merge(['timezone' => auth()->user()->timezone]);
<p>Last login: {{ user.last_login|format_datetime('medium', request.timezone) }}</p>
Route::get('/admin', function() {
return view('admin', ['locale' => 'es_ES']);
})->middleware('locale');
{% set locale = 'es_ES' %}
<h1>{{ 'Dashboard'|trans({'locale': locale}) }}</h1>
<p>{{ 1000|number_format(0, locale) }}</p>
public function build()
{
return $this->markdown('emails.order-confirmation')
->with([
'order' => $this->order,
'locale' => $this->user->locale,
]);
}
<p>Order #{{ order.id }} total: {{ order.total|format_currency('USD') }}</p>
<p>Placed on: {{ order.created_at|format_date('medium', locale) }}</p>
Blade + Twig Hybrid:
Use @twig directives in Blade for Intl-Extra features:
@twig
{{ $price|format_currency('GBP') }}
@endtwig
Laravel Nova Customization:
Extend Nova’s Twig templates (e.g., resources/views/vendor/nova/) to use Intl-Extra:
{{ resource.createdAt|format_datetime('medium', resource.locale) }}
API Responses with Twig: Use Twig to format API responses (e.g., for GraphQL or JSON:API):
$response = new TwigResponse($twig->render('api/response.twig', [
'data' => $data,
'locale' => $request->locale,
]), 200, ['Content-Type' => 'application/json']);
Livewire + Twig: Pass data to Livewire components and format in Twig:
public $price = 199.99;
public $currency = 'USD';
<div>{{ this.price|format_currency(this.currency) }}</div>
Inertia.js: Use Twig for server-side rendering of Inertia pages:
return Inertia::render('Dashboard', [
'stats' => $stats,
])->withViewData(['twig' => $twig]);
<!-- resources/js/Pages/Dashboard.vue -->
<template>
<div>{{ $page.props.stats.revenue|format_currency('EUR') }}</div>
</template>
How can I help you explore Laravel packages today?