PHP code example of mosab / translation

1. Go to this page and download the library: Download mosab/translation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mosab / translation example snippets


composer 

'providers' => [
    // ...
    Mosab\Translation\TranslationServiceProvider::class,
];

php artisan vendor:publish --provider="Mosab\Translation\TranslationServiceProvider"

php artisan vendor:publish --provider="Mosab\Translation\TranslationServiceProvider" --tag=migrations

php artisan vendor:publish --provider="Mosab\Translation\TranslationServiceProvider" --tag=seeders

php artisan migrate

php artisan db:seeder --class=TranslationsLanguageSeeder 

RequestLanguage::$all_languages = ['en', 'ar', 'code for new language'];

TranslationsLanguage::create([
    'code'  => 'code for new language',
    'title' => 'new language name',
]);

protected $middlewareGroups = [
        'api' => [
            \Mosab\Translation\Middleware\RequestLanguage::class,
            // ...
        ],
    ];

use Mosab\Translation\Database\TranslatableModel;

class Test extends TranslatableModel
{
    // ...
}

class Employee extends TranslatableModel
{
    // ...
    protected $fillable = [
        'salary'
    ];

    protected $translatable = [
        'name',
        'position',
    ];
}

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
            $table->id();
            $table->float('salary');
            $table->timestamps();
        });
    }
     /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('employees');
    }
}

class EmployeeController extends Controller
{
    // ...
    public function store(Request $request)
    {
        // Validate the request...
 
        $employee = new Employee();
 
        $employee->name = [
            'en'  => 'name in english',
            'ar'  => 'name in arabic',
        ];
        $employee->position = [
            'en'  => 'position in english',
            'ar'  => 'position in arabic',
        ];
        $employee->salary = 5000,
 
        $employee->save();
    }
}

class EmployeeController extends Controller
{
    // ...
    public function store(Request $request)
    {
        // Validate the request...
 
        $employee = Employee::create([
            'name'     => 'translation name',
            'position' => 'translation position',
            'salary'   => 5000,
        ]);
    }
}

class EmployeeController extends Controller
{
    // ...
    public function store(Request $request)
    {
        $request->validate([
            'name'      => ['      $employee->name = $request->name,
        $employee->position = $request->position,
        $employee->salary = $request->salary,
 
        $employee->save();
    }
}

class EmployeeController extends Controller
{
    // ...
    public function update($id ,Request $request)
    {
        // Validate the request...

       $employee = Employee::find($id);

       $employee->name = [
            'en'  => 'name in english',
            'ar'  => 'name in arabic',
        ];
        $employee->position = [
            'en'  => 'position in english',
            'ar'  => 'position in arabic',
        ];
 
        $employee->save();
    }
}

class EmployeeController extends Controller
{
    // ...
    public function update($id ,Request $request)
    {
        // Validate the request...

       $employee = Employee::find($id);

       $employee->update = [
            'name'     => ['en'=> 'name in english','ar'=> 'name in arabic'], 
            'position' => ['en'=> 'position in english','ar'=> 'position in arabic'],
       ];
    }
}

class EmployeeController extends Controller
{
    // ...
    public function destroy($id)
    {
       $employee = Employee::find($id);

       $employee->delete();
    }
}

class EmployeeController extends Controller
{
    // ...
    public function show($id)
    {
        $employee = Employee::find($id);
        
        return $employee;
    }
}

Accept-Language : en;