PHP code example of zaber-dev / laravel-localization

1. Go to this page and download the library: Download zaber-dev/laravel-localization 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/ */

    

zaber-dev / laravel-localization example snippets


use ZaberDev\Localization\Facades\Localization;
use ZaberDev\Localization\Enums\LocalizationSource;

// 1. Transparent Model Proxying (reads active application locale seamlessly)
app()->setLocale('fr');
echo $post->title; // Automatically returns French title!

// 2. Explicit Locale Access
echo $post->localize('fr')->title;

// 3. Storing AI/Human Translations with Rich Metadata
Localization::model($post)->store('fr', [
    'title'   => 'Bonjour le monde',
    'content' => 'Contenu du billet...',
], LocalizationSource::AI, [
    'provider' => 'openai',
    'model'    => 'gpt-4o',
    'tokens'   => 1200,
    'cost'     => 0.04,
]);

// 4. Stale Detection via Attribute-Level Hashing
$post->update(['title' => 'Updated English Title']);

$info = Localization::model($post)->get('fr');
$info->isStale();            // true
$info->staleAttributes();    // ['title'] (content is still valid!)

return [
    /*
    |--------------------------------------------------------------------------
    | Default Locale
    |--------------------------------------------------------------------------
    */
    'default_locale' => 'en',

    /*
    |--------------------------------------------------------------------------
    | Default Storage Driver
    |--------------------------------------------------------------------------
    */
    'default' => 'database',

    /*
    |--------------------------------------------------------------------------
    | Database Table Configuration
    |--------------------------------------------------------------------------
    */
    'database' => [
        'table' => 'localizations',
    ],

    /*
    |--------------------------------------------------------------------------
    | Fallback Chains
    |--------------------------------------------------------------------------
    */
    'fallbacks' => [
        'default' => ['en'],
        'fr-CA'   => ['fr', 'en'],
        'es-MX'   => ['es', 'en'],
    ],
];

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use ZaberDev\Localization\Contracts\Localizable;
use ZaberDev\Localization\Traits\HasLocalizations;

class Post extends Model implements Localizable
{
    use HasLocalizations;

    public function getTranslatableAttributes(): array
    {
        return ['title', 'content', 'seo_description'];
    }
}

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

// Automatically returns the French title
echo $post->title;

// Access a specific locale explicitly without altering global app state
echo $post->localize('es')->title;
echo $post->localize('es')->seo_description;

use ZaberDev\Localization\Facades\Localization;

$info = Localization::model($post)->get('fr');

if ($info) {
    echo $info->locale;        // 'fr'
    echo $info->state->value;  // 'published'
    echo $info->source->value; // 'ai'
    echo $info->completion;    // 100 (% of translatable fields populated)
    echo $info->provider;      // 'openai'
    echo $info->tokens;        // 1200
    echo $info->cost;          // 0.04
}

// Update source model title
$post->update(['title' => 'New Product Title']);

$info = Localization::model($post)->get('fr');

if ($info->isStale()) {
    // Returns array of changed attributes needing update: ['title']
    $staleFields = $info->staleAttributes();
    
    // Only re-translate $staleFields instead of regenerating everything!
}

use ZaberDev\Localization\Enums\LocalizationSource;

Localization::model($post)->store(
    locale: 'fr',
    payload: [
        'title'   => 'Nouveau titre du produit',
        'content' => 'Contenu inchangé...',
    ],
    source: LocalizationSource::AI,
    metadata: [
        'source_locale'  => 'en',
        'provider'       => 'openai',
        'model'          => 'gpt-4o',
        'prompt_version' => 'v2.1',
        'tokens'         => 850,
        'cost'           => 0.025,
    ]
);

use ZaberDev\Localization\Facades\Localization;

// Dispatches LocalizationRequested events for background consumption
Localization::batch([$post1, $post2, $product])
    ->from('en')
    ->to('de')
    ->dispatch();

namespace App\Listeners;

use ZaberDev\Localization\Events\LocalizationRequested;

class ProcessAiTranslation
{
    public function handle(LocalizationRequested $event): void
    {
        // $event->model
        // $event->sourceLocale
        // $event->targetLocale
        // $event->changedAttributes (only attributes requiring translation!)
    }
}

use ZaberDev\Localization\Enums\LocalizationState;

// Move to published state (dispatches LocalizationPublished event)
$post->localization('fr')->publish();

// Transition to custom state
Localization::model($post)
    ->to('fr')
    ->transitionTo(LocalizationState::NeedsReview);

use ZaberDev\Localization\Enums\LocalizationState;

// Models having AT LEAST ONE translation
$posts = Post::translated()->get();

// Models missing a French translation
$untranslated = Post::missingTranslations('fr')->get();

// Models with a Published French localization
$publishedFrench = Post::whereLocale('fr')
    ->whereLocalizationState(LocalizationState::Published)
    ->get();

class Post extends Model implements Localizable
{
    use HasLocalizations;

    public function toSearchableArray(): array
    {
        return array_merge(
            $this->toArray(),
            $this->toLocalizedSearchArray()
        );
    }
}
bash
php artisan vendor:publish --provider="ZaberDev\Localization\LocalizationServiceProvider"
bash
php artisan migrate