simsol/yii2-multilang

There is no license information available for the latest version (v1.0.7) of this package.

Extensions for multi languages in objects

Installs: 19

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Type:yii2-extension

v1.0.7 2021-08-30 04:34 UTC

This package is auto-updated.

Last update: 2024-04-29 04:21:36 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

This is a Guide for using MultiLang Module

1. Install

composer require simsol/yii2-multilang

2. Init

Module declaration and config for languages to be stored:

    'modules' => [
        'multilang' => [
            'class'     => 'simsol\multilang\Module',
            //config for languages, default will be ["en"=>"ENG"]."en" is language code and "ENG" is the label to be displayed
            'languages' => [
                'en' => 'ENG',
                'vi' => 'VN',
            ],
        ],
    ],

3. Run Migrations

./yii migrate --migrationPath=@simsol/multilang/migrations

4. Usage

Please follow below steps to set up and use this extension.

a. In model

  • Add property translation to your model class.

    public $translations;
    
  • Add rule for translation property.

    /**
      * {@inheritdoc}
      */
      public function rules()
      {
         return [
            ...
            [['translations'],'safe'],
         ];
      }
    
  • Modify afterSave() and add this line:

    Translate::saveTranslation($this->translations,$this->id_model,Model::class);
    

    This will save translation for current model.

b. In form

  • Add this widget for attributes that you want to provide translation to your create or update form.

    <?=$form->field($model,'attribute')->widget(MultiLangFieldsWidget::class)->label(false)?>
    
  • Available config:

    • attributeLabel: Optional. In case you want to use another name for model attribute, default is model's attribute name.
    • attributeToSave: Optional. In case you want to save with a different attribute, default is model's attribute. Useful when working with custom model.
    • attributeToLoad: Optional. In case you want to load saved translation value on edit with a different attribute, default is model's attribute. Useful when working with custom model.
    • inputType: Optional. Default is text input, available options:
      • Text Input: MultiLangFieldsWidget::TYPE_TEXT_INPUT
      • Textarea: MultiLangFieldsWidget::TYPE_TEXTAREA
    <?=$form->field($model,'attribute')->widget(MultiLangFieldsWidget::class,[
                    'inputType' => MultiLangFieldsWidget::TYPE_TEXTAREA,
                    'attributeLabel' => 'Another name',
                ])->label(false)?>
    

c. Query translations

 Translate::loadTranslation($model,$attribute = null,$modelId = null;$modelClass = null,$language = null)
  • $model: Required. Model to load translations.

  • $attribute: Optional. Translations for specified attribute will be queried if provided.

  • $modelId: Optional. In case the model id is different from defined $model, useful when working with custom model.

  • $modelClass: Optional. In case model class name is different from provided model. For example: app\models\form\CustomerForm and app\models\Customer

  • $language: Optional. If passed, value will be used to query for specified language.

  • Query results: Result might be varied depends on provided parameters. See examples below:

Translate::loadTranslation($model);

Result:

[
    'description' => [
        'en' => 'Description of data.',
        'vi' => 'Mô tả của dữ liệu',
    ],
    'title' => [
        'en' => 'Title of data',
        'vi' => 'Tiêu đề của dữ liệu',
    ],
]
Translate::loadTranslation($model,'title');

Result:

[
        'en' => 'Title of data',
        'vi' => 'Tiêu đề của dữ liệu',
]
Translate::loadTranslation($model,'title',null,null,'en');

Result:

'Title of data'
Translate::loadTranslation($model,null,null,null,'en');

Result:

[
    'description' => 'Description of data.',
    'title' => 'Title of data',
]