mindtwo/laravel-translatable

This package is a Laravel extension for easy translation of model attributes, enabling seamless multi-language support in your application.

1.0.3 2024-02-01 12:38 UTC

This package is auto-updated.

Last update: 2024-04-30 13:09:38 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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

  • Easy integration with Eloquent models.
  • Seamless translation of model attributes.
  • Simple configuration and usage.

Installation

To install the package, run the following command in your Laravel project:

composer require mindtwo/laravel-translatable

You can publish and run the migrations with:

php artisan vendor:publish --tag="translatable-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="translatable-config"

This is the contents of the published config file:

<?php

use mindtwo\LaravelTranslatable\Models\Translatable;

return [
    'model' => Translatable::class,
];

Setup

After installation, you need to publish and run the migrations:

php artisan vendor:publish --provider="mindtwo\LaravelTranslatable\TranslatableServiceProvider"
php artisan migrate

This will create a translatable table in your database.

Usage

Creating Translatable Models

  1. Migration: Use the provided create_translatable_table.php migration to set up the translatable table.

  2. Model Trait: Include the HasTranslations trait in your model. This trait provides methods to interact with translations.

    use mindtwo\LaravelTranslatable\Traits\HasTranslations;
    
    class YourModel extends Model
    {
        use HasTranslations;
    
        // Model content
    }
  3. Translatable Model: The Translatable model is used to store translations. It uses the AutoCreateUuid trait for unique identification.

    use mindtwo\LaravelTranslatable\Models\Translatable;
    
    // Usage within your application logic

Working with Translations

  • Add a Translation:

    $yourModelInstance->translations()->create([
        'key' => 'your_key',
        'locale' => 'en',
        'text' => 'Your translation text'
    ]);
  • Check for a Translation:

    $exists = $yourModelInstance->hasTranslation('your_key', 'en');
  • Get a Translation:

    $translation = $yourModelInstance->getTranslation('your_key', 'en');
  • Retrieve All Translations:

    $translations = $yourModelInstance->getTranslations();

Best Practices

  • Always check if a translation exists before attempting to retrieve it.
  • Use consistent keys across different models to maintain clarity.
  • Regularly back up your translations as they are stored in the database.

Testing

composer test

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

License

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