joelstein/laravel-t
Composer 安装命令:
composer require joelstein/laravel-t
包简介
Gettext PO-based translations for Laravel with ICU MessageFormat support
README 文档
README
Gettext PO-based translations for Laravel with ICU MessageFormat support. Use source strings as keys — no more auth.failed style lookup tables — and manage translations with standard PO files that every translation tool already understands.
What It Does
Replaces Laravel's key-based translator with a t() helper (and @t() Blade directive) that looks up translations by their English source string:
t('Hello, :name!', ['name' => $user->name])
- PO files as the source of truth. One
.pofile per locale, loaded via gettext/gettext. Works with Poedit, Crowdin, Weblate, and every other gettext-aware tool. - Readable fallbacks. When a translation is missing, the source string renders as-is — no
translation.missing.keyartifacts. - ICU MessageFormat. Full plural, select, and number formatting via PHP's
MessageFormatter:t('{count, plural, one {# item} other {# items}}', ['count' => $total]). - Contextual translations. Disambiguate identical strings with
msgctxt:t('May', context: 'month'). - Closure-based inline markup. Wrap translated text in dynamic tags without splitting the sentence:
t('Click <a>here</a>.', ['a' => fn ($s) => "<a href=\"/next\">{$s}</a>"]). - Cached in production. Parsed PO files are cached through Laravel's cache system.
Requirements
- PHP 8.2+
- Laravel 11 or 12
ext-intl(for ICU MessageFormat)
Installation
composer require joelstein/laravel-t php artisan vendor:publish --tag=t-config
The service provider is auto-discovered. Create your translation directory (default lang/t/) and add a PO file per locale.
Usage
// Plain strings t('Hello, world!') // Named placeholders t('Hello, :name!', ['name' => $user->name]) // ICU plurals t('{count, plural, one {# item} other {# items}}', ['count' => $total]) // Contexts (for homographs) t('May', context: 'month') // Inline markup via closures t('Click <a>here</a>.', [ 'a' => fn ($text) => "<a href=\"/next\">{$text}</a>", ]) // Explicit locale override t('Saved.', locale: 'es')
In Blade:
<h1>@t('Welcome back, :name!', ['name' => $user->name])</h1>
Commands
# Scan source files and update PO files for every configured locale php artisan t:extract # List untranslated strings, optionally for one locale php artisan t:untranslated php artisan t:untranslated es # Validate PO files (parse errors, placeholder mismatches, invalid ICU) php artisan t:lint php artisan t:lint es # Clear the translation cache php artisan t:clear
t:extract preserves existing translations, adds new entries, and marks strings that no longer exist in source as obsolete (#~ entries in the PO file). Obsolete translations keep the translator's work so they snap back if the source string reappears. Run t:extract --purge to hard-delete obsolete entries instead. The source locale (default en) gets msgstr = msgid auto-populated.
Fallback Chain
When a translation is missing, the lookup walks a fallback chain:
- The requested locale (e.g.
es_MX) - The base locale, if the requested one has a region suffix (
es_MX→es) - The configured
fallback_locale, if set
If nothing in the chain has the string, the original msgid is returned. ICU formatting always uses the originally requested locale, so plural rules and number formats still match the user's region even when the translation comes from a fallback.
Configuration
config/t.php:
return [ 'path' => lang_path('t'), 'source_locale' => 'en', 'locales' => ['en'], 'fallback_locale' => null, 'scan_paths' => ['app', 'resources/views'], 'cache' => null, // null = cache in production only 'cache_ttl' => 86400, 'log_missing' => null, // true, a channel name, or null to disable ];
CI Integration
- name: Check translations are extracted run: | php artisan t:extract git diff --exit-code lang/t
Combine with t:untranslated to fail on missing translations when that matters for your release.
License
MIT
joelstein/laravel-t 适用场景与选型建议
joelstein/laravel-t 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 320 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 04 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「i18n」 「translations」 「gettext」 「laravel」 「po」 「icu」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 joelstein/laravel-t 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 joelstein/laravel-t 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 joelstein/laravel-t 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use i18n translation PHP class for multi-language websites
Gettext library to translate with i18n
Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.
A custom URL rule class for Yii 2 which allows to create translated URL rules
Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.
Po plugin for CakePHP 3
统计信息
- 总下载量: 320
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 40
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-24