- How do I install and set up astrotomic/laravel-translatable in a Laravel project?
- Run `composer require astrotomic/laravel-translatable` to install the package. Then, use the `Translatable` trait in your Eloquent models and define translatable attributes in the `$translatedAttributes` property. The package automatically handles migrations for translation tables, so no additional setup is required unless you customize the schema.
- Does this package support Laravel 10 or 11? What about PHP 8.1+?
- The package is actively maintained and supports Laravel 10 and 11, as well as PHP 8.1 and later. Check the [GitHub releases](https://github.com/Astrotomic/laravel-translatable/releases) for version-specific compatibility notes. Older Laravel versions (e.g., 9.x) may require specific package versions.
- Can I use separate tables for translations or does it force JSON columns?
- The package defaults to separate tables for translations (e.g., `post_translations`), which is the recommended approach for scalability. JSON columns are not natively supported, though you could manually implement this by overriding the package’s behavior. Separate tables allow for better query performance and relational integrity.
- How do I handle nested locales like `en_US` or `fr_CA` with this package?
- The package fully supports nested locales by default. Simply define them in your `$translatedAttributes` array (e.g., `['title' => ['en_US', 'fr_CA']]`). Fallback locales are also configurable, and the package respects Laravel’s `App::setLocale()` for dynamic locale switching.
- Will using this package cause N+1 query issues when fetching translations?
- Yes, if you don’t use eager loading. Always call `withTranslations()` on your queries to preload translations and avoid N+1 queries. For APIs or admin panels, this is critical to maintain performance. Example: `Post::withTranslations()->get()`.
- How do I integrate translations with Laravel Scout for search functionality?
- Extend your model’s `toSearchableArray()` method to include translated attributes. Use `withTranslations()` in your search queries to ensure translations are indexed. For Algolia or other search engines, serialize translations in your API resources or custom searchable scopes.
- Can I use this package with Laravel Nova for admin panels?
- Yes, but you’ll need to create custom Nova fields or use extensions like `NovaTranslatable`. The package provides the underlying translation logic, but Nova requires additional configuration to display and edit translations in the UI. Check the [Nova documentation](https://nova.laravel.com/) for field customization.
- What’s the best way to test models with translations in PHPUnit?
- Use Laravel’s testing helpers like `actingAs()` and `assertDatabaseHas()` to verify translations. The package integrates seamlessly with PHPUnit, so you can test translation-specific logic by asserting attribute values per locale. Example: `$post->translate('title', 'es')->save(); $this->assertEquals('Título', $post->title);`.
- How do I handle fallback locales if a translation is missing?
- Configure fallback locales in your model’s `getFallbackLocales()` method or globally via `App::setFallbackLocale()`. The package will automatically fetch the fallback translation if the requested locale isn’t available. Example: `public function getFallbackLocales() { return ['en']; }` ensures English is used as a fallback.
- Are there performance concerns with writing translations for multiple locales?
- Writing translations for multiple locales increases database operations, which can impact performance under high load. To mitigate this, consider batching writes or using transactions. For read-heavy workloads, eager loading (`withTranslations()`) and caching (e.g., Redis) are recommended to reduce database load.