A little package to translate what you need in a model and provide to the user the translate field following the App Locale

1.3.0 2022-02-14 13:36 UTC

This package is auto-updated.

Last update: 2024-10-14 19:21:16 UTC


README

Latest Version on Packagist Total Downloads Build Status StyleCI

A very simple package to translate everything you need in a Laravel Model and present to the user the field in the language set in the application

Installation

Via Composer

$ composer require thenonsensefactory/translate

Usage

First run the needed migration:

$ php artisan migrate

Now your database have a new table called 'translations'

In order to have a model Transatable add the relative Trait

<?php

    use TheNonsenseFactory\Translate\Traits\Translatable;

    class Article extends Model {
        
        use Translatable;

    }

Then you have to declare what you need to translate in the $translatable array. This is a mandatory step.

<?php

    use TheNonsenseFactory\Translate\Traits\Translatable;

    class Article extends Model {
        
        use Translatable;

        protected $translatable = ['title', 'body'];

    }

To Save a new translation you can do in this way:

$article->translations()->create([
        'lang' => 'en',
        'field' => 'title',
        'text' => 'My Fancy Title'
    ]);

To save multiple translations for the same model (es. in a store method of a Controller) you can use the storeMultipleTranslations method

$translations = [
    'title' => [
        'it' => 'Il mio bel titolo',
        'en' => 'My fancy Title'
    ]
];

$article->storeMultipleTranslations($translations);

For sake of simplicity this method if found an existent translation it update the record. If you want to use a more specific o readable method yo can use:

$article->createOrUpdateMultipleTranslations($translations);

The magic came when you access to the field you have declared as Translatable. The package return the translation in the current App language if present or fallback in the Model table data.

//If the App Locale is 'en'

$article->title //Provide the en translation (if present)

//If the App locale is 'it'

$article->title //Provide the it translation (if present)

//If the App locale is 'de' and the translation does not exsist

$article->title //Provide the title from the Articles Table

You have a Query Scope and a useful help method

$article->translations()->currentLang() //Provide all the translations in the current App Locale set

$article->updateOrCreateTranslation($array) // Update a translation if present or create a new one in the current Language set in App Locale

The Array that you have to give to the updateOrCreateTranslation method must be in this shape:

field => text

for example:

['title' => 'My Fancy Title Updated']

Change log

Please see the changelog for more information on what has changed recently.

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email author email instead of using the issue tracker.

Credits

License

license. Please see the license file for more information.