The extension is a package of tools to implement multilanguage in Yii2 project:
- Automatically redirects the user to the URL selected (automatically or manually) language and remembers the user selected language
- Automatically collect all new translates into DB
- Has a widget to set a correct hreflang attributes
- Provides a CRUD actions for edit the list of languages and the interface translations
- Has a widget to create language selector (for adminlte theme)
- Has a @weblang\
alias with current language, (*2)
-
The preferred way to install this extension is through composer, run:, (*3)
php composer.phar require --prefer-dist xz1mefx/yii2-multilang "~1"
-
Add new multilangCache component to common config file:, (*4)
'multilangCache' => [
'class' => \xz1mefx\multilang\caching\MultilangCache::className(),
],
-
Execute migration:, (*5)
php yii migrate --migrationPath=@vendor/xz1mefx/yii2-multilang/migrations --interactive=0
or you can create new migration and extend it, example:, (*6)
require(Yii::getAlias('@vendor/xz1mefx/yii2-multilang/migrations/m161210_131014_multilang_init.php'));
/**
* Class m161221_135351_multilang_init
*/
class m161221_135351_multilang_init extends m161210_131014_multilang_init
{
}
-
Override components in common config file:, (*7)
'urlManager' => [
'class' => \xz1mefx\multilang\web\UrlManager::className(),
],
'request' => [
'class' => \xz1mefx\multilang\web\Request::className(),
],
'i18n' => [
'class' => \xz1mefx\multilang\i18n\I18N::className(),
],
'lang' => [
'class' => \xz1mefx\multilang\components\Lang::className(),
],
-
[not necessary] If you use iiifx-production/yii2-autocomplete-helper
you need to run:, (*8)
-
Override some components in console config file:, (*9)
'request' => [ // override common config
'class' => 'yii\console\Request',
],
'urlManager' => [], // override common config
'i18n' => [], // override common config
-
Add HrefLangs widget to page <head></head>
section in layout(s):, (*10)
<?= \xz1mefx\multilang\widgets\HrefLangs::widget() ?>
-
Add LanguageController (or another) with next code:, (*11)
use xz1mefx\multilang\actions\language\IndexAction;
use xz1mefx\multilang\actions\language\CreateAction;
use xz1mefx\multilang\actions\language\UpdateAction;
use xz1mefx\multilang\actions\language\DeleteAction;
...
/**
* @inheritdoc
*/
public function actions()
{
return [
'index' => [
'class' => IndexAction::className(),
// 'theme' => IndexAction::THEME_ADMINLTE,
// 'canAdd' => false,
// 'canUpdate' => false,
// 'canDelete' => false,
],
'create' => [
'class' => CreateAction::className(),
// 'theme' => CreateAction::THEME_ADMINLTE,
],
'update' => [
'class' => UpdateAction::className(),
// 'theme' => UpdateAction::THEME_ADMINLTE,
],
'delete' => [
'class' => DeleteAction::className(),
// 'theme' => DeleteAction::THEME_ADMINLTE,
],
];
}
, where you can change action theme (THEME_BOOTSTRAP
- by default or THEME_ADMINLTE
)
, view path and access to controls in index action., (*12)
This controller will control system languages., (*13)
-
Add TranslationController (or another) with next code:, (*14)
use xz1mefx\multilang\actions\translation\IndexAction;
use xz1mefx\multilang\actions\translation\UpdateAction;
...
/**
* @inheritdoc
*/
public function actions()
{
return [
'index' => [
'class' => IndexAction::className(),
// 'theme' => IndexAction::THEME_ADMINLTE,
// 'canUpdate' => false,
],
'update' => [
'class' => UpdateAction::className(),
// 'theme' => UpdateAction::THEME_ADMINLTE,
],
];
}
, where you can change action theme (THEME_BOOTSTRAP
- by default or THEME_ADMINLTE
)
, view path and access to controls in index action., (*15)
This controller will control interface translations., (*16)
-
[not necessary, only for adminlte theme] Add language selector widget into header ul.nav
:, (*17)
<?= \xz1mefx\multilang\widgets\adminlte\HeaderDropDownLangSelector::widget() ?>