latevaweb/laravel-translatable
Add translatable Eloquent attributes backed by a Translations database table. Use a Translatable trait to set/get per-locale values, auto-resolve attributes by current app locale, and store translations via polymorphic relations.
## Technical Evaluation
**Architecture fit**
The `latevaweb/laravel-translatable` package is a lightweight, Laravel-specific solution for handling multilingual model attributes via a dedicated `Translations` table. It fits well within Laravel’s Eloquent ORM ecosystem, particularly for applications requiring dynamic, locale-aware attribute storage without complex schema changes. The package’s reliance on traits and polymorphic relationships aligns with Laravel’s modular design, making it suitable for projects where:
- Translations are attribute-level (not entire models).
- A separate `Translations` table is acceptable (avoiding JSON columns or nested structures).
- Laravel 8.x is the target version (or minimal validation is done for newer versions).
**Integration feasibility**
- **High for Laravel 8.x**: The package is explicitly tested and fixed for Laravel 8.x (post-0.1.8), reducing integration friction. The migration and configuration steps are straightforward (publish migrations/config, add trait to models).
- **Medium for Laravel 9+/10+**: While the package may work, the lack of active maintenance (last release in 2020) and no explicit Laravel 9+ support introduces risk. Teams on newer Laravel versions should evaluate alternatives like:
- Native Laravel localization (e.g., `json` columns with `localizedAttributes`).
- Dedicated packages like `spatie/laravel-translatable` (more maintained).
- **Dependencies**: Minimal (only Laravel core), but the package’s use of polymorphic relationships may require additional database indexes or query optimizations.
**Technical risk**
- **Low for Laravel 8.x**: The fix for Laravel 8 support suggests stability, but teams should validate:
- Performance impact of polymorphic queries (e.g., `Translations` table joins).
- Compatibility with custom Eloquent events or observers.
- **High for Laravel 9+/10+**: Risks include:
- Undocumented breaking changes in newer Laravel versions (e.g., PHP 8.1+ features, updated container syntax).
- Potential conflicts with Laravel’s evolving localization APIs (e.g., `Illuminate\Database\Eloquent\Concerns\WithLocalizedAttributes`).
- **Database schema risk**: The package requires a `Translations` table with polymorphic relationships (`model_type`, `model_id`). Teams must ensure:
- No naming conflicts with existing tables.
- Sufficient indexing for performance (e.g., `locale`, `attribute` columns).
**Key questions**
1. **Laravel version compatibility**:
- Are there untested Laravel 8.x edge cases (e.g., custom service providers, middleware) that could break the package?
- What is the package’s behavior with Laravel’s `app()->setLocale()` vs. route-based localization?
2. **Performance**:
- How does the package handle N+1 query issues when accessing translated attributes? Are there built-in optimizations (e.g., eager loading)?
- What is the overhead of storing translations in a separate table vs. JSON columns or nested models?
3. **Maintenance and support**:
- Is the package actively maintained? The last release (2020) and low stars/dependents suggest low activity.
- Are there alternatives (e.g., `spatie/laravel-translatable`) that offer better Laravel 9+/10+ support?
4. **Customization**:
- Can the `Translations` table schema be extended (e.g., adding `created_at`, `updated_at`, or user-specific fields)?
- How does the package handle soft deletes or model events (e.g., `deleting`, `saved`)?
5. **Testing**:
- What is the test coverage for Laravel 8.x? Are there CI checks for multiple Laravel versions?
- Are there known issues with specific database drivers (e.g., MySQL, PostgreSQL, SQLite)?
---
## Integration Approach
**Stack fit**
- **Laravel 8.x**: Ideal fit. The package is designed for Laravel 8.x and includes a fix for a critical compatibility issue (0.1.8). Teams using Laravel 8.x can proceed with minimal validation.
- **Laravel 9+/10+**: *Not recommended* without thorough testing. The package lacks explicit support, and newer Laravel versions may introduce breaking changes (e.g., PHP 8.1+ features, updated container syntax).
- **Non-Laravel PHP**: Not applicable. The package is tightly coupled to Laravel’s Eloquent ORM and service container.
**Migration path**
1. **Assessment phase**:
- Audit existing translation logic (e.g., JSON columns, nested models, or custom implementations).
- Identify models requiring translatable attributes and estimate effort to migrate.
2. **Proof of concept (PoC)**:
- Install the package in a staging environment (`composer require latevaweb/laravel-translatable`).
- Publish migrations and config (`php artisan vendor:publish`).
- Test with 1–2 critical models (e.g., `NewsItem`, `Product`).
3. **Gradual rollout**:
- **Phase 1**: Migrate non-critical models. Validate:
- Data integrity (no lost translations during migration).
- Performance (query times, memory usage).
- **Phase 2**: Migrate core models (e.g., `User`, `Post`). Test:
- API endpoints returning translated attributes.
- Admin panels or CMS interfaces.
- **Phase 3**: Replace custom translation logic (e.g., JSON columns) with the package’s trait.
4. **Fallback plan**:
- If issues arise, revert to custom implementations or use a more maintained package (e.g., `spatie/laravel-translatable`).
**Compatibility**
- **Laravel services**:
- **Service providers**: The package registers a `TranslatableServiceProvider`. Ensure no conflicts with existing bindings (e.g., `Translatable` trait or `Translations` table).
- **Facades**: The package does not expose facades, reducing risk.
- **Middleware**: No direct impact, but test routes using translated attributes.
- **Database**:
- The package requires a `Translations` table with columns: `id`, `model_type`, `model_id`, `attribute`, `locale`, `value`, and `created_at`. Ensure:
- No naming conflicts with existing tables.
- Appropriate indexes (e.g., `locale`, `attribute`, `model_type` + `model_id`).
- For existing data, write a migration to backfill translations from JSON columns or other sources.
- **PHP/Laravel versions**:
- Confirm PHP 7.4+ compatibility (Laravel 8.x requirement).
- Test with Laravel’s default testing framework to catch edge cases.
**Sequencing**
1. **Pre-integration**:
- Backup the database.
- Set up a staging environment mirroring production.
2. **Configuration**:
- Publish and run migrations (`php artisan migrate`).
- Configure the package (if needed) via `config/translatable.php`.
3. **Model integration**:
- Add the `Translatable` trait to models.
- Define `$translatable` static property (e.g., `['title', 'description']`).
- Update model migrations to include nullable translatable fields (e.g., `title`).
4. **Testing**:
- Unit tests: Validate `setTranslation()`/`getTranslation()` methods.
- Integration tests: Test API endpoints, admin panels, and user flows.
- Load testing: Simulate high traffic to check performance.
5. **Deployment**:
- Roll out in stages (e.g., feature flags for translated models).
- Monitor logs for errors (e.g., missing translations, query timeouts).
---
## Operational Impact
**Maintenance**
- **Effort**: Low to moderate.
- **Pros**:
- Minimal code changes (trait addition, config).
- No external dependencies beyond Laravel.
- **Cons**:
- Lack of active maintenance (last release in 2020). Teams must monitor for Laravel version conflicts.
- Custom logic may be needed for edge cases (e.g., soft deletes, custom locales).
- **Update strategy**:
- Pin the package version in `composer.json` to avoid unintended updates.
- Monitor Laravel releases for breaking changes affecting the package (e.g., Eloquent, service container).
**Support**
- **Debugging**:
- Common issues:
- Missing translations (check `Translations` table data).
- Query performance (add indexes, use eager loading).
- Locale fallback (test `app()->setLocale()` behavior).
- Tools:
- Laravel’s `DB::enableQueryLog()` to inspect polymorphic queries.
- `tinker` to test translation methods interactively.
- **Community**:
- Limited support due to low activity. Rely on:
- GitHub issues (check for Laravel 8.x-specific bugs).
- Laravel forums or Stack Overflow for general Eloquent questions.
- Consider forking the package if critical fixes are needed.
**Scaling**
- **Performance**:
- **Database**: The `Translations` table may grow large for models with many translations. Optimize with:
- Indexes on `locale`, `attribute`, and `model_type` + `model_id`.
- Query caching for frequently accessed translations.
- **Queries**: The package uses polymorphic relationships, which can lead to N+1 issues. Mitigate with:
- Eager loading: `$model->withTranslations()->get()` (if the package supports it).
- Local scope or accessors to limit
How can I help you explore Laravel packages today?