99namespace skeeks \yii2 \multiLanguage ;
1010
1111
12+ use yii \base \BootstrapInterface ;
13+ use yii \base \Event ;
14+ use yii \helpers \ArrayHelper ;
15+ use yii \helpers \Url ;
16+ use yii \web \Application ;
17+ use yii \web \View ;
1218/**
1319 * @property string $widgetSuffix
1420 *
1521 * @author Semenov Alexander <semenov@skeeks.com>
1622 */
17- class MultiLangComponent extends \yii \base \Component
23+ class MultiLangComponent extends \yii \base \Component implements BootstrapInterface
1824{
1925 /**
2026 * @var array доступные языки
@@ -25,4 +31,43 @@ class MultiLangComponent extends \yii\base\Component
2531 * @var string язык проекта по умолчанию
2632 */
2733 public $ default_lang = 'ru ' ;
34+
35+ /**
36+ * @var string
37+ */
38+ public $ lang_param_name = 'lang ' ;
39+
40+ /**
41+ * @param \yii\base\Application $application
42+ */
43+ public function bootstrap ($ application )
44+ {
45+ if ($ application instanceof Application) {
46+ $ application ->view ->on (View::EVENT_BEGIN_BODY , function (Event $ e ) {
47+ foreach ($ this ->langs as $ langCode )
48+ {
49+ $ urlData = [];
50+ $ params = \Yii::$ app ->request ->getQueryParams ();
51+ if ($ params )
52+ {
53+ $ params = ArrayHelper::merge ($ params , [$ this ->lang_param_name => $ langCode ]);
54+ } else
55+ {
56+ $ params = ArrayHelper::merge ([], [$ this ->lang_param_name => $ langCode ]);
57+ }
58+
59+ $ route = \Yii::$ app ->requestedRoute ;
60+ $ urlData = ["/ " . $ route ];
61+
62+ $ urlData = ArrayHelper::merge ($ urlData , $ params );
63+
64+ $ e ->sender ->registerLinkTag ([
65+ 'rel ' => 'alternate ' ,
66+ 'hreflang ' => $ langCode ,
67+ 'href ' => Url::to ($ urlData , true ),
68+ ]);
69+ }
70+ });
71+ }
72+ }
2873}
0 commit comments