PHP code example of somar / translation

1. Go to this page and download the library: Download somar/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/ */

    

somar / translation example snippets


composer 

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

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

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

php artisan vendor:publish --provider="Somar\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' => [
            \Somar\Translation\Middleware\RequestLanguage::class,
            // ...
        ],
    ];

use Somar\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;