yiisoft/i18n
Yii i18n provides lightweight internationalization utilities for PHP: a BCP 47 Locale value object to parse and modify locale parts, build locale strings, and derive fallbacks, plus a stateful LocaleProvider service for managing the current and default locale.
laravel-translation-manager, spatie/laravel-translation-loader) or serve as a replacement for core Symfony/Translation components.en-US, zh-Hans-CN), which is more precise than Laravel’s default setlocale() approach, enabling advanced regionalization (e.g., en-GB vs. en-US date formats).Symfony/Translation bundle, offering additional features like locale validation, negotiation, and region-specific formatting.yiisoft/i18n to TranslatorInterface) or as a middleware for dynamic locale detection (e.g., from Accept-Language headers).config/translation.php or custom storage adapters.Symfony/Translation uses a slightly different API (e.g., translator->trans() vs. yiisoft/i18n's Translator::translate()). Migration may require adapter wrappers.NumberFormatter) could impact performance in high-throughput APIs.laravel-translation-manager) or advanced regionalization (e.g., locale-aware number/date formatting)?xx-YY) be managed? Default to en-US or throw errors?App::setLocale() calls or __() helpers in legacy codebases? Refactoring may be needed.Symfony/Translation for backend logic. Works alongside spatie/laravel-translation-loader for file-based translations.i18next or custom scripts). The package itself is backend-only.Phase 1: Proof of Concept
Symfony/Translation in a single module (e.g., API routes) using a facade wrapper:
// app/Providers/AppServiceProvider.php
Translator::bind('yiisoft/i18n', function () {
return new \Yiisoft\I18n\Translator([
'locale' => app()->getLocale(),
'messages' => require base_path('resources/lang/en/messages.php'),
]);
});
yiisoft/i18n's Locale class for parsing Accept-Language headers.Phase 2: Full Integration
trans() helper with a custom macro:
Blade::if('trans', function ($key, $replace = [], $locale = null) {
return app('yiisoft/i18n')->translate($key, $replace, $locale);
});
Illuminate/Foundation/Application to use yiisoft/i18n's Locale for setLocale().Phase 3: Advanced Features
yiisoft/i18n's NumberFormatter/DateFormatter for locale-aware output (e.g., in API responses or Blade templates).public function handle($request, Closure $next) {
$locale = Locale::createFromLanguageTag($request->header('Accept-Language'));
app()->setLocale($locale->getId());
return $next($request);
}
spatie/laravel-translation-manager or laravel-translation-manager; can coexist or replace them.laravel-localization for frontend routing but requires manual sync of locale files.Symfony/Translation for backend messages.setlocale() calls to yiisoft/i18n's Locale class.NumberFormatter/DateFormatter for API responses.i18next) to match backend locales.spatie/laravel-translation-loader). Custom wrappers may need maintenance.yiisoft/i18n's Logger interface to log translation issues (e.g., missing keys, invalid locales).Locale::getErrors() for runtime validation.Translator with fallback chains (e.g., en-US → en → en-GB) to handle missing translations gracefully.yiisoft/i18n's FileMessageSource with FileCache).$translator = new Translator([
'source' => new DatabaseMessageSource(new MyTranslationModel()),
]);
Locale parsing under high traffic (e.g., 10K RPS); expect <1ms overhead.Locale objects are immutable to avoid serialization issues in queues.| Failure Scenario | Impact | Mitigation |
|---|---|---|
Invalid locale (e.g., xx-YY) |
App crashes or falls back to en |
Configure Locale::createFromLanguageTag() with Locale::FALLBACK flag. |
| Missing translation key | Blank strings or errors | Use Translator::translate() with null fallback. |
| Database connection failure | Translations unavailable | Implement a hybrid FileMessageSource + DatabaseMessageSource with fallback. |
| PHP 8.1+ feature incompatibility | Runtime errors | Use yiisoft/i18n’s Compat traits or polyf |
How can I help you explore Laravel packages today?