Skip to content

simplify managing translations for multilingual Eloquent models with power of morph relationships and caching .

License

Notifications You must be signed in to change notification settings

YemenOpenSource/lexi-translate

 
 

Repository files navigation

Lexi Translate

lexi translate banner

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.

Table of Contents

Installation

You can install the package via Composer:

composer require omaralalwi/lexi-translate

Publishing Configuration File

php artisan vendor:publish --provider="Omaralalwi\LexiTranslate\Providers\LexiTranslateServiceProvider" --tag=config

Migration for translations Table

Run the following command to create the translations table:

php artisan migrate

Usage

Defining LexiTranslatable Models

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'];
}

Create Translations

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',
]);

Retrieving Translations

To retrieve translations, simply use the translate method:

$title = $post->transAttr('title', 'ar');

Or

$titleInArabic = $post->translate('title', 'ar');
$titleInEnglish = $post->translate('title', 'en');

Eager Loading Translations

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');
}

Cache Handling

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.


Features

  • 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 .

Testing

To run the tests for this package:

composer test

Changelog

Please see CHANGELOG for more information on recent updates.

Contributing

We welcome contributions! If you'd like to contribute, please check the CONTRIBUTING guide for details.

Contributors

This project exists thanks to all the people who contribute.

Security

If you discover any security-related issues, please email omaralwi2010@gmail.com instead of using the issue tracker.

License

The MIT License (MIT). Please see the License File for more information.


📚 Helpful Open Source Packages

  • laravel Taxify 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 Laravel Taxify provides a set of helper functions and classes to simplify tax (VAT) calculations within Laravel applications.

  • laravel Deployer laravel Deployer Streamlined Deployment for Laravel and Node.js apps, with Zero-Downtime and various environments and branches.

  • laravel Trash Cleaner laravel Trash Cleaner clean logs and debug files for debugging packages.

  • laravel Trash Cleaner laravel Time Craft simple trait and helper functions that allow you, Effortlessly manage date and time queries in Laravel apps.

  • Laravel Startkit Laravel Startkit Laravel Admin Dashboard, Admin Template with Frontend Template, for scalable Laravel projects.

About

simplify managing translations for multilingual Eloquent models with power of morph relationships and caching .

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%