gettext/translator
Lightweight PHP translation layer for gettext/gettext. Use Translator to load PHP array translations without the native gettext extension, or GettextTranslator to leverage the extension with the same API. Includes global helper functions for template-friendly __().
Pros:
TranslatorFunctions::register() enables __() syntax in Blade templates, mirroring Laravel’s native trans() helper, reducing developer learning curves.messages, validation), aligning with Laravel’s modular architecture and enabling shared libraries or microservices.GettextTranslator leverages native gettext extension when available, optimizing production performance while Translator provides a consistent fallback.gettext('Welcome', 'greeting')), critical for global apps.Gettext\Extractors\PhpArray for automated .po/.mo file generation, bridging PHP arrays and traditional gettext workflows.Cons:
.json/.php translation files require conversion to .php arrays or .mo files, introducing migration overhead.AppServiceProvider bootstrapping, config('app.locale'), or php artisan lang:publish..mo files must be pre-compiled and cached (e.g., via opcache or filesystem), adding DevOps considerations.gettext standards, requiring manual alignment.gettext extension is unavailable..json files, custom pluralization logic).Translator ensures functionality even without gettext extension..json/.php files to .php arrays or .mo files may require custom scripts or manual work..mo file caching and GettextTranslator configuration require DevOps attention.php artisan lang commands or translation file generation tools.Translation Strategy:
lang/en/messages.php)?File Format & Workflow:
.php arrays (for Translator) or .mo files (for GettextTranslator)? What’s the migration path for existing .json files?__('Hello'))? Will Gettext\Extractors\PhpCode or a custom tool be used?Pluralization & Localization:
gettext standards? How will conflicts be resolved?Performance & Scaling:
.mo files be pre-compiled and cached (e.g., via opcache or filesystem)? Who manages this?Translator (dev) and GettextTranslator (prod) based on extension availability?Tooling & CI/CD:
.po files be committed to the repo, or managed externally (e.g., Crowdin)?Fallback & Edge Cases:
.mo files? Will Laravel’s fallback chain still apply?gettext('Welcome', 'greeting')) be handled in Blade templates?Ideal Use Cases:
gettext Extension: Shared hosting, Docker containers, or environments where PECL extensions are restricted.auth, validation).gettext is unavailable or overkill.Avoid When:
.json files, php artisan lang commands, or custom pluralization logic without extension gaps.Audit Existing Translations:
resources/lang/*) and identify:
en, es, ar).messages, validation).trans() helper, config('app.locale')).Define Scope:
.php arrays (for Translator) or .mo files (for GettextTranslator).Tooling Setup:
gettext/translator and gettext/gettext (if using hybrid mode).Gettext\Extractors\PhpArray for generating .po files from PHP arrays..po/.mo file generation (e.g., via GitHub Actions or Crowdin).Convert Translation Files:
.php Arrays):
.json/.php files to .php arrays (e.g., resources/lang/es/messages.php).return [
'welcome' => 'Bienvenido',
'items' => [
'one' => '1 elemento',
'other' => '{count} elementos',
],
];
.mo Files):
.po files from existing translations using Gettext\Extractors\PhpArray..po to .mo files (e.g., resources/lang/es/LC_MESSAGES/messages.mo).msgfmt (part of gettext tools) or a PHP-based compiler.Update Blade Templates:
{{ trans('messages.welcome') }} with __('messages.welcome') (after registering TranslatorFunctions).ngettext() or plural() methods:
echo $t->ngettext('{count} item', '{count} items', $count);
Configure Hybrid Mode (Optional):
GettextTranslator in production (if gettext extension is available) and Translator in development.$t = extension_loaded('get
How can I help you explore Laravel packages today?