- How do I install and set up astrotomic/laravel-translatable in a Laravel 12 project?
- Run `composer require astrotomic/laravel-translatable`, then publish the config with `php artisan vendor:publish --tag=translatable`. Configure your default locale and translation table in `config/translatable.php`, and create a migration for your translation table (e.g., `post_translations`). Finally, add the `Translatable` trait to your model and define `$translatedAttributes`.
- Which Laravel versions and PHP versions are officially supported?
- The package supports Laravel 8 through 13 and PHP 8.0+. Always check the package’s GitHub releases for the latest compatibility details, especially if using Laravel’s LTS versions like 10.x or 11.x.
- How do I handle fallback locales if a translation is missing?
- Set a default locale (e.g., `en`) in your model’s `getTranslations()` method or globally via `App::setLocale()`. The package will automatically fall back to the default locale if a translation is missing. For example, `$post->translate('fr')->title` falls back to `$post->translate('en')->title` if French is missing.
- Can I use this package with Single Table Inheritance (STI) models?
- Yes, but you must manually override the `translationForeignKey` in your STI model to ensure translations are stored correctly. For example, `protected $translationForeignKey = 'type_id';` ensures translations are linked to the polymorphic parent table.
- How do I optimize performance for models with many translations?
- Use `getTranslationsArray()` to cache translations in memory or leverage Redis for session-based caching. Avoid eager-loading translations with `withTranslations()` in high-traffic scenarios; instead, fetch translations on-demand or use query scopes to limit loaded data.
- What’s the best way to validate locale-specific data in forms?
- Use Laravel’s validation rules with locale-specific keys. For example, `['en.title' => 'required', 'fr.title' => 'required']` ensures translations are validated per locale. You can also create custom validation rules or use form requests to handle locale-specific logic.
- Does this package support dynamic locales (e.g., user-selected languages)?
- Yes, but you must manually set the locale before accessing translations. For example, `App::setLocale($user->preferredLocale)` before calling `$model->translate()`. Dynamic locales require additional logic to handle fallback chains and avoid runtime errors.
- How do I migrate existing data to use this package?
- Create a seeder or script to backfill translations for existing records. Loop through your models, fetch current data, and save translations using `translateOrNew()` or direct database inserts into the translation table. Test thoroughly to ensure data integrity.
- Are there alternatives like spatie/laravel-translatable? How do I choose?
- Both packages offer similar functionality, but `astrotomic/laravel-translatable` uses a dedicated translation table (normalized) while `spatie/laravel-translatable` supports JSON columns (denormalized). Choose normalized for relational integrity or JSON for simplicity in MVP phases.
- How do I handle concurrent updates to translations in a multi-user environment?
- The package doesn’t include built-in concurrency controls, but you can use Laravel’s optimistic locking (e.g., `$model->increment('version')`) or database transactions (`DB::transaction()`) to prevent race conditions during translation updates.