Skip to content

fazelit/yii2-multi-language

Repository files navigation

Yii2 Fazelit Multi Language

Install:

composer require fazelit/yii2-multilanguage

Enable multi language support in your website:

1. Add language and sourceLanguage attributes to your config array.

'language' => 'en',
'sourceLanguage' => 'en',

2. Add languages list to params array in params.php

'languages' => [
    'en' => 'English',
    'ar' => 'Arabic',
],

3. Add custom url manager:

This url manager class will automatically add the language to each url.

'urlManager' => [
      'class' => abcms\multilanguage\UrlManager::className(),
      'enablePrettyUrl' => true,
      'showScriptName' => false,
      'rules' => [
          '<lang:\w{2}>/<controller>/<action>/' => '<controller>/<action>',
      ],
],

5. Add language switcher to the layout.

Using language bar widget:

<?= abcms\multilanguage\widgets\LanguageBar::widget() ?>

or manually:

<a class="<?= (Yii::$app->language == 'en') ? 'active' : ''; ?>" href="<?= Url::current(['lang' => 'en']) ?>">En</a>

Enable multi language support in your model and crud:

1. Migration:

./yii migrate --migrationPath=@vendor/abcms/yii2-library/migrations
./yii migrate --migrationPath=@vendor/abcms/yii2-multilanguage/migrations

2. Add model behvarior:

Add the multi language behavior and specify which attributes can be translated and the type for each field. If field type is not specified, text input will be used by default.

[
    'class' => \abcms\multilanguage\behaviors\ModelBehavior::className(),
    'attributes' => [
        'title',
        'description:text-area',
    ],
],

3. Add translation form in the admin panel:

Add in _form.php

<?= \abcms\multilanguage\widgets\TranslationForm::widget(['model' => $model]) ?>

4. Add translation detail in the admin panel:

Add in view.php

<?=
\abcms\multilanguage\widgets\TranslationView::widget([
    'model' => $model,
])
?>

How to get translated content?

Get a single model translation for the current language:

$translatedModel = $model->translate();

Get multiple models translation for the current language:

use abcms\multilanguage\Multilanguage;

$translatedModels = Multilanguage::translateMultiple($models);

About

yii2-multi-language for php 7.2>

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages