Laravel 5 Multilingual Models
, (*1)
This laravel package makes Eloquent Models attributes translatable without the need to separate database tables for translation values., (*2)
You simply call $country->name
and you get a value based on your application's current locale., (*3)
You can also call $country->nameTranslations->en
to get the value of a specific locale., (*4)
You can check all the translations of a given attributes as easy as $country->nameTranslations->toArray()
., (*5)
Installation
Begin by installing the package through Composer. Run the following command in your terminal:, (*6)
composer require themsaid/laravel-multilingual
Once composer is done, add the package service provider in the providers array in config/app.php
, (*7)
Themsaid\Multilingual\MultilingualServiceProvider::class
Finally publish the config file:, (*8)
php artisan vendor:publish
That's all, you are now good to go., (*9)
Usage
First you need to make sure that the translatable attributes has a mysql field type of text or json, if you are building the database from a migration file you may do this:, (*10)
<?php
Schema::create('countries', function (Blueprint $table)
{
$table->increments('id');
$table->json('name');
});
Now that you have the database ready to save a JSON string, you need to prepare your models:, (*11)
<?php
class Country extends Model
{
use Themsaid\Multilingual\Translatable;
protected $table = 'countries';
public $translatable = ['name'];
public $casts = ['name' => 'array'];
}
- Add the
Translatable
trait to your model class
- Add a public class property
$translatable
as an array that holds the names of the translatable fields in your model.
- Remember to cast the translatable attribute as 'array' in the
$casts
property of the model.
Now our model has the name
attribute translatable, so on creating a new Model you may specify the name field as follow:, (*12)
<?php
Country::create([
'name' => [
'en' => "Spain",
'sp' => 'España'
]
]);
It'll be automatically converted to a JSON string and saved in the name field of the database, you can later retrieve the name like this:, (*13)
$country->name
This will return the country name based on the current locale, if the current locale doesn't have a value then the fallback_locale
defined in the config file will be used., (*14)
In case nothing can be found an empty string will be returned., (*15)
You may also want to return the value for a specific locale, you can do that using the following syntax:, (*16)
$country->nameTranslations->en
This will return the English name of the country., (*17)
To return an array of all the available translations you may use:, (*18)
$country->nameTranslations->toArray()
Validation
You can use the new array validation features released with laravel 5.2 to validate the presence of specific locales:, (*19)
<?php
$validator = Validator::make(
['name' => ['en'=>'One', 'sp'=>'Uno']],
['name.en' => 'required']
);
However a validation rule is included in this package that deals with requiring all the validations to be provided:, (*20)
<?php
$validator = Validator::make(
['name' => ['en'=>'One', 'sp'=>'Uno']],
['name' => 'translatable_required']
);
The translatable_required
rule will make sure all the values of the available locales are set., (*21)
You may define the available locales as well as the fallback_locale from the package config file., (*22)
Now you only need to add the translated message of our new validation rule, add this to the validation.php
translation file:, (*23)
'translatable_required' => 'The :attribute translations must be provided.',
Queries
If you're using MySQL 5.7 or above, it's recommended that you use the json data type for housing translations in the Database,
this will allow you to query these columns like this:, (*24)
Company::whereRaw('name->"$.en" = \'Monsters Inc.\'')->orderByRaw('specs->"$.founded_at"')->get();
However in laravel 5.2.23 and above you can use the fluent syntax:, (*25)
Company::where('name->en', 'Monsters Inc.')->orderBy('specs->founded_at')->get();