|
| 1 | +# Laravel Translation |
| 2 | + |
| 3 | +Laravel Translation is a package that provides a simple and efficient way to manage translations in your Laravel applications. It allows you to store translations of all your models attributes in database, making it easy to manage and update translations without modifying language files. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +You can install the package via Composer: |
| 8 | + |
| 9 | +```bash |
| 10 | +composer require jetcod/laravel-translation |
| 11 | +``` |
| 12 | + |
| 13 | +## Configuration |
| 14 | +After installing the package, you need to publish the configuration file and migration: |
| 15 | + |
| 16 | +```bash |
| 17 | +php artisan vendor:publish --tag=translation-config |
| 18 | +php artisan vendor:publish --tag=translation-migrations |
| 19 | +``` |
| 20 | + |
| 21 | +Then, run the migration to create the translations table: |
| 22 | + |
| 23 | +```bash |
| 24 | +php artisan migrate |
| 25 | +``` |
| 26 | + |
| 27 | +In order to avoid conflictig with other packages and database tables, you can customize your database table name in the `config/translation.php` config file: |
| 28 | + |
| 29 | +```php |
| 30 | +return [ |
| 31 | + 'database' => [ |
| 32 | + 'prefix' => env('TRANSLATION_TABLE_PREFIX', 'lt_'), |
| 33 | + 'table_name' => env('TRANSLATION_TABLE_NAME', 'translations'), |
| 34 | + ], |
| 35 | +]; |
| 36 | +``` |
| 37 | + |
| 38 | +## Usage |
| 39 | + |
| 40 | +### Setup model |
| 41 | +First, you need to import the `Jetcode\Laravel\Translation\Traits\HasTranslations` trait in your model and specify the fields you want to translate: |
| 42 | + |
| 43 | +```php |
| 44 | +use Jetcode\Laravel\Translation\Traits\HasTranslations; |
| 45 | + |
| 46 | +class Post extends Model |
| 47 | +{ |
| 48 | + use HasTranslations; |
| 49 | + |
| 50 | + protected const TRANSLATABLE_ATTRIBUTES = ['title', 'content']; |
| 51 | +} |
| 52 | +``` |
| 53 | +Alternatively, you can specify the fields in a method in your model class: |
| 54 | + |
| 55 | +```php |
| 56 | +use Jetcode\Laravel\Translation\Traits\HasTranslations; |
| 57 | + |
| 58 | +class Post extends Model |
| 59 | +{ |
| 60 | + use HasTranslations; |
| 61 | + |
| 62 | + protected function getTranslatableAttributes() |
| 63 | + { |
| 64 | + return ['title', 'content']; |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +> **NOTE**: The `TRANSLATABLE_ATTRIBUTES` constant or the `getTranslatableAttributes` method return value can be either a string or an array of the model attribute names. |
| 70 | +
|
| 71 | + |
| 72 | +### Create a translation |
| 73 | +Now, you can create translations for the model attributes through the defined relations: |
| 74 | + |
| 75 | +```php |
| 76 | +// Create a new post with translations |
| 77 | +$post = Post::create([ |
| 78 | + 'title' => 'Hello World', |
| 79 | + 'content' => 'This is a post', |
| 80 | +]); |
| 81 | + |
| 82 | +// Create a new translation for the post |
| 83 | +$post->translation()->saveMany([ |
| 84 | + new Translation([ |
| 85 | + 'locale' => 'fr_FR', |
| 86 | + 'key' => 'title', |
| 87 | + 'value' => 'Bonjour le monde', |
| 88 | + ]), |
| 89 | + new Translation([ |
| 90 | + 'locale' => 'fr_FR', |
| 91 | + 'key' => 'content', |
| 92 | + 'value' => 'Ceci est un article', |
| 93 | + ]), |
| 94 | +]); |
| 95 | +``` |
| 96 | + |
| 97 | +### Retrieve translated model |
| 98 | +This package is compatible with Laravel localization system, so the models are translated according to the current locale. All you need to do is to set the appl locale and your model will be translated automatically: |
| 99 | + |
| 100 | +```php |
| 101 | +$post = Post::find(123); |
| 102 | +var_dump($post->title); // "Hello World" |
| 103 | + |
| 104 | +app()->setLocale('fr_FR'); |
| 105 | +var_dump($post->title); // "Bonjour le monde" |
| 106 | +``` |
| 107 | + |
| 108 | +## Testing |
| 109 | +The package includes tests that you can run using `PHPUnit`: |
| 110 | + |
| 111 | +```bash |
| 112 | +composer test |
| 113 | +``` |
| 114 | + |
| 115 | +You can also run static analysis with PHPStan: |
| 116 | + |
| 117 | +```bash |
| 118 | +composer phpstan |
| 119 | +``` |
| 120 | + |
| 121 | +## License |
| 122 | +This package is open-source software licensed under the [MIT license](LICENSE.md). |
0 commit comments