PHPackages                             h1ch4m/multi-lang - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. h1ch4m/multi-lang

ActiveLibrary[Localization &amp; i18n](/categories/localization)

h1ch4m/multi-lang
=================

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

v2.0.13(10mo ago)0159MITBladePHP &gt;=8.0

Since May 2Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/Hicham-1/multi-lang-package)[ Packagist](https://packagist.org/packages/h1ch4m/multi-lang)[ RSS](/packages/h1ch4m-multi-lang/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (21)Used By (0)

H1ch4m/MultiLang 🌐
==================

[](#h1ch4mmultilang-)

[![Latest Version](https://camo.githubusercontent.com/d5132cf696464f8eec70251c30394c2fbce48388f5d13c3c45b805d58358274d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68316368346d2f6d756c74692d6c616e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/h1ch4m/multi-lang)[![Total Downloads](https://camo.githubusercontent.com/04971539ffacb2b0c488e6e42657a281a6399f974f0045de77de08529d0b2b51/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f68316368346d2f6d756c74692d6c616e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/h1ch4m/multi-lang)[![License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

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

Table of Contents
=================

[](#table-of-contents)

- [H1ch4m/MultiLang 🌐](#h1ch4mmultilang-)
- [Table of Contents](#table-of-contents)
- [✨ Features](#-features)
- [📋 Requirements](#-requirements)
- [⚙️ Installation](#%EF%B8%8F-installation)
    - [Config files](#config-files)
    - [Migrations](#migrations)
    - [Views](#views)
    - [Routes](#routes)
- [⚙️ Configuration](#%EF%B8%8F-configuration)
    - [Config Files](#config-files-1)
- [🔧 Advanced Usage](#-advanced-usage)
    - [Custom route](#custom-route)
        - [if you published the routes](#if-you-published-the-routes)
        - [if you want to use package routes](#if-you-want-to-use-package-routes)
- [🧰 Helpers](#-helpers)
    - [Available helper functions](#available-helper-functions)
- [🗺️ Navigation](#%EF%B8%8F-navigation)
- [🚧 Middleware](#-middleware)
- [📄 Service Provider](#-service-provider)
- [📖 Model Setup Example](#-model-setup-example)
- [🎓 License](#-license)

✨ Features
==========

[](#-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
==============

[](#-requirements)

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

⚙️ Installation
===============

[](#️-installation)

1. Install via Composer:

```
composer require h1ch4m/multi-lang
```

2. Publish required components:

Config files
------------

[](#config-files)

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

Migrations
----------

[](#migrations)

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

Views
-----

[](#views)

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

Routes
------

[](#routes)

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

3. Run migrations:

```
php artisan migrate
```

⚙️ Configuration
================

[](#️-configuration)

Config Files
------------

[](#config-files-1)

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'],
];
```

2. `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
    ]
];
```

3. `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
================

[](#-advanced-usage)

Custom route
------------

[](#custom-route)

### if you published the routes

[](#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

[](#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
=========

[](#-helpers)

Available helper functions
--------------------------

[](#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
=============

[](#️-navigation)

```

             {{ __('Translation') }}

             {{ __('ML Setting') }}

```

🚧 Middleware
============

[](#-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
==================

[](#-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
=====================

[](#-model-setup-example)

your Model should be look like this

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

```
