PHP code example of tuzelko / yii2-localizable

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/ */

    

tuzelko / yii2-localizable example snippets


$this->createTable('{{%post}}', [
    'id' => $this->primaryKey(),
    // ... your non-localized columns
]);

$this->createTable('{{%post_localization}}', [
    'id'          => $this->primaryKey(),
    'post_id'     => $this->integer()->notNull(),
    'lang'        => $this->string(5)->notNull(),
    'is_default'  => $this->boolean()->notNull()->defaultValue(false),
    'title'       => $this->string()->notNull(),
    'description' => $this->text()->null(),
]);

$this->addForeignKey('fk_post_localization_post', '{{%post_localization}}', 'post_id', '{{%post}}', 'id', 'CASCADE');
$this->createIndex('idx_post_localization_post_lang', '{{%post_localization}}', ['post_id', 'lang'], true);

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

$post->localization;
// [
//     'default' => 'en',
//     'values'  => [
//         'en' => ['title' => 'Hello', 'description' => 'World'],
//         'ru' => ['title' => 'Привет', 'description' => 'Мир'],
//     ],
// ]

$post = new Post();
$post->localization = [
    'default' => 'en',
    'values'  => [
        'en' => ['title' => 'Hello', 'description' => 'World'],
        'ru' => ['title' => 'Привет', 'description' => 'Мир'],
    ],
];
$post->save();

use tuzelko\yii\localization\LocalizationValidator;
use yii\base\Model;

class PostForm extends Model
{
    public $localization;

    public function rules(): array
    {
        return [
            [['localization'], '

'components' => [
    'i18n' => [
        'translations' => [
            'localization' => [
                'class'    => \yii\i18n\PhpMessageSource::class,
                'basePath' => '@app/messages',
            ],
        ],
    ],
],