1. Go to this page and download the library: Download tuzelko/yii2-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/ */
use yii\db\ActiveRecord;
class PostLocalization extends ActiveRecord
{
public static function tableName(): string { return 'post_localization'; }
public function rules(): array
{
return [
[['lang', 'title'], '
use tuzelko\yii\localization\LocalizableBehavior;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
class Post extends ActiveRecord
{
public static function tableName(): string { return 'post'; }
public function behaviors(): array
{
return [
'localizable' => [
'class' => LocalizableBehavior::class,
'attributes' => ['title', 'description'],
],
];
}
public function getLocalizations(): ActiveQuery
{
return $this->hasMany(PostLocalization::class, ['post_id' => 'id']);
}
}
$post = Post::findOne(1);
Yii::$app->language = 'ru';
$post->title; // Russian title
Yii::$app->language = 'de'; // no German row
$post->title; // falls back to the default language
use tuzelko\yii\localization\LocalizationValidator;
use yii\base\Model;
class PostForm extends Model
{
public $localization;
public function rules(): array
{
return [
[['localization'], '