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

app()->setLocale('ar');

$locale = app()->getLocale();



return [
    'validation.ssages.welcome',
    'blog::post.title',
];



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; // منشوري الأول

// Convert from attribute-based to locale-based format
$data = [
    'author' => 'John',
    'title' => [
        'en' => 'My Post',
        'ar' => 'منشوري'
    ],
    'content' => [
        'en' => 'Content',
        'ar' => 'المحتوى'
    ]
];

$converted = to_locale_based($data, ['title', 'content']);

// Result:
// [
//     'author' => 'John',
//     'en' => ['title' => 'My Post', 'content' => 'Content'],
//     'ar' => ['title' => 'منشوري', 'content' => 'المحتوى']
// ]

// Convert back to attribute-based format
$original = to_attribute_based($converted, ['title', 'content']);

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

$this->createTableWithTranslations(
    'posts',
    function (Blueprint $table) {
        $table->uuid('id')->primary();
        // ...
    },
    function (Blueprint $table) {
        // ...
    },
    true // useUuid = true
);

// In your custom middleware
if (config('locale-detector.auto_detect_locale')) {
    $locale = $request->header('Accept-Language');
    // Your custom logic here
    app()->setLocale($locale);
}
bash
php artisan vendor:publish --tag=locale-detector-config
bash
php artisan vendor:publish --tag=locale-detector-lang
bash
php artisan locale:extract-keys --format=php --output=config/translation-keys.php
bash
php artisan locale:extract-keys --exclude=vendor,storage
json
[
    {
        "key": "welcome",
        "file": "app/Http/Controllers/TestController.php",
        "line": 11,
        "location": "app/Http/Controllers/TestController.php:11",
        "locations": [
            "app/Http/Controllers/TestController.php:11",
            "app/Http/Controllers/TestController.php:12"
        ]
    }
]
bash
php artisan locale:extract-keys
bash
php artisan locale:extract-keys --path=app/Http/Controllers
bash
php artisan locale:extract-keys --exclude=vendor,storage,node_modules