h1ch4m/multi-lang

A multi language solution for Laravel applications with database-driven translations, automatic language detection.

v2.0.10 2025-05-31 15:51 UTC

This package is auto-updated.

Last update: 2025-05-31 15:53:02 UTC


README

Latest Version Total Downloads License

A multi language solution for Laravel applications with database-driven translations, automatic language detection.

Table of Contents

✨ Features

  • πŸ—ƒοΈ Database-driven language management
  • 🌍 Automatic language detection middleware
  • βš™οΈ Customizable configuration files
  • πŸ”„ Language switcher UI component
  • πŸ“¦ Built-in migration system
  • πŸ“ Asset publishing support
  • πŸ“š View customization
  • πŸ› οΈ Helper functions
  • πŸ”Œ Event system integration
  • 🧩 Modular architecture

πŸ“‹ Requirements

  • PHP 8.0+
  • Laravel 9.x or later
  • spatie/laravel-translatable 6.6 or later
  • Composer
  • Database (MySQL)

βš™οΈ Installation

  1. Install via Composer:
composer require h1ch4m/multi-lang
  1. Publish required components:

Config files

php artisan vendor:publish --tag=config --provider="H1ch4m\MultiLang\MultiLangServiceProvider"

Migrations

php artisan vendor:publish --tag=migration --provider="H1ch4m\MultiLang\MultiLangServiceProvider"

Views

php artisan vendor:publish --tag=views --provider="H1ch4m\MultiLang\MultiLangServiceProvider"

Routes

php artisan vendor:publish --tag=routes --provider="H1ch4m\MultiLang\MultiLangServiceProvider"
  1. Run migrations:
php artisan migrate

βš™οΈ Configuration

Config Files

Configure these published files in config/ directory:

  1. h1ch4m_languages.php - Manage available languages
return [
    "fr" => ["name" => "French", "is_active" => true, "is_default" => false, "flag_url" => 'https://cdn-icons-png.flaticon.com/128/323/197560.png'],
    "ar" => ["name" => "Arabic", "is_active" => true, "is_default" => false, "flag_url" => 'https://cdn-icons-png.flaticon.com/128/197/197467.png'],
    "en" => ["name" => "English", "is_active" => true, "is_default" => true, "flag_url" => 'https://cdn-icons-png.flaticon.com/128/197/197374.png'],
];
  1. h1ch4m_config.php - Configure package layouts and paths
return [
    'layout' => '', //'admin.layouts.app'
    'models_path' => app_path('Models'),
    'custom_route' => '', // panel.
    'custom_content' => 'content', // content
    'custom_javascript' => 'javascript', // javascript
    'custom_style' => 'style', // style
];
  1. app.php - add locale and fallback language
return [
    // your other configs

    'locale' => env('APP_LOCALE', 'en'),
    'fallback_locale' => env('APP_FALLBACK_LOCALE', 'en')
]

πŸ”§ Advanced Usage

Custom route

if you published the routes

Route::group(['prefix' => 'panel', 'as' => 'panel.'], function () {
    require base_path('routes/vendor/h1ch4m_multi_lang.php');
});

if you want to use package routes

Route::group(['prefix' => 'panel', 'as' => 'panel.'], function () {
    require base_path('vendor/h1ch4m/multi-lang/src/routes/web.php');
});

🧰 Helpers

Available helper functions

// Get active languages from config
getActiveLanguages(): array

// Get languages saved in database (we use Cache)
// we use $refresh just to refresh data when languages changed (you can use just $params)
// $justKeys, true if you want just array of saved languages ['ar', 'en', 'fr']
getSavedLanguages($params = ['*'], $refresh = false, $justKeys = true): array

// Get default language
getDefaultLanguage(): string

// Get or Set location (see 'Service Provider' section)
getOrSetCachedLocale($localeLang = null): string

πŸ—ΊοΈ Navigation

    <li class="pc-item">
        <a href="{{ route('panel.languages.models') }}" class="pc-link">
            <span class="pc-micon">
                <i data-feather="globe"></i>
            </span>
            <span class="pc-mtext"> {{ __('Translation') }} </span>
        </a>
    </li>

    <li class="pc-item">
        <a href="{{ route('panel.languages.setting') }}" class="pc-link">
            <span class="pc-micon">
                <i data-feather="globe"></i>
            </span>
            <span class="pc-mtext"> {{ __('ML Setting') }} </span>
        </a>
    </li>

🚧 Middleware

in your frontend (Store, Blog...) use this middleware 'h1ch4m_middleware'

Route::middleware(['h1ch4m_middleware'])->group(function () {
    Route::get('/your-path', [YourBlogController::class, 'method']);
});

πŸ“„ Service Provider

in your service provider add it if you are using spatie/laravel-translatable, you will not need to call getTranslation or setTranslation (the language will add automatically)

class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {

    }

    public function boot(): void
    {
        if (Request::is('panel/*')) {
            getOrSetCachedLocale(getDefaultLanguage());
        }
    }
}

πŸ“– Model Setup Example

your Model should be look like this

The types that we support are text, editor, textarea, array

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;

class Program extends Model
{
    use HasTranslations;

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->setLocale(getOrSetCachedLocale());
    }

    // spatie attribute
    public $translatable = ['title', 'body', 'short', 'features', 'characteristics'];

    public $translatableInputs = [
        'title' => [
            'type' => 'text',
        ],
        'short' => [
            'type' => 'textarea',
        ],
        'body' => [
            'type' => 'editor',
        ],
        'features' => [
            'type' => 'array',
            'fields' => [
                'title' => 'text',
            ]
        ],
        'characteristics' => [
            'type' => 'array',
            'fields' => [
                'title' => 'text',
                'value' => 'text',
            ]
        ],
    ];
    public $custom_name = 'Program';
    public $default_title = 'title';

    // if the Model has parent (just to group programs by events)
    public $parent_method = 'event';


    protected $table = 'programs';

    protected $fillable = [
        'event_id',
        'title',
        'body',
        'features',
        'characteristics',
        'is_active',
        'is_pinned'
    ];

    public function event()
    {
        return $this->belongsTo(Event::class, 'event_id');
    }
}

πŸŽ“ License

This package is open-sourced software licensed under the MIT license.