I
I
Igor Vasiliev2018-10-09 21:08:46
Yii
Igor Vasiliev, 2018-10-09 21:08:46

How to translate a site using cache (cookies) in Yii2?

Background: there is a site vaigmania.ru
It changes the language of the site, replacing the variable. That is, when another language is selected, a POST request occurs, which substitutes a different value for the variable, which is stored in cookies;) [cookies] for a day, then the language crashes.
By the same principle, I want to change the language to Yii2 advanced
in $cache = \Yii::app()->cache;I will store the language cache, but I would like it to last at least a year.
How to store cache in pure php - I can and I know, with Yii cache constructs we still have enmity and misunderstanding.
As I see it this way:

'language' => [
        'class' => 'app\modul\languages\Lang',
        'languages' => [        //Языки используемые в приложении
            'English' => 'en',
            'Русский' => 'ru',
        ],   //  или Lang::getArrayLang()
        'default_language' => 'ru', //основной язык (по-умолчанию)
    ],

--------------------
Alternative for functionality extensibility:
public function getArrayLang()
{
     return  [        //Языки используемые в приложении
            'English' => 'en',
            'Русский' => 'ru',
        ];  // если понадобится можно впихнуть массив из БД, но пока такой нужды нет
}

-------------------------------------------------- --------------------
That is, create a class that will track and process the event on click:
$items = Yii::$app->language->languages;
<?=Html::dropDownList('lang', 'null', $items);?>

The class itself will look something like this:
<?php 
...
namespace \tram\pam\pam;
...
use Yii;

class Lang extends \yii\web\UrlManager 
{
     static $lang; //строка вида ru|en|
     public $default_language
     public $languages

     public function langManager()
     {
             $params = Yii::$app->request->post();
             $cache = \Yii::app()->cache;
             if (empty($params['lang'])) {
            //текущий язык приложения
            $curentLang = Yii::$app->language->default_language;
           } else {
                  $cache->set('lang');
                  $curentLang = $cache->get('lang')
           }
           return $curentLang;
     }



}

That is, I need to receive For , but only for the frontend Question: Am I going in the right direction? How to get language separately for backend and separately from frontend? What to add so that the cache per language lasts a year, or about a year? Has anyone stored a language through a cache without resorting to changing urls? Basically, I see that the site is multilingual, that they substitute the desired address in the link, creating separate pages. I do not need separate pages, because through render I myself can get any page or page fragment, depending on the value of the current language. The point is to send a POST request to the cache "ru" or "en" I generally wanted to do it through:<?=Lang::langManager();?>
<?=Yii::$app->language;?>

<?=Html::a('EN', '/siteen', ['data-method' => 'post']);?>
<?=Html::a('RU', '/siteru', ['data-method' => 'post']);?>

But this may mean binding to each controller, in behavior - which is not entirely adequate.
In general, what thoughts, advice, considerations on this matter?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Timofeev, 2018-10-09
@webinar

Your cache does not depend on the user, that is, in fact, this is a common variable and, as a result, Vasya will change the language for everyone else with his post.
So the idea is so-so. And why the story with the parameter in the url does not suit you, it is convenient, familiar, good for seo and is out of the box. What is the meaning of your idea?
Alternatively, you can store in the session, but there are other pitfalls.

M
Mykola, 2018-10-12
@iSensetivity

https://github.com/codemix/yii2-localeurls
Able to use cookies, language auto-detection, with or without prefix in url.

I
Igor Vasiliev, 2018-11-04
@Isolution666

Solution found!
You need to create a model, and in it a function that inherits from the application class, redefine the variable responsible for the output of the language, and you're done!
Naturally, we need to declare two more variables, one will be the default value if the language is not selected, and the second for storing cookies!
Unfortunately, I have not yet figured out how to translate the backend and frontend separately, but I think I will do it soon, since the documentation on multilingualism already had an example with urls, so it remains to add the conditions from which the representation is displayed, such is the language.
If we can define a controller Yii::app()->controller->id, then we can determine where the view comes from, because the backend and frontend controllers are different.
If it is not clear, I will give instructions individually, write to my e-mail.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question