amjad-ah / laravel-translation
A package to simplify translation in database
Requires
- illuminate/console: ^5.7|^6.0|^7.0|^8.0
- illuminate/database: ^5.7|^6.0|^7.0|^8.0
- illuminate/filesystem: ^5.7|^6.0|^7.0|^8.0
- illuminate/support: ^5.7|^6.0|^7.0|^8.0
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is not auto-updated.
Last update: 2024-05-06 22:34:29 UTC
README
Prerequisites
php8.0^, laravel8.*^
What it does
The package allows you to manage multi-language apps in database
Installation
composer require amjad-ah/laravel-translation
How to use
php artisan make:translatable Article
This command will generate 4 files, 2 migrations file & 2 models, the first model will be called Article
, the second one will be called ArticleTranslation
, I guess you know the tables names now,
let's look at the migrations now, the articles
table migration is an empty migration file, where you will fill your columns in, the second one will be like this
Schema::create('article_translations', function (Blueprint $table) { $table->id(); $table->string('lang_key')->comment('the iso code of the language ex: `en`, `es`'); $table->string('field_key'); $table->longText('field_value'); $table->foreignId('article_id') ->references('id') ->on('articles') ->onDelete('cascade'); $table->timestamps(); $table->index(['article_id', 'lang_key']); });
the models are very simple, the first one Article
, will implement AmjadAH\LaravelTranslation\Contracts\TranslatableInterface
, and use AmjadAH\LaravelTranslation\Traits\Translatable
,
the other one ArticleTranslation
will implement AmjadAH\LaravelTranslation\Contracts\TranslationsInterface
, and use AmjadAH\LaravelTranslation\Traits\TranslateScope
.
now publish the config file to edit the languages you are using and set the default language
php artisan vendor:publish --provider="AmjadAH\LaravelTranslation\TranslationServiceProvider"
now all you have to do is, in your "controller, service..." use the trait AmjadAH\LaravelTranslation\Traits\Translate
, for example:
use AmjadAH\LaravelTranslation\Traits\Translate; use App\Models\Article; class Service { use Translate; public function store() { $article = new Article; $article->title = 'Article title in English'; // this will be the default language $article->save(); $this->translate('title', $article, [ // there we will store the other languages 'es' => 'Article title in Spanish', 'ar' => 'Article title in Arabic' ]); } }
now to retrieve the corresponding language, all you have to do is:
$article = \App\Models\Article::first(); $article->trans('title'); // this will return the corresponding language to the locale language $article->trans('title', 'es'); // this will return the chosen language
License
The MIT License (MIT). Please see License File for more information.