composer require fazelit/yii2-multilanguage
'language' => 'en',
'sourceLanguage' => 'en',
'languages' => [
'en' => 'English',
'ar' => 'Arabic',
],
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>',
],
],
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>
./yii migrate --migrationPath=@vendor/abcms/yii2-library/migrations
./yii migrate --migrationPath=@vendor/abcms/yii2-multilanguage/migrations
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',
],
],
Add in _form.php
<?= \abcms\multilanguage\widgets\TranslationForm::widget(['model' => $model]) ?>
Add in view.php
<?=
\abcms\multilanguage\widgets\TranslationView::widget([
'model' => $model,
])
?>
$translatedModel = $model->translate();
use abcms\multilanguage\Multilanguage;
$translatedModels = Multilanguage::translateMultiple($models);