PHP code example of h1ch4m / multi-lang

1. Go to this page and download the library: Download h1ch4m/multi-lang library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

h1ch4m / multi-lang example snippets


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

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
    ]
];

return [
    // your other configs

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


Route::group(['prefix' => 'panel', 'as' => 'panel.'], function () {
    

Route::group(['prefix' => 'panel', 'as' => 'panel.'], function () {
    

// 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

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

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

    }

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


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');
    }
}
bash
php artisan vendor:publish --tag=config --provider="H1ch4m\MultiLang\MultiLangServiceProvider"
bash
php artisan vendor:publish --tag=migration --provider="H1ch4m\MultiLang\MultiLangServiceProvider"
bash
php artisan vendor:publish --tag=views --provider="H1ch4m\MultiLang\MultiLangServiceProvider"
bash
php artisan vendor:publish --tag=routes --provider="H1ch4m\MultiLang\MultiLangServiceProvider"
bash
php artisan migrate
h1ch4m_languages.php
h1ch4m_config.php
app.php
array