laravel-lang/models
Adds localized names and attributes for Laravel Eloquent models via Laravel Lang. Plug-and-play translations for model labels across multiple languages to improve UI, validation messages, and admin panels. Install with composer and follow the docs for setup.
Pros:
Product → ProductTranslation), avoiding complex joins or nested structures.name, description) via a dedicated translation table, enabling granular control over which fields are localized.scopeTranslated(), scopeOrderByTranslation()) for querying translations efficiently, reducing custom query logic.en → es → default) via the HasTranslations trait, aligning with Laravel’s App::setLocale().LocaleData type hints (PHP 8.0+) for method arguments, improving IDE support and reducing runtime errors.Cons:
make:model-translation).// Parent model (e.g., Product)
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('sku');
$table->timestamps();
});
// Translation model (auto-generated)
Schema::create('product_translations', function (Blueprint $table) {
$table->id();
$table->foreignId('product_id')->constrained()->onDelete('cascade');
$table->string('locale')->index();
$table->string('name')->nullable();
$table->text('description')->nullable();
$table->timestamps();
});
use HasTranslations; to parent models and define $translatable fields:
class Product extends Model
{
use HasTranslations;
protected $translatable = ['name', 'description'];
}
Product::withTranslations()->get()).zh-Hans), and empty translations.HasTranslations behavior (e.g., assertTranslated(), assertFallback()).Locale Strategy:
App::setLocale()?Product::find(1)->translate('fr')).Translation Granularity:
name, description)?$translatable array in the model.Fallback Behavior:
default or return null?fallbackLocales() method or config('lang.models.fallback').Performance:
translated() method events).Multi-Tenant:
tenant_id in translation table)?tenant_id to the translation model).Validation:
Rule::unique('product_translations')->where('locale', $locale)).API Contracts:
JsonSerializable interface) or API resources.Internationalization (i18n) vs. Localization (l10n):
Str::of($text)->locale($locale)) alongside this package.Assessment Phase:
name, description, meta_title).Pilot Phase:
Page, Category) to test the package.php artisan make:model-translation Product --fields="name:string,description:text"
HasTranslations and define $translatable.Data Migration:
$products = Product::all();
foreach ($products as $product) {
$translations = json_decode($product->translations, true);
foreach ($translations as $locale => $data) {
$product->translate($locale, $data);
}
}
Feature Rollout:
Product, Article).Deprecation Phase:
fillable or guarded behavior (e.g., some audit packages).How can I help you explore Laravel packages today?