Package Data | |
---|---|
Maintainer Username: | luisdalmolin |
Maintainer Contact: | luis.nh@gmail.com (Luís Dalmolin) |
Package Create Date: | 2016-04-03 |
Package Last Update: | 2016-11-02 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-14 15:01:04 |
Package Statistics | |
---|---|
Total Downloads: | 62 |
Monthly Downloads: | 1 |
Daily Downloads: | 0 |
Total Stars: | 2 |
Total Watchers: | 4 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Easily translate your laravel models to as many languages you need.
Add this line to your composer.json
file:
"escapework/laravel-translations": "0.2.*"
And add this service provider to your laravel providers:
EscapeWork\Translations\TranslationServiceProvider::class
And publish the migrations running the following command:
$ php artisan vendor:publish --provider="EscapeWork\Translations\TranslationServiceProvider"
$ php artisan migrate
First, you need to create the locales that your models will be translated.
EscapeWork\Translations\Locale::create(['id' => 'pt-br', 'title' => 'Português (Brasil)']);
EscapeWork\Translations\Locale::create(['id' => 'en', 'title' => 'English']);
Then, you need to import the Translatable
in your models.
use EscapeWork\Translations\Translatable;
...
class Product extends Model
{
use Translatable;
}
For storing a translation, you can do the following:
// $data can have as many fields you want
$data = [
'title' => 'My translated title',
'description' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit',
];
$product->storeTranslation((array) $data, 'pt-br');
Here it's an example on how you can use with Laravel $request
object.
$product = Product::find(1);
foreach ((array) $request->translations as $locale => $data) {
$product->storeTranslation((array) $data, $locale);
}
$product->deleteTranslations();
For getting an existing translation, you just need to do this:
$product = Product::find(1);
echo $product->translations->_get('title'); // this will get the translation for the current config('app.locale') value
If you need an translation for an specific locale, just pass the locale as the second argument:
$product = Product::find(1);
echo $product->translations->_get('title', 'pt-br');
If you don't pass the $locale
, the default is gonna be the config('app.locale')
value.
You can also do something like this to make your life easier:
class Product extends Model
{
...
public function getTitleAttribute()
{
return $this->translations->_get('title');
}
...
}
// then, just call like a simple field
echo $product->title;
See the License file.