Download the PHP package themsaid/laravel-multilingual without Composer
On this page you can find all versions of the php package themsaid/laravel-multilingual. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download themsaid/laravel-multilingual
More information about themsaid/laravel-multilingual
Files in themsaid/laravel-multilingual
Package laravel-multilingual
Short Description Easy multilingual laravel models
License MIT
Homepage https://github.com/themsaid/laravel-multilingual
Informations about the package laravel-multilingual
Laravel 5 Multilingual Models
This laravel package makes Eloquent Models attributes translatable without the need to separate database tables for translation values.
You simply call $country->name
and you get a value based on your application's current locale.
You can also call $country->nameTranslations->en
to get the value of a specific locale.
You can check all the translations of a given attributes as easy as $country->nameTranslations->toArray()
.
Installation
Begin by installing the package through Composer. Run the following command in your terminal:
Once composer is done, add the package service provider in the providers array in config/app.php
Finally publish the config file:
That's all, you are now good to go.
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:
Now that you have the database ready to save a JSON string, you need to prepare your models:
- 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:
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:
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.
In case nothing can be found an empty string will be returned.
You may also want to return the value for a specific locale, you can do that using the following syntax:
This will return the English name of the country.
To return an array of all the available translations you may use:
Validation
You can use the new array validation features released with laravel 5.2 to validate the presence of specific locales:
However a validation rule is included in this package that deals with requiring all the validations to be provided:
The translatable_required
rule will make sure all the values of the available locales are set.
You may define the available locales as well as the fallback_locale from the package config file.
Now you only need to add the translated message of our new validation rule, add this to the validation.php
translation file:
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:
However in laravel 5.2.23 and above you can use the fluent syntax: