DEV Community

Mgcodeur
Mgcodeur

Posted on

Laravel Translation Loader

Multilingual apps in Laravel are often a pain…

JSON files everywhere, hard to sync, conflicts in Git.

Here’s a package that makes translations as easy as database migrations.

Easily manage Laravel translations with version-controlled migration files.

Full database support — and you can still use JSON or PHP files if you want to.

🚀 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
Enter fullscreen mode Exit fullscreen mode

2. Publish Assets

Publish configuration and migration files:

php artisan laravel-translation-loader:install
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

🛠️ Creating Translation Migrations

Generate a migration to define translations:

php artisan make:translation welcome
Enter fullscreen mode Exit fullscreen mode

👉 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');
    }
};
Enter fullscreen mode Exit fullscreen mode

Apply or Revert Migrations

Run all pending translation migrations:

php artisan translation:migrate
Enter fullscreen mode Exit fullscreen mode

Rollback the last migration:

php artisan translation:rollback
Enter fullscreen mode Exit fullscreen mode

🎯 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)
Enter fullscreen mode Exit fullscreen mode

📦 Bonus Features

1. Check Migration Status

View the status of translation migrations:

php artisan translation:status
Enter fullscreen mode Exit fullscreen mode

Example Output:

+-------------------------+----------+
| Migration               | Status   |
+-------------------------+----------+
| welcome                 | Migrated |
| auth                    | Pending  |
+-------------------------+----------+
Enter fullscreen mode Exit fullscreen mode

2. Generate Language Files

👉 Export database translations to Laravel’s lang directory:

php artisan translation:generate
Enter fullscreen mode Exit fullscreen mode

This creates files like:

lang/
├── en.json
├── fr.json
├── es.json
└── ...
Enter fullscreen mode Exit fullscreen mode

💡 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',
]);
Enter fullscreen mode Exit fullscreen mode

or

$this->addMany([
    'en' => [
        'login' => 'Login',
        'logout' => 'Logout',
    ],
    'fr' => [
        'login' => 'Se connecter',
        'logout' => 'Se déconnecter',
    ],
]);
Enter fullscreen mode Exit fullscreen mode

- update

Update existing translations.

$this->update('en', 'welcome.title', 'Welcome to Our Awesome App');
Enter fullscreen mode Exit fullscreen mode

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.

❤️ Support the Project

If this package saves you time:

For more help, check GitHub Issues or open a new issue.

Top comments (0)