|
| 1 | +# Installation |
| 2 | + |
| 3 | +### Install package |
| 4 | + |
| 5 | +Add the package in your `composer.json` by executing the command. |
| 6 | + |
| 7 | +```bash |
| 8 | +composer require astrotomic/laravel-translatable |
| 9 | +``` |
| 10 | + |
| 11 | +### Configuration |
| 12 | + |
| 13 | +We copy the configuration file to our project. |
| 14 | + |
| 15 | +```bash |
| 16 | +php artisan vendor:publish --tag=translatable |
| 17 | +``` |
| 18 | + |
| 19 | +{% hint style="info" %} |
| 20 | +There isn't any restriction for the format of the locales. Feel free to use whatever suits you better, like "eng" instead of "en", or "el" instead of "gr". The important is to define your locales and stick to them. |
| 21 | +{% endhint %} |
| 22 | + |
| 23 | +### Migrations |
| 24 | + |
| 25 | +In this example, we want to translate the model `Post`. We will need an extra table `post_translations`: |
| 26 | + |
| 27 | +{% code-tabs %} |
| 28 | +{% code-tabs-item title="create\_posts\_table.php" %} |
| 29 | +```php |
| 30 | +Schema::create('posts', function(Blueprint $table) { |
| 31 | + $table->increments('id'); |
| 32 | + $table->string('author'); |
| 33 | + $table->timestamps(); |
| 34 | +}); |
| 35 | +``` |
| 36 | +{% endcode-tabs-item %} |
| 37 | +{% endcode-tabs %} |
| 38 | + |
| 39 | +{% code-tabs %} |
| 40 | +{% code-tabs-item title="create\_post\_translations\_table" %} |
| 41 | +```php |
| 42 | +Schema::create('post_translations', function(Blueprint $table) { |
| 43 | + $table->increments('id'); |
| 44 | + $table->integer('post_id')->unsigned(); |
| 45 | + $table->string('locale')->index(); |
| 46 | + $table->string('title'); |
| 47 | + $table->text('content'); |
| 48 | + |
| 49 | + $table->unique(['post_id', 'locale']); |
| 50 | + $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'); |
| 51 | +}); |
| 52 | +``` |
| 53 | +{% endcode-tabs-item %} |
| 54 | +{% endcode-tabs %} |
| 55 | + |
| 56 | +### Models |
| 57 | + |
| 58 | +The translatable model `Post` should [use the trait](http://www.sitepoint.com/using-traits-in-php-5-4/) `Astrotomic\Translatable\Translatable`. The default convention for the translation model is `PostTranslation`. The array `$translatedAttributes` contains the names of the fields being translated in the `PostTranslation` model. |
| 59 | + |
| 60 | +{% code-tabs %} |
| 61 | +{% code-tabs-item title="Post.php" %} |
| 62 | +```php |
| 63 | +class Post extends Eloquent |
| 64 | +{ |
| 65 | + use \Astrotomic\Translatable\Translatable; |
| 66 | + |
| 67 | + public $translatedAttributes = ['title', 'content']; |
| 68 | + protected $fillable = ['author']; |
| 69 | +} |
| 70 | +``` |
| 71 | +{% endcode-tabs-item %} |
| 72 | +{% endcode-tabs %} |
| 73 | + |
| 74 | +{% code-tabs %} |
| 75 | +{% code-tabs-item title="PostTranslation.php" %} |
| 76 | +```php |
| 77 | +class PostTranslation extends Eloquent |
| 78 | +{ |
| 79 | + public $timestamps = false; |
| 80 | + protected $fillable = ['title', 'content']; |
| 81 | +} |
| 82 | +``` |
| 83 | +{% endcode-tabs-item %} |
| 84 | +{% endcode-tabs %} |
| 85 | + |
0 commit comments