PHP code example of rawnoq / laravel-locale-detector
1. Go to this page and download the library: Download rawnoq/laravel-locale-detector 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/ */
rawnoq / laravel-locale-detector example snippets
return [
/*
|--------------------------------------------------------------------------
| Auto Detect Locale
|--------------------------------------------------------------------------
|
| Enable automatic locale detection from Accept-Language header.
|
*/
'auto_detect_locale' => env('AUTO_DETECT_LOCALE', true),
/*
|--------------------------------------------------------------------------
| Auto Detect Preferred Language
|--------------------------------------------------------------------------
|
| Use Laravel's getPreferredLanguage() method for locale detection.
| If false, uses the Accept-Language header directly.
|
*/
'auto_detect_preferred_language' => env('AUTO_DETECT_PREFERRED_LANGUAGE', true),
/*
|--------------------------------------------------------------------------
| Supported Locales
|--------------------------------------------------------------------------
|
| List of supported locales for automatic detection.
|
*/
'supported_locales' => env('SUPPORTED_LOCALES', 'en,ar')
? explode(',', env('SUPPORTED_LOCALES', 'en,ar'))
: ['en', 'ar'],
];
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Rawnoq\LocaleDetector\Concerns\Translatable;
class Post extends Model
{
use Translatable;
public $translatedAttributes = ['title', 'content'];
protected $fillable = ['author', 'title', 'content'];
}
// Using locale-based format (Astrotomic standard)
$post = Post::create([
'author' => 'John Doe',
'en' => [
'title' => 'My First Post',
'content' => 'This is the content.'
],
'ar' => [
'title' => 'منشوري الأول',
'content' => 'هذا المحتوى.'
]
]);
// Accessing translations
echo $post->translate('en')->title; // My First Post
echo $post->translate('ar')->title; // منشوري الأول
App::setLocale('ar');
echo $post->title; // منشوري الأول
// Using the model's static methods
$converted = Post::convertToLocaleBased($data);
$original = Post::convertToAttributeBased($converted);
use Rawnoq\LocaleDetector\Database\Migrations\MigrationWithTranslations;
use Illuminate\Database\Schema\Blueprint;
class CreatePostsTable extends MigrationWithTranslations
{
public function up(): void
{
$this->createTableWithTranslations(
'posts',
function (Blueprint $table) {
$table->id();
$table->string('author');
$table->boolean('is_published')->default(false);
$table->timestamps();
},
function (Blueprint $table) {
$table->string('title');
$table->text('content');
$table->string('slug')->unique();
}
);
}
public function down(): void
{
$this->dropIfExistsWithTranslations('posts');
}
}
use Illuminate\Database\Migrations\Migration;
use Rawnoq\LocaleDetector\Concerns\CreateTableWithTranslations;
use Illuminate\Database\Schema\Blueprint;
class CreatePostsTable extends Migration
{
use CreateTableWithTranslations;
public function up(): void
{
$this->createTableWithTranslations(/* ... */);
}
}
// In your custom middleware
if (config('locale-detector.auto_detect_locale')) {
$locale = $request->header('Accept-Language');
// Your custom logic here
app()->setLocale($locale);
}