2017 © Pedro Peláez
 

library laravel-translatable

A trait to make an Eloquent model hold translations

image

zoomyboy/laravel-translatable

A trait to make an Eloquent model hold translations

  • Monday, September 18, 2017
  • by zoomyboy
  • Repository
  • 1 Watchers
  • 1 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 83 Forks
  • 0 Open issues
  • 16 Versions
  • 0 % Grown

The README.md

A trait to make Eloquent models translatable

Latest Version on Packagist Software License Build Status Quality Score StyleCI Total Downloads, (*1)

This package contains a trait to make Eloquent models translatable. Translations are stored as json. There is no extra table needed to hold them., (*2)

Once the trait is installed on the model you can do these things:, (*3)

$newsItem = new NewsItem; // This is an Eloquent model
$newsItem
   ->setTranslation('name', 'en', 'Name in English')
   ->setTranslation('name', 'nl', 'Naam in het Nederlands')
   ->save();

$newsItem->name; // Returns 'Name in English' given that the current app locale is 'en'
$newsItem->getTranslation('name', 'nl'); // returns 'Naam in het Nederlands'

app()->setLocale('nl');

$newsItem->name; // Returns 'Naam in het Nederlands'

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment we highly appreciate your sending us a postcard from your hometown, mentioning which of our package(s) you are using., (*4)

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium., (*5)

All postcards are published on our website., (*6)

Installation

You can install the package via composer:, (*7)

``` bash composer require spatie/laravel-translatable, (*8)


The package will automatically register itself. If you want to change add fallback_locale, you must publish the config file:

php artisan vendor:publish --provider="Spatie\Translatable\TranslatableServiceProvider", (*9)


This is the contents of the published file: ```php return [ 'fallback_locale' => 'en', ];

Making a model translatable

The required steps to make a model translatable are:, (*10)

  • First you need to add the Spatie\Translatable\HasTranslations-trait.
  • Next you should create a public property $translatable which holds an array with all the names of attributes you wish to make translatable.
  • Finally you should make sure that all translatable attributes are set to the text-datatype in your database. If your database supports json-columns, use that.

Here's an example of a prepared model:, (*11)

``` php use Illuminate\Database\Eloquent\Model; use Spatie\Translatable\HasTranslations;, (*12)

class NewsItem extends Model { use HasTranslations;, (*13)

public $translatable = ['name'];

}, (*14)


### Available methods #### Getting a translation The easiest way to get a translation for the current locale is to just get the property for the translated attribute. For example (given that `name` is a translatable attribute): ```php $newsItem->name;

You can also use this method:, (*15)

public function getTranslation(string $attributeName, string $locale) : string

This function has an alias named translate., (*16)

Setting a translation

``` php public function setTranslation(string $attributeName, string $locale, string $value), (*17)


To actually save the translation, don't forget to save your model. ```php $newsItem->setTranslation('name', 'en', 'Updated name in English'); $newsItem->save();

Forgetting a translation

You can forget a translation for a specific field: ``` php public function forgetTranslation(string $attributeName, string $locale), (*18)


You can forget all translations for a specific locale: ``` php public function forgetAllTranslations(string $locale)

Getting all translations in one go

``` php public function getTranslations(string $attributeName): array, (*19)


#### Setting translations in one go ``` php public function setTranslations(string $attributeName, array $translations)

Here's an example:, (*20)

``` php $translations = [ 'en' => 'Name in English', 'nl' => 'Naam in het Nederlands' ];, (*21)

$newsItem->setTranslations('name', $translations);, (*22)


### Events #### TranslationHasBeenSet Right after calling `setTranslation` the `Spatie\Translatable\Events\TranslationHasBeenSet`-event will be fired. It has these properties: ```php /** @var \Illuminate\Database\Eloquent\Model */ public $model; /** @var string */ public $attributeName; /** @var string */ public $locale; public $oldValue; public $newValue;

Creating models

You can immediately set translations when creating a model. Here's an example:, (*23)

NewsItem::create([
   'name' => [
      'en' => 'Name in English'
      'nl' => 'Naam in het Nederlands'
   ],
]);

Querying translatable attributes

If you're using MySQL 5.7 or above, it's recommended that you use the json data type for housing translations in the db. This will allow you to query these columns like this:, (*24)

NewsItem::where('name->en', 'Name in English')->get();

Using translations in json responses

The easiest way to add translations to json reponse is to override the toArray method on your model., (*25)

Here's a quick example:, (*26)

``` php // in your model, (*27)

/**
 * Convert the model instance to an array.
 *
 * @return array
 */
public function toArray()
{
    $attributes = parent::toArray();

    foreach ($this->getTranslatableAttributes() as $name) {
        $attributes[$name] = $this->getTranslation($name, app()->getLocale());
    }

    return $attributes;
}

}, (*28)


## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. ## Testing ```bash $ composer test

Contributing

Please see CONTRIBUTING for details., (*29)

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker., (*30)

Credits

We got the idea to store translations as json in a column from Mohamed Said. Parts of the readme of his multiligual package were used in this readme., (*31)

About Spatie

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website., (*32)

License

The MIT License (MIT). Please see License File for more information., (*33)

The Versions

18/09 2017

dev-laravel54

dev-laravel54 https://github.com/zoomyboy/laravel-translatable

A trait to make an Eloquent model hold translations

  Sources   Download

MIT

The Requires

 

The Development Requires

by Philipp Lang

eloquent model multilingual translate laravel-translatable i8n zoomyboy

18/09 2017

1.3.3

1.3.3.0 https://github.com/zoomyboy/laravel-translatable

A trait to make an Eloquent model hold translations

  Sources   Download

MIT

The Requires

 

The Development Requires

by Philipp Lang

eloquent model multilingual translate laravel-translatable i8n zoomyboy

03/09 2017

1.3.2

1.3.2.0 https://github.com/zoomyboy/laravel-translatable

A trait to make an Eloquent model hold translations

  Sources   Download

MIT

The Requires

 

The Development Requires

by Philipp Lang

eloquent model multilingual translate laravel-translatable i8n zoomyboy

03/09 2017

dev-master

9999999-dev https://github.com/zoomyboy/laravel-translatable

A trait to make an Eloquent model hold translations

  Sources   Download

MIT

The Requires

 

The Development Requires

eloquent model multilingual translate spatie laravel-translatable i8n

03/09 2017

1.3.1

1.3.1.0 https://github.com/zoomyboy/laravel-translatable

A trait to make an Eloquent model hold translations

  Sources   Download

MIT

The Requires

 

The Development Requires

by Philipp Lang

eloquent model multilingual translate laravel-translatable i8n zoomyboy

30/08 2017

dev-laravel-55

dev-laravel-55 https://github.com/spatie/laravel-translatable

A trait to make an Eloquent model hold translations

  Sources   Download

MIT

The Requires

 

The Development Requires

eloquent model multilingual translate spatie laravel-translatable i8n

30/08 2017

2.0.0

2.0.0.0 https://github.com/spatie/laravel-translatable

A trait to make an Eloquent model hold translations

  Sources   Download

MIT

The Requires

 

The Development Requires

eloquent model multilingual translate spatie laravel-translatable i8n

12/06 2017

1.3.0

1.3.0.0 https://github.com/spatie/laravel-translatable

A trait to make an Eloquent model hold translations

  Sources   Download

MIT

The Requires

 

The Development Requires

eloquent model multilingual translate spatie laravel-translatable i8n

28/01 2017

1.2.2

1.2.2.0 https://github.com/spatie/laravel-translatable

A trait to make an Eloquent model hold translations

  Sources   Download

MIT

The Requires

 

The Development Requires

eloquent model multilingual translate spatie laravel-translatable i8n

23/01 2017

1.2.1

1.2.1.0 https://github.com/spatie/laravel-translatable

A trait to make an Eloquent model hold translations

  Sources   Download

MIT

The Requires

 

The Development Requires

eloquent model multilingual translate spatie laravel-translatable i8n

23/01 2017

1.2.0

1.2.0.0 https://github.com/spatie/laravel-translatable

A trait to make an Eloquent model hold translations

  Sources   Download

MIT

The Requires

 

The Development Requires

eloquent model multilingual translate spatie laravel-translatable i8n

02/10 2016

1.1.2

1.1.2.0 https://github.com/spatie/laravel-translatable

A trait to make an Eloquent model hold translations

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

eloquent model multilingual translate spatie laravel-translatable i8n

24/08 2016

1.1.1

1.1.1.0 https://github.com/spatie/laravel-translatable

A trait to make an Eloquent model hold translations

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

eloquent model multilingual translate spatie laravel-translatable i8n

02/05 2016

1.1.0

1.1.0.0 https://github.com/spatie/laravel-translatable

A trait to make an Eloquent model hold translations

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

eloquent model multilingual translate spatie laravel-translatable i8n

11/04 2016

1.0.0

1.0.0.0 https://github.com/spatie/laravel-translatable

A trait to make an Eloquent model hold translations

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

eloquent model multilingual translate spatie laravel-translatable i8n

08/04 2016

0.0.1

0.0.1.0 https://github.com/spatie/laravel-translatable

A trait to make an Eloquent model hold translations

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

eloquent model multilingual translate spatie laravel-translatable i8n