Download the PHP package mindtwo/laravel-translatable without Composer
On this page you can find all versions of the php package mindtwo/laravel-translatable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mindtwo/laravel-translatable
More information about mindtwo/laravel-translatable
Files in mindtwo/laravel-translatable
Package laravel-translatable
Short Description This package is a Laravel extension for easy translation of model attributes, enabling seamless multi-language support in your application.
License MIT
Homepage https://github.com/mindtwo/laravel-translatable
Informations about the package laravel-translatable
Laravel Translatable Package
Overview
The mindtwo/laravel-translatable package provides a simple and effective way to manage multilingual models in a Laravel application. It allows you to easily translate Eloquent model attributes into multiple languages without the need for separate tables for each language.
Features
- ✅ Automatic Translation Override: Translated fields automatically override base model attributes
- ✅ Fallback Locale Support: Uses Laravel's app locale and fallback locale
- ✅ Query Scope Methods: Search, filter, and order by translated content
- ✅ Performance Optimized: Efficient database queries with proper indexing
- ✅ IDE Autocomplete: Full PHPDoc support for all methods
- ✅ Flexible Configuration: Customizable locale resolution and auto-translation
- ✅ Laravel Integration: Works seamlessly with Eloquent relationships and collections
Installation
To install the package, run the following command in your Laravel project:
You can publish and run the migrations with:
You can publish the config file with:
This is the contents of the published config file:
Quick Start
1. Create a Translatable Model
2. Add Translations
3. Automatic Translation Override
Advanced Usage
Query Scope Methods
All scope methods support IDE autocomplete and have full parameter type hints:
Locale Resolution
By default, the package uses Laravel's built-in locale configuration:
Custom Locale Resolution
You can customize locale resolution by extending the LocaleResolver:
Then register it in your config/translatable.php:
Or set locales dynamically:
Working with Translation Data
Performance Features
- Efficient Eager Loading: Use
withTranslations()to eager load translations and avoid N+1 queries - Optimized Subqueries: Order by translation uses database-agnostic subqueries
- Index-Friendly Queries: Uses
whereIn()andwhereColumn()for better database performance - Cached Locale Resolution: Locale chain resolved once per request
- Translation Map Indexing: O(1) attribute access after initial load
Configuration Examples
E-commerce Setup
Multi-Region Content
API Reference
Instance Methods
| Method | Description | Parameters |
|---|---|---|
setTranslation($key, $value, $locale) |
Set translation for a field | string $key, string $value, ?string $locale |
setTranslations($translations, $locale) |
Set multiple translations at once | array $translations, ?string $locale |
getTranslation($key, $locales) |
Get translated text with fallback | string $key, string\|array\|null $locales |
getTranslations($key, $locales) |
Get translations as array | string $key, string\|array\|null $locales |
getAllTranslations($key) |
Get all translations for a key | string $key |
hasTranslation($key, $locale) |
Check if translation exists | string $key, ?string $locale |
getUntranslated($key) |
Get original table value | string $key |
translations() |
Get translations relationship | - |
Query Scope Methods (Static)
| Method | Description | Parameters |
|---|---|---|
withTranslations($locales) |
Eager load translations | ?array $locales |
searchByTranslation($key, $search, $locales, $operator) |
Search in translations | string\|array $key, string $search, string\|array\|null $locales, string $operator |
searchByTranslationExact($key, $search, $locales) |
Exact match search | string\|array $key, string $search, string\|array\|null $locales |
searchByTranslationStartsWith($key, $search, $locales) |
Prefix search | string\|array $key, string $search, string\|array\|null $locales |
searchByTranslationEndsWith($key, $search, $locales) |
Suffix search | string\|array $key, string $search, string\|array\|null $locales |
whereHasTranslation($key, $locales) |
Filter models with translation | string $key, string\|array\|null $locales |
whereTranslation($key, $value, $locales, $operator) |
Filter by translation value | string $key, string $value, string\|array\|null $locales, string $operator |
orderByTranslation($key, $direction) |
Order by translated field | string $key, string $direction |
Required Configuration
Models using HasTranslations must define translatable fields:
Optional Configuration
IDE Support
The package includes comprehensive PHPDoc annotations for full IDE support:
- ✅ Autocomplete for all scope methods
- ✅ Parameter hints with proper types (
string|array,?string) - ✅ Return type information for method chaining
- ✅ Inline documentation on method hover
- ✅ Static analysis support (PHPStan compatible)
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- mindtwo GmbH
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-translatable with dependencies
illuminate/contracts Version ^10.18||^11.0||^12.0||^13.0
illuminate/database Version ^10.18||^11.0||^12.0||^13.0
mindtwo/laravel-auto-create-uuid Version ^2.6
spatie/laravel-package-tools Version ^1.14.0