PHP code example of simexis / multi-language

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

    

simexis / multi-language example snippets


'providers' => [
    Simexis\MultiLanguage\MultiLanguageServiceProvider::class,
]

App::setLocale('ru');

  $greece = Country::where('code', 'gr')->first();
  echo $greece->translate('en')->name; // Greece
  
  App::setLocale('en');
  echo $greece->name;     // Greece

  App::setLocale('de');
  echo $greece->name;     // Griechenland

  $greece = Country::where('code', 'gr')->first();
  echo $greece->translate('en')->name; // Greece
  
  $greece->translate('en')->name = 'abc';
  $greece->save();
  
  $greece = Country::where('code', 'gr')->first();
  echo $greece->translate('en')->name; // abc

  $data = [
    'code' => 'gr',
    'en'  => ['name' => 'Greece'],
    'fr'  => ['name' => 'Grece'],
  ];

  $greece = Country::create($data);
  
  echo $greece->translate('fr')->name; // Grece

Schema::create('countries', function (Blueprint $table) {
	$table->increments('id');
	$table->string('code');
	$table->timestamps();
});
Schema::create('country_translations', function (Blueprint $table) {
	$table->increments('id');
	$table->integer('country_id')->unsigned();
	$table->string('name');
	$table->char(config('multilanguage.locale_key'), 2)->index('idx_locale');

	$table->unique(['country_id',config('multilanguage.locale_key')]);
	$table->foreign('country_id')->references('id')->on('countries')->onDelete('cascade');
	$table->foreign(config('multilanguage.locale_key'))
		->references(config('multilanguage.locale_key'))
		->on('languages')->onDelete('cascade')->onUpdate('cascade');
});

// models/Country.php
class Country extends Model
{

    use \Simexis\MultiLanguage\Traits\Translatable;
    
    public $translatedAttributes = ['name'];
    protected $fillable = ['code', 'name'];
	
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'countries';
}

// models/CountryTranslation.php
use Illuminate\Database\Eloquent\Model;

class CountryTranslation extends Model
{

    public $timestamps = false;
    protected $fillable = ['name'];
	
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'country_translations';
}

 

namespace App\Models;

class Country extends Model
{

    use \Simexis\MultiLanguage\Traits\Translatable;
    
    public $translatedAttributes = ['name'];
    protected $fillable = ['code', 'name'];
	
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'countries';

	//convention for the translation model is `CountryTranslation`.
    public $translationModel = 'MyApp\Models\CountryAwesomeTranslation';
}