2017 © Pedro Peláez
 

yii2-extension yii2-multilanguage

image

abcms/yii2-multilanguage

  • Friday, November 24, 2017
  • by abcms
  • Repository
  • 1 Watchers
  • 2 Stars
  • 170 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 4 Versions
  • 10 % Grown

The README.md

Yii2 ABCMS Multi-Language Component

Features:

  • Add a language bar widget to your website
  • Translate models
  • Manage languages from database or configuration
  • Message translation CRUD

Install:

composer require abcms/yii2-library:dev-master
composer require abcms/yii2-multilanguage:dev-master

Enable multi-language support in your website:

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

$config = [
    ......
    'language' => 'en',
    'sourceLanguage' => 'en',
    ......
];

2. Add multilanguage component

[
    'components' => [
        ......
        'multilanguage' => [
            'class' => 'abcms\multilanguage\Multilanguage',
            'languages' => [
                'en' => 'English',
                'ar' => 'Arabic',
                'fr' => 'French',
            ],
        ],
    ],
]

Add the component to the bootstrap array to allow it to read and set the language from cookies and URL:, (*1)

'bootstrap' => ['log', 'multilanguage'],

3. Add custom URL manager:

This URL manager class will automatically add the language to each URL., (*2)

'urlManager' => [
      'class' => abcms\multilanguage\UrlManager::className(),
      'enablePrettyUrl' => true,
      'showScriptName' => false,
      'rules' => [
          '<lang:([a-z]{2,3}(-[A-Z]{2})?)>/<controller>/<action>/' => '<controller>/<action>',
      ],
],

4. Add a language switcher to the layout.

Using the language bar widget:, (*3)

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

or manually:, (*4)

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

Enable translation for your models and CRUDs:

1. Migration:

1- Add the migration namespaces in the console.php configuration:, (*5)

'controllerMap' => [
    'migrate' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationNamespaces' => [
            'abcms\library\migrations',
            'abcms\multilanguage\migrations',
        ],
    ],
],

2- Run ./yii migrate, (*6)

You can use abcms/yii2-generators to generate a custom model and CRUD or continue with the manual steps below., (*7)

2. Add model behavior:

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

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

3. Add translation form in the admin panel:

Add in _form.php:, (*9)

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

4. Add translation detail view in the admin panel:

Add in view.php:, (*10)

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

5. Enable automatic translation saving in the controller

Add in Controller create and update actions:, (*11)

$model->automaticTranslationSaving = true;

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:

$translatedModels = Yii::$app->multilanguage->translateMultiple($models);

The Versions

24/11 2017

dev-master

9999999-dev

  Sources   Download

The Requires

 

21/10 2016

v0.3.0

0.3.0.0

  Sources   Download

The Requires

 

14/12 2015

v0.2.0

0.2.0.0

  Sources   Download

The Requires

 

14/12 2015

v0.1.0

0.1.0.0

  Sources   Download

The Requires