S
S
santaTihonov2020-06-29 23:36:23
Yii
santaTihonov, 2020-06-29 23:36:23

How is language stored when multilingual in Yii2?

I'm trying to implement multilanguage on the site (on the Yii2 framework). I read a bunch of documentation, as examples:
this , this , this , this , and much more.
Some of this even implemented. It even works =) But if everything was fine, I would not contact you =))

Multilingual links look like this:
https://site.com/contact
https://site.com/en/contact
https:// site.com/uk/contact

Now in the project I do not use helpers and generate a link like this:

<a class="nav-link" href="/cat/<?=$item->adjusted_name?>"><?=$item->name?></a>


Accordingly, when I click on the link, the selected language "flies" to me. With helper usage
Html::a .....

Nothing changes. Actually the question itself: How to correctly generate a URL for following links while maintaining the selected language?

In frontend/config/main.php I have a module connected and other settings:
'modules' => [
        'languages' => [
            'class' => 'common\modules\languages\Module',
            //Языки используемые в приложении
            'languages' => [
                'English' => 'en',
                'Русский' => 'ru',
                'Українська' => 'uk',
                'Қазақ' => 'kz',
            ],
            'default_language' => 'ru', //основной язык (по-умолчанию)
            'show_default' => false, //true - показывать в URL основной язык, false - нет
        ],
    ],
......
        'i18n' => [
            'translations' => [
                'app' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    //'forceTranslation' => true,
                    'basePath' => '@common/messages',
                ],
            ],
        ],
......
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'class' => 'common\components\UrlManager',
            'rules' => [
                'languages' => 'languages/default/index', //для модуля мультиязычности
......

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2020-06-30
@santaTihonov

To paraphrase Dmitry, he suggests modifying the output of the link using a helper, something like this:

<?= Html::a(
  Yii::t('app',$item->name),
  ['/cat/' . $item->adjusted_name]
) ?>

But I think this will not solve the issue of "rewriting" links that were made without helpers. Perhaps Dmitry will correct me =)

A
Arman Hovhannisyan, 2020-06-29
@Armrisch

Enter PHP-UP Yii2 internationalization in YouTube,
see the lesson I think it will help))
PS You can not use the get parameter in the link at all, just use ajax and data type attributes

<button type='button' data-lang ='en' >English</button>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question