organi / translatables
Installs: 8 104
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- illuminate/bus: ^8.0|^9.0
- illuminate/console: ^8.0|^9.0
- illuminate/contracts: ^8.0|^9.0
- illuminate/database: ^8.0|^9.0
- illuminate/queue: ^8.0|^9.0
- illuminate/support: ^8.0|^9.0
- spatie/laravel-package-tools: ^1.4.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- orchestra/testbench: ^6.15
- pestphp/pest: ^1.18
- pestphp/pest-plugin-faker: ^1.0
- pestphp/pest-plugin-laravel: ^1.1
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 orderinglocale
: locale that should be used for the orderingdirection
(optional defaults toasc
):asc
ordesc
$products = Product::orderByTranslation('title')->get();