PHP code example of jc-it / yii2-i18n-model

1. Go to this page and download the library: Download jc-it/yii2-i18n-model 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/ */

    

jc-it / yii2-i18n-model example snippets


public function behaviors(): array
{
    return [
        \JCIT\i18n\behaviors\I18nBehavior::class => [
            'class' => \JCIT\i18n\behaviors\I18nBehavior::class,
            'attributes' => [
                '<attribute>',
            ],       
        ]       
    ]; 
}

public function rules(): array
{
    return [
        [['<attribute>'], \yii\validators\RequiredValidator::class],
        [['<attribute>'], \yii\validators\StringValidator::class],
    ]; 
}

$model = new ClassWithI18nBehavior();
$model->locale = 'en-US';
$model-><attribute> = 'value en';
$model->save();

$model->locale = 'nl-NL';
$model-><attribute> = 'value nl';
$model->save();

$model = ClassWithI18nBehavior::findOne();
$model->locale = 'en-US';
echo $model-><attribute>; // 'value en'

$model->locale = 'nl-NL';
echo $model-><attribute>; // 'value nl'

class FormModel extends \yii\base\Model
{
    use \JCIT\i18n\traits\models\I18nTrait;
}