- How do I set up multilingual models in Laravel using this package?
- First, install the package via Composer: `composer require guidocella/laravel-multilingual`. Then, extend your Eloquent models with `MultilingualModel` and define translatable fields in the `$translatable` property. For example, `protected $translatable = ['name', 'description'];`. Run the migrations to create the translations table if it doesn’t exist.
- Does this package support Laravel 11? I’m planning to upgrade soon.
- The package is officially tested on Laravel 10.x, but it should work with Laravel 11 due to minimal breaking changes. Check the GitHub issues or test it in a staging environment before upgrading. If compatibility issues arise, the package’s simple architecture makes it easier to patch than more complex solutions.
- Can I use this package with existing database schemas that store translations differently (e.g., separate columns like `name_en`, `name_es`)?
- Yes, but you’ll need to migrate your data. The package expects a `translations` table with `locale`, `model_id`, and `fields` columns. You can backfill existing translations into this structure using Laravel migrations or a data seeder. For zero-downtime migrations, consider using a feature flag to toggle the package during the transition.
- How does locale detection work? Can I force a specific locale for certain routes or users?
- The package provides middleware and helpers to detect locales via URL, session, or cookies. You can override the default behavior by creating custom middleware or using the `setLocale()` helper. For route-specific locales, integrate it with `laravel-localization` or define route groups like `Route::middleware('locale.session')->group(...);`.
- What happens if a translation is missing for the current locale? How do I set fallback locales?
- By default, the package falls back to a predefined default locale (usually `en`). You can customize the fallback chain by overriding the `getFallbackLocales()` method in your model or globally via the package’s config. For example, set `['es' => 'en']` to fall back to English if Spanish is missing.
- Is this package suitable for APIs where I need to return locale-aware JSON responses?
- Yes, the package works seamlessly with APIs. When serializing models to JSON, include the `locale` in the response or use API resources to format data per locale. For example, `return new ProductResource(Product::find(1));` will automatically include translations if your resource is configured to handle them.
- How do I handle user-generated translations (e.g., comments or reviews) to prevent spam or malicious content?
- The package itself doesn’t include moderation features, but you can integrate Laravel’s built-in validation or third-party packages like `spatie/laravel-activitylog` to track changes. For spam prevention, use CAPTCHA or rate-limiting middleware on translation endpoints. Always sanitize input before storing translations.
- Can I use this package alongside other translation packages like `spatie/laravel-translatable`?
- While both packages achieve similar goals, they use different architectures. `spatie/laravel-translatable` stores translations as separate model instances, whereas this package uses a single `translations` table. You can use them together in a hybrid approach, but it may complicate migrations and queries. Evaluate your needs first—this package is lighter and more aligned with Eloquent conventions.
- Does this package support multilingual media (e.g., alt text for images, video captions) or only text?
- The package is designed for text-based translations (e.g., titles, descriptions) and stores them in a JSON or columnar format. For multilingual media, you’ll need to extend the `translations` table or use a separate system like `spatie/laravel-medialibrary` with custom locale fields. The package’s architecture allows for customization if you need to support non-text data.
- Are there performance considerations when dealing with thousands of translations? How can I optimize queries?
- The package doesn’t include built-in caching, but you can optimize performance by using Laravel’s query caching or Redis. Avoid N+1 issues by eager-loading translations with `with('translations')`. For high-traffic apps, consider denormalizing frequently accessed translations or using a read replica for translation-heavy queries. Test under load to identify bottlenecks.