mgcodeur / laravel-translation-loader
Store your translations in the database
Fund package maintenance!
mgcodeur
Requires
- php: ^8.3
- illuminate/contracts: ^10.0||^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^2.0||^3.0
- pestphp/pest-plugin-arch: ^2.5||^3.0
- pestphp/pest-plugin-laravel: ^2.0||^3.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
- spatie/laravel-ray: ^1.35
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
Add 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.php
database/migrations/create_languages_table.php
database/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'); } };
Apply or Revert Migrations
Run all pending translation migrations:
php artisan translation:migrate
Rollback the last migration:
php artisan translation:rollback
🎯 Using Translations
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
Check Migration Status
View the status of translation migrations:
php artisan translation:status
Example Output:
+-------------------------+----------+
| Migration | Status |
+-------------------------+----------+
| welcome | Migrated |
| auth | Pending |
+-------------------------+----------+
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
└── ...
Tip: Customize the output path in config/translation-loader.php
.
⚙️ Configuration
Customize settings in config/translation-loader.php
Fallbacks
If a translation is missing, Laravel will fall back to the default language defined in your config/app.php
.
⚠️ Troubleshooting
For more help, check GitHub Issues or open a new issue.
❓ 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 for performance. Automatically cleared when migrations are applied or rolled back.
📜 License
Licensed under the MIT License.
❤️ Support the Project
If this package saves you time, show some love:
- ⭐ Star the GitHub repo
- 📢 Share it with your network
- 💸 Sponsor development via GitHub Sponsors
Built for Laravel developers, by Laravel developers. Let’s make multilingual apps effortless! 🚀