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