organi/translatables

2.10.0 2023-02-13 13:36 UTC

This package is auto-updated.

Last update: 2024-04-29 06:58:54 UTC


README

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

Usage

Make your eloquent model translatable.

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

Setting translatable fields

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 translatable fields

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

Filtering on a translatable fields

This package provides a whereTranslation function.

$product = Product::whereTranslation('title', 'Lorem ipsum dolor sit amet')->first();

Ordering on a translatable fields

This package provides a orderByTranslation function. The function has 3 parameters:

  • field: field that should be used for the ordering
  • locale: locale that should be used for the ordering
  • direction (optional defaults to asc): asc or desc
$products = Product::orderByTranslation('title')->get();