h1ch4m / multi-lang
A multi language solution for Laravel applications with database-driven translations, automatic language detection.
Installs: 62
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Language:Blade
Requires
- php: >=8.0
- illuminate/support: >=8.0
- spatie/laravel-translatable: >=6.6
README
A multi language solution for Laravel applications with database-driven translations, automatic language detection.
Table of Contents
- H1ch4m/MultiLang π
- Table of Contents
- β¨ Features
- π Requirements
- βοΈ Installation
- βοΈ Configuration
- π§ Advanced Usage
- π§° Helpers
- πΊοΈ Navigation
- π§ Middleware
- π Service Provider
- π Model Setup Example
- π License
β¨ 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
- Install via Composer:
composer require h1ch4m/multi-lang
- 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"
- Run migrations:
php artisan migrate
βοΈ Configuration
Config Files
Configure these published files in config/
directory:
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'], ];
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 ];
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.