mgcodeur/laravel-translation-loader
Composer 安装命令:
composer require mgcodeur/laravel-translation-loader
包简介
Store your translations in the database
README 文档
README
Manage Laravel translations in a database with version-controlled migration files. This package streamlines multilingual app development, letting you create, update, and maintain translations without touching JSON or PHP files.
🚀 Quick Start
Get up and running in three steps.
1. Install the Package
Install the package to your Laravel project:
composer require mgcodeur/laravel-translation-loader
2. Publish Assets
Publish configuration and migration files:
php artisan laravel-translation-loader:install
This generates:
config/translation-loader.php(package settings)database/migrations/create_translations_table.phpdatabase/migrations/create_languages_table.phpdatabase/migrations/create_translation_migrations_table.php(table for translations migrations)
3. Run Migrations
Run the migrations:
php artisan migrate
🛠️ Creating Translation Migrations
Generate a migration to define translations:
php artisan make:translation welcome
👉 This creates a file in database/translations/:
<?php use Mgcodeur\LaravelTranslationLoader\Translations\TranslationMigration; return new class extends TranslationMigration { public function up(): void { $this->add('en', 'welcome.title', 'Welcome to Our App'); $this->add('fr', 'welcome.title', 'Bienvenue dans notre application'); $this->add('es', 'welcome.title', 'Bienvenido a nuestra aplicación'); } public function down(): void { $this->delete('en', 'welcome.title'); $this->delete('fr', 'welcome.title'); $this->delete('es', 'welcome.title'); // or you can just do: $this->deleteAll('welcome.title'); // $this->deleteAll('key1', 'key2', 'key3'); } };
Apply or Revert Migrations
Run all pending translation migrations:
php artisan translation:migrate
Rollback the last migration:
php artisan translation:rollback
🎯 Usage
Access translations as you would with standard Laravel language files:
// In controllers, views, or anywhere echo __('welcome.title'); // Outputs: "Welcome to Our App" (if en is active)
📦 Bonus Features
1. Check Migration Status
View the status of translation migrations:
php artisan translation:status
Example Output:
+-------------------------+----------+
| Migration | Status |
+-------------------------+----------+
| welcome | Migrated |
| auth | Pending |
+-------------------------+----------+
2. Generate Language Files
👉 Export database translations to Laravel’s lang directory:
php artisan translation:generate
This creates files like:
lang/
├── en.json
├── fr.json
├── es.json
└── ...
💡 Customize the output path in config/translation-loader.php.
⚙️ Configuration
Customize settings in config/translation-loader.php
✨ Extra Helpers
- addMany
Add multiple keys at once.
$this->addMany('en', [ 'email' => 'Email', 'password' => 'Password', ]); $this->addMany('fr', [ 'email' => 'Email', 'password' => 'Mot de passe', ]);
or
$this->addMany([ 'en' => [ 'login' => 'Login', 'logout' => 'Logout', ], 'fr' => [ 'login' => 'Se connecter', 'logout' => 'Se déconnecter', ], ]);
🧩 Translation Groups
Groups let you namespace keys without repeating prefixes.
$this->group('welcome', function () { $this->add('en', 'title', 'Welcome'); $this->add('fr', 'title', 'Bienvenue'); $this->addMany('en', [ 'subtitle' => 'Enjoy your stay', 'cta' => 'Get started', ]); });
- update
Update existing translations.
$this->update('en', 'welcome.title', 'Welcome to Our Awesome App');
Fallbacks
If a translation is missing, Laravel will fall back to the default language defined in your config/app.php.
❓ FAQ
Q: Can I use this with existing JSON/PHP translation files?
A: Yes! The package works alongside file-based translations.
Q: How does caching work?
A: Translations are cached. Automatically cleared when migrations are applied or rolled back.
📜 License
Licensed under the MIT License.
❤️ Support the Project
If this package saves you time:
- ⭐ Star the GitHub repo
- 📢 Share it with your network
- 💸 Sponsor development via GitHub Sponsors
For more help, check GitHub Issues or open a new issue.
mgcodeur/laravel-translation-loader 适用场景与选型建议
mgcodeur/laravel-translation-loader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 158 次下载、GitHub Stars 达 10, 最近一次更新时间为 2025 年 08 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「laravel-translation-loader」 「mgcodeur」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mgcodeur/laravel-translation-loader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mgcodeur/laravel-translation-loader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mgcodeur/laravel-translation-loader 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Currency Converter: Effortlessly convert currencies in your Laravel applications, no API key required. It's fast, easy, and completely free.
Manage your translation with DB and cache, you can scan your languages tags like trans(), __(), and get the string inside and translate them use UI.
Store your language lines in the database, yaml or other sources
Store your language lines in the database, yaml or other sources
Store your language lines in the database, yaml or other sources
Store your language lines in the database, yaml or other sources
统计信息
- 总下载量: 158
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-19