h1ch4m/multi-lang
最新稳定版本:v2.0.13
Composer 安装命令:
composer require h1ch4m/multi-lang
包简介
A multi language solution for Laravel applications with database-driven translations, automatic language detection.
README 文档
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' 'custom_route' => '', // panel. 'custom_content' => 'content', // content 'custom_javascript' => 'javascript', // javascript 'custom_style' => 'style', // style 'models_path' => [ 'Models', // add paths of models e.g. Tenants ] ];
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.
统计信息
- 总下载量: 126
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-02