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/ */
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
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');
}
}