simplify managing translations for multilingual Eloquent models with power of morph relationships and caching .
Its lightweight design and flexibility make it an excellent choice for applications needing multi-language support with minimal performance overhead.
You can install the package via Composer:
composer require omaralalwi/lexi-translate
php artisan vendor:publish --provider="Omaralalwi\LexiTranslate\Providers\LexiTranslateServiceProvider" --tag=config
Run the following command to create the translations
table:
php artisan migrate
To use the package, include the LexiTranslatable
trait in your Eloquent models, and add translated attributes in $translatableFields
array:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Omaralalwi\LexiTranslate\Traits\LexiTranslatable;
class Post extends Model
{
use LexiTranslatable;
protected $translatableFields = ['title', 'description'];
}
Easily store translations for your model using the built-in relationship:
$post = Post::find(1);
// Store a translation for the title in Arabic
$post->translations()->create([
'locale' => 'ar',
'column' => 'title',
'text' => 'العنوان بالعربية',
]);
// Store a translation for the title in English
$post->translations()->create([
'locale' => 'en',
'column' => 'title',
'text' => 'English Title',
]);
To retrieve translations, simply use the translate
method:
$title = $post->transAttr('title', 'ar');
Or
$titleInArabic = $post->translate('title', 'ar');
$titleInEnglish = $post->translate('title', 'en');
You can eager load the translations relationship when querying your models to reduce the number of queries:
$posts = Post::with('translations')->get();
// Access translations directly after eager loading
foreach ($posts as $post) {
$titleInArabic = $post->translate('title', 'ar');
$titleInEnglish = $post->translate('title', 'en');
}
Disable Cache:
by default the cache enabled, you can disable it by make use_cache
= false , in config/lexi-translate.php
file
Cache Management:
Lexi Translate automatically caches translations to boost performance.
Also Cache is cleared automatically when translations are updated or deleted by booted
function in Translation
model .
Clear Model Cache Manually:
If you need to manually clear the cache, you can do so $model->clearTranslationsCache()
for ex :
$post->clearTranslationsCache();
Note:
Please note that the supported_locales
setting in the configuration file defines the locales that will be handled by the cache by default.
If you add additional locales for translations, make sure to include them in the supported_locales
list to ensure proper cache handling. Failing to do so may result in cache issues for locales not added to the list.
- Dynamic Morph Relationships: Manage translations across different models with ease, thanks to its dynamic morphable relationships.
- Automatic Caching: Enjoy enhanced performance as translations are automatically cached and invalidated, ensuring quick access and updates.
- Fallback Mechanism: Never worry about missing translations—Lexi Translate falls back to the default language if a translation is not available.
- Simple, Intuitive API: A clean and consistent API for adding, retrieving, and managing translations.
- Eloquent-Friendly: Seamlessly integrates with Laravel's Eloquent ORM, making it easy to work with translated data while maintaining the power of Laravel’s query builder.
- Feature Tests: supported with Feature Tests .
To run the tests for this package:
composer test
Please see CHANGELOG for more information on recent updates.
We welcome contributions! If you'd like to contribute, please check the CONTRIBUTING guide for details.
This project exists thanks to all the people who contribute.
If you discover any security-related issues, please email omaralwi2010@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see the License File for more information.
-
Gpdf Open Source HTML to PDF converter for PHP & Laravel Applications, supports Arabic content out-of-the-box and other languages..
-
laravel Taxify Laravel Taxify provides a set of helper functions and classes to simplify tax (VAT) calculations within Laravel applications.
-
laravel Deployer Streamlined Deployment for Laravel and Node.js apps, with Zero-Downtime and various environments and branches.
-
laravel Trash Cleaner clean logs and debug files for debugging packages.
-
laravel Time Craft simple trait and helper functions that allow you, Effortlessly manage date and time queries in Laravel apps.
-
Laravel Startkit Laravel Admin Dashboard, Admin Template with Frontend Template, for scalable Laravel projects.