corecave / laratrans
Laravel helper package for building database mutli-lang websites.
This package is auto-updated.
Last update: 2024-09-07 09:26:03 UTC
README
Laravel translation package for building database localized mutli-lang websites..
Contents
Installation
You can install the package via composer:
composer require corecave/laratrans
Laravel auto-discovery with discover the service provicer
OR
Add the service provider manually:
// config/app.php 'providers' => [ // ... CoreCave\Laratrans\LaratransServiceProvider::class, ],
Append CoreCave\Laratrans\Middleware\Locale
to the $routeMiddleware
array in app\Http\Kernel.php
file.
// app\Http\Kernel.php protected $routeMiddleware = [ // ... 'laratrans' => \CoreCave\Laratrans\Middleware\Locale::class, ];
Put your database credentials to your .env
file
Then you should migrate the database
php artisan migrate
Usage
After you have installed laratrans package you need to add the
MasterTranslatable
and DetailsTranslatable
traits to your models that you want to make localizable. Additionaly,
NOTE:- Please note that slave model must be proceeded with underscore _
and its related table must be proceeded by double underscore __
(e.g. Person
=> _Person
, people
=> __people
)
Then, define the fields required by the package for localization to work properly on your migration:
//app\Person.php <?php namespace App; use CoreCave\Laratrans\Translation\MasterTranslatable; use Illuminate\Database\Eloquent\Model; class Person extends Model { use MasterTranslatable; }
//app\_Person.php <?php namespace App; use CoreCave\Laratrans\Translation\DetailsTranslatable; use Illuminate\Database\Eloquent\Model; class _Person extends Model { use DetailsTranslatable; }
If you need to specify a different foreign key name for your model, just override getForeignKeyName
on MasterTranslatable
trait.
//database\migrations\2020_07_06_185838_create_people_table.php <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreatePeopleTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('people', function (Blueprint $table) { $table->id(); $table->integer('age'); $table->float('hight'); $table->float('weight'); $table->timestamps(); }); Schema::create('__people', function (Blueprint $table) { $table->id(); $table->string('name'); $table->mediumText('bio'); $table->integer('person_id'); $table->integer('locale_id'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('people'); Schema::dropIfExists('__people'); } }
Then, setup some models, migrations, factories and seeders for locales
, people
and __people
tables or just you can publish them for faster development:
php artisan vendor:publish --tag="laratrans"
If you want to see localization in action, just setup some dummy routes in your routes/web.php
)file.
// routes/web.php use App\Person; use Illuminate\Support\Facades\Route; Route::get('/', function () { return Person::first()->trans(); }); Route::get('/ar', function () { return Person::first()->trans('ar'); }); Route::middleware('laratrans:web')->get('/trans', function () { return Person::first()->trans(); });
Change Application Locale.
To change your application localication code.
Issue a GET
request to http://yourdomain.com/localize/web/en
.
This route is constructed as http://yourdomain.com/localize/{guard}/{locale-code}
Consider a donation
Become a patreon and encourage us to do more. [Become a patreon]
Credits
License
The MIT License (MIT). Please see License File for more information.