You can install the package via composer:
composer require organi/translatables
You can publish and run the migrations with:
php artisan vendor:publish --provider="Organi\Translatables\TranslatablesServiceProvider" --tag="translatables-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --provider="Organi\Translatables\TranslatablesServiceProvider" --tag="translatables-config"
This is the contents of the published config file:
return [
'accepted_locales' => [
'nl', 'en', 'fr', 'de',
],
];
Create a _translations
table for your model.
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('code');
$table->timestamps();
});
Schema::create('products_translations', function (Blueprint $table) {
$table->translations('translations');
$table->string('name');
$table->text('description');
});
Add the HasTranslations
trait to your model and a localizable
array containing the translatable fields.
use Organi\Translatables\Traits\HasTranslations;
class Product extends Model
{
use HasTranslations;
/**
* The attributes that should be translatable.
*/
protected array $localizable = [
'name', 'description'
];
}
Now you can add a translation to the name attribute of your model.
use Organi\Translatables\Models\Translation;
...
$product->name = Translation::make([
'nl' => 'Lorem ipsum dolor sit amet',
'en' => 'Lorem ipsum dolor sit amet',
'fr' => 'Lorem ipsum dolor sit amet',
]);
You can also set an array and the model will turn it in a translation:
$product->name = [
'nl' => 'Lorem ipsum dolor sit amet',
'en' => 'Lorem ipsum dolor sit amet',
'fr' => 'Lorem ipsum dolor sit amet',
];
You can also set the property to anything other than an array. In this case the default locale of the application will be set with the value.
$product->name = 'Lorem ipsum dolor sit amet';
/* returns:
Organi\Translatables\Models\Translation {
translations: array:4 [
"nl" => "Lorem ipsum dolor sit amet"
"en" => ""
"fr" => ""
]
}
*/
Or set multiple translations for a single locale
$product->setTranslations('nl', [
'name' => 'Lorem ipsum dolor sit amet',
'description' => 'Lorem ipsum dolor sit amet',
]);
Or set a single translation for a single locale
$product->setTranslation('nl', 'name', 'Lorem ipsum dolor sit amet');
Or set a single string for all locales
$product->setAllLocales('title', 'Lorem ipsum dolor sit amet');
Getting a translatable fields will return a Translation
object.
Converting it to a string will automatically take the value of the active locale. Some options are:
echo $product->name;
$dt->title->__toString(),
(string) $dt->title
Or you can get a specific locale:
echo $product->name->get('en');
This package provides a whereTranslation
function.
$product = Product::whereTranslation('title', 'Lorem ipsum dolor sit amet')->first();
This package provides a orderByTranslation
function.
The function has 3 parameters:
field
: field that should be used for the orderinglocale
: locale that should be used for the orderingdirection
(optional defaults toasc
):asc
ordesc
$products = Product::orderByTranslation('title')->get();