binsoul/common-i18n-intl
Intl-based i18n helpers for PHP from binsoul/common-i18n. Provides locale-aware formatting and parsing for numbers, currencies, dates/times, and messages via the PHP Intl extension, to simplify building internationalized apps and libraries.
Installation
composer require binsoul/common-i18n-intl
Add the service provider to config/app.php:
'providers' => [
// ...
Binsoul\CommonI18nIntl\CommonI18nIntlServiceProvider::class,
],
Basic Usage Publish the config file (optional):
php artisan vendor:publish --provider="Binsoul\CommonI18nIntl\CommonI18nIntlServiceProvider" --tag="config"
Configure locales in config/common-i18n-intl.php:
'locales' => [
'en' => 'English',
'es' => 'Spanish',
],
First Use Case: Localized Strings
use Binsoul\CommonI18nIntl\Localizer;
$localizer = app(Localizer::class);
echo $localizer->get('en', 'greeting', ['name' => 'John']); // Loads from `resources/lang/en/greeting.php`
Locale Switching Dynamically set locale in middleware or controllers:
$localizer->setLocale('es'); // Switches to Spanish
Pluralization & Genderization Leverage Intl for grammar-aware translations:
$localizer->pluralize('en', 'item', 5); // Handles singular/plural forms
$localizer->genderize('es', 'user', 'male'); // Gender-specific terms
Fallback Chains
Configure fallback locales in config/common-i18n-intl.php:
'fallbacks' => [
'es' => ['ca'], // Catalan as fallback for Spanish
],
Date/Number Formatting
Use the Formatter facade:
use Binsoul\CommonI18nIntl\Facades\Formatter;
$date = Formatter::formatDate('en', '2023-12-25', 'MMMM d, yyyy');
$number = Formatter::formatNumber('de', 1234.56, '€#,##0.00');
return view('welcome', ['localizer' => $localizer]);
{{ $localizer->get($locale, 'key', $vars) }}
return response()->json([
'message' => $localizer->get($request->locale, 'api.success')
]);
$messages = $localizer->get($locale, 'validation');
$validator = Validator::make($data, $rules, $messages);
Missing Locale Files
Translation string not found.resources/lang/{locale}/ and match the config.Intl Extension Dependency
Class 'IntlDateFormatter' not found.pecl install intl) and restart the server.Caching Headaches
php artisan cache:clear) or disable caching in config:
'cache' => false,
Log Missing Keys: Enable debug mode in config:
'debug' => env('APP_DEBUG', false),
Missing keys will log to storage/logs/laravel.log.
Intl Errors: Use Intl::getErrorCode() and Intl::getErrorMessage() to diagnose Intl-specific issues.
Custom Formatters
Extend the Formatter class to add locale-specific rules:
class CustomFormatter extends Formatter {
public function formatCustom($locale, $value) { ... }
}
Override Localizer
Bind a custom localizer in AppServiceProvider:
$this->app->bind(Localizer::class, function ($app) {
return new CustomLocalizer($app['config']);
});
Add Locale Data Dynamically register locales at runtime:
$localizer->registerLocale('pt', 'Português');
$localizer->preloadLocales(['en', 'es', 'fr']);
$localizer->remember('en', 'key', function () {
return $localizer->get('en', 'key');
}, 60); // Cache for 60 minutes
How can I help you explore Laravel packages today?