A little package to translate what you need in a model and provide to the user the translate field following the App Locale
thenonsensefactory/translate is a Laravel package for a little package to translate what you need in a model and provide to the user the translate field following the app locale.
It currently has 0 GitHub stars and 426 downloads on Packagist (latest version 1.3.1).
Install it with composer require thenonsensefactory/translate.
Discover more Laravel packages by thenonsensefactory
or browse all Laravel packages to compare alternatives.
Last updated
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
Via Composer
$ composer require thenonsensefactory/translate
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']
Please see the changelog for more information on what has changed recently.
Please see contributing.md for details and a todolist.
If you discover any security related issues, please email author email instead of using the issue tracker.
license. Please see the license file for more information.