Download the PHP package esign/laravel-underscore-translatable without Composer
On this page you can find all versions of the php package esign/laravel-underscore-translatable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download esign/laravel-underscore-translatable
More information about esign/laravel-underscore-translatable
Files in esign/laravel-underscore-translatable
Package laravel-underscore-translatable
Short Description A laravel package to make your eloquent models translatable.
License MIT
Homepage https://github.com/esign/laravel-underscore-translatable
Informations about the package laravel-underscore-translatable
Make Eloquent models translatable
This package allows you to make eloquent models translatable by using separate columns for each language, e.g. title_nl
and title_en
. This package is heavily inspired by Spatie's spatie/laravel-translatable.
Installation
You can install the package via composer:
Usage
Preparing your model
To make your model translatable you need to use the Esign\UnderscoreTranslatable\UnderscoreTranslatable
trait on the model.
Next up, you should define which fields are translatable by adding a public $translatable
property.
Your database structure should look like the following:
Retrieving translations
To retrieve a translation in the current locale you may use the attribute you have defined in the translatable
property. Or you could use the getTranslation
method:
To retrieve a translation in a specific locale you may use the fully suffixed attribute or pass the locale to the getTranslation
method:
To check if a translation exists, you may use the hasTranslation
method:
In case you do not supply a locale, the current locale will be used.
Using a fallback
This package allows you to return the value of an attribute's fallback_locale
defined in the config/app.php
of your application.
The third useFallbackLocale
parameter of the getTranslation
method may be used to control this behaviour:
Or you may use dedicated methods for this:
Setting translations
To set the translation for the current locale you may use the attribute you have defined in the translatable
property. Or you could pass it immediately when creating a model:
You may also use the setTranslation
method:
You could also set multiple translations at once using the setTranslations
method or immediately passing them along when creating a model:
This package does not persist translations to the database, so don't forget to save your model:
Defining accessors and mutators
You're able to define accessors just like you're used to in Laravel:
The same goes for mutators:
Testing
License
The MIT License (MIT). Please see License File for more information.