PHP code example of wakjoko / localizable

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

    

wakjoko / localizable example snippets


class Person extends Model
{
    use \Wakjoko\Localizable\Localizable;

    protected $localizable = ['name', 'title', 'gender'];
}

$person = Person::withLocalizable()->find(1);
$person = Person::withLocalizable()->first();
$persons = Person::withLocalizable()->get();
$persons = Person::withLocalizable()->paginate();

// find anyone with he's name in english contains "john"
$models = Person::withLocalizable(
        attribute: 'name',
        locale: 'en',
        value: 'john'
    )
    ->get();

// insert new data along with localized language translation
$person = Person::create([
    'name' => [
        'ms' => 'Nuh',
        'en' => 'Noah'
    ]
]);

// another way of creating new data
$person = new Person([
    'name' => [
        'ms' => 'Nuh',
        'en' => 'Noah'
    ]
]);
$person->save();

// change my english name
$person->name('en', 'Charlie');
$person->save();

// or update via attribute
$person->name = [
    'ms' => 'Nuh',
    'en' => 'Noah'
];
$person->save();

// or use update() instead of save()
$person->update(['name' => ['en' => 'John']]);


$person->name               // prints Noah: use default lang in config('app.locale')
$person->setLocale('ms');   // switch language only on this model
$person->name               // prints: Nuh
$person->name('en');        // prints Noah: another way to get localized translation without changing default lang on the model

App\Models\Person {#10328
    id: 1,
    name: [
      "en" => "Noah",
      "ms" => "Nuh",
    ],
  }
bash
php artisan migrate