1. Go to this page and download the library: Download muratsplat/multilang 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/ */
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePagesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('pages', function(Blueprint $t) {
$t->increments('id');
$t->boolean('enable')->default(0);
// you can add more for your needs
// but don't add columns for multi language
$t->timestamp('created_at')->nullable();
$t->timestamp('updated_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('pages');
}
}
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePageLangsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('pageLangs', function(Blueprint $t) {
$t->increments('id');
$t->integer('page_id')->unsigned();
// it is oreign('page_id')->references('id')->on('pages');
// This reference is optional. If you want to language model
// to manage app languages, the refrences is recommended.
// Having a Language model make be easy to manage language
$t->foreign('__lang_id__')->references('id')->on('languages');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('contents');
}
}
use Illuminate\Database\Migrations\Migration;
class languages extends Migration {
public function up() {
Schema::create('languages', function($t) {
$t->increments('id');
$t->string('lang_code', 10);
$t->string('name', 50);
$t->string('name_native', 50);
$t->tinyInteger('enable' )->default(0);
$t->boolean('default')->default(false);
$t->timestamps();
$t->index('lang_code');
$t->unique(array('lang_code', 'name'));
});
}
public function down() {
Schema::drop('languages');
}
}
// in pageLangs migration class
$t->foreign('__lang_id__')->references('id')->on('languages');
$rawPost = array(
"enable" => 1,
'title@1' => "Foo English",
'content@1' => "Simple example of content in English",
'title@2' => 'Foo Türkçe',
'content@2' => 'Türkçe bir içerik langur lungur bir yoğurt',
"title@3" => 'здравствуйте',
"content@3" => 'Путинхороший человек. Он любит русские , я думаю, россияне любят его.'
);
$rules = ['enable' => 'dels, it is overwrote on same rule in models.
// So one of in model is updated by overwriting
// Rules parameter is optional.
// If you have been defined rules in Page model, rules parameter is not need.
if(MultiLang::create($rawPost, new Page(), $rules)) {
// it is in success
} else {
$instace = MultiLang::getInstance();
Redirect::route('panel.create')->withErrors($instace)->withInput();
}
$wantedLangId = 3;
$defaultLangId = 1; // if the value is null or empty, returns PageLang models by Language id
$wrapper = MultiLang::makeWrapper(Page::find(1), $wantedLangId,$defaultLangId);
echo $wrapper->title; // returns: "здравствуйте"
echo $wrapper->content // returns: "Путинхороший человек. Он любит русские , я думаю, россияне любят его."
$wrapper->wanted(2)->force()->title; // "Foo Türkçe"
// 99 language id is not existed and so no record is on PageLang Model !
$wrapper->wanted(99)->force()->title; // null
private function setMultilangWrapperByEvent($lang_id = 1) {
$events = App::make('events');
/* setting wanted lang by using event */
$events->listen('multilang.wrapper.creating',function(MultiLang $event) use($lang_id) {
$event->getWrapperInstance()->setWantedLang($lang_id);
});
}