As for Yii2 to do the translation of the website using the cache (cookies)?

Background: there is a website
It changes the language of the site, replacing the variable. That is, when you select another language, is a POST request, which inserts a variable to a different value that is stored in cookies ;) [cookies] on the day, then the language flies.
On the same principle I want to change the language in Yii2 advanced
in $cache = \Yii::app()->cache; I'll keep a cache of the language, but I would like to stay for at least a year.
How to store cache in pure php I can and know with Yii cache yet hostility and misunderstanding.
As I see it this way:
'language' => [
 'class' => 'app\module\languages\Lang',
 'languages' => [ //Languages used in the application
 'English' => 'en',
 'Russian' => 'EN',
 ], // or Lang::getArrayLang()
 'default_language' => 'EN', //primary language (default)

Alternative to the extensibility functionality:
public function getArrayLang()
 return [ //the Languages used in the application
 'English' => 'en',
 'Russian' => 'EN',
 ]; // if needed you can push the array from the DB, but while this need not

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

Something like that would look like the class itself:
namespace \tram\pam\pam;
use Yii;

class Lang extends \yii\web\UrlManager 
 static $lang; //string of the form EN|en|
 public $default_language
 public $languages

 public function langManager()
 $params = Yii::$app->request->post();
 $cache = \Yii::app()->cache;
 if (empty($params['lang'])) {
 //the current application language
 $curentLang = Yii::$app->language->default_language;
 } else {
 $curentLang = $cache->get('lang')
 return $curentLang;


That is, I need to <?=Lang::langManager();?>
For <?=Yii::$app->language;?>, but only for the frontend

Question: In the right direction I'm going? How to separate language for backend separately from the frontend?
What to add to the cache on the language lasted a year, or about a year? Kept anyone language via a cache without having to change URLs?
Basically I see about a multilingual website that they set up in the link the right address, creating a separate page.
I don't need a separate page using the render and I can get any page or piece of page, depending on the value of the current language.

The point is that using a POST request to send to the cache "EN" or "en"
I actually wanted to do it through:
<?=Html::a('EN', '/siteen', ['data-method' => 'post']);?>
<?=Html::a('EN', '/siteru', ['data-method' => 'post']);?>

But it may mean reference to each controller in behavior - which is not quite adequate.
In General, any thoughts, tips, thoughts on this?
March 12th 20 at 08:02
3 answers
March 12th 20 at 08:04
You cache is not dependent on the user, it is essentially a shared variable and as a consequence Bob will change his post to the language for everyone else.
So the idea is so-so. And what is the history parameter in the url is not satisfied, it is comfortable, familiar, good for seo and is out of the box. What is the meaning of Your thing?
Alternatively, you can in the session store, but there are other pitfalls.
@Brady , Hello. Each user of the website should be in the language which he has chosen, no matter what language is selected by another user. As in the example above, in pure php is possible, I checked each put their own language, and it is not as affected by the choices of other users. Once this is possible in pure PHP framework that means it is maybe my considerations is only override variable via the cache by means of a function that will return the value EN or en for Yii::$app->language - Gunnar_Wiegand64 commented on March 12th 20 at 08:07
@Gunnar_Wiegand64, what does the cache to the language?

I have not read, "many bukaf", but did not think that you can just cookies to record for a longer period?

In any case, whether there is language in the URL or not, will still be stored in cookies.

By the way, if nothing is broken, it is out of the box and so selects the language based on the preferences from the user's browser.

Well, the last. The meaning of the advanced template that is placed on different domains (subdomain), so it's not in any way associated languages for parts. - Missouri commented on March 12th 20 at 08:10
Each user of the website should be in the language which he has chosen, no matter what language is selected by another user.

That's why first we must decide how to define user? As You know the cache can not, so either cookies or localStorage or authorization or something. And only then already to think about where to store the selected language and the like. - Brady commented on March 12th 20 at 08:13
@Missouri, can you show or tell?
'i18n' => [
 'translations' => [
 'app' => [
 'class' => 'yii\i18n\PhpMessageSource',
 //'forceTranslation' => true,
 'basePath' => '@common/messages',
I will help to get the current language from the cache?
<?=setcookie('cookie', 'lang', strtotime('+365 days'), '/'); if I am not mistaken, cookies year are stored right?>

lang - will keep EN or en - Gunnar_Wiegand64 commented on March 12th 20 at 08:16
I see You are a dangerous man. You are right in the provision of write to the cache...

\Yii::$app->language contains the language of the user. And that's all, nothing more is needed.

The idea is that if we write a different value, he will do everything himself, now with the phone, so there is no possibility to look in the code, but the idea is so. It's PHP, you should not hesitate to go into the framework code to understand what and how it works. - Missouri commented on March 12th 20 at 08:19
@Missouri, you misunderstood, no, not in the presentation I showed how the cookies are stored year year. in the specific example, cookies are stored for the main directory (root), the installation of cookies will be made in the function in the model.
I'm not afraid to go into the code, I was just wondering, maybe someone has already implemented this using Yii2, and that everywhere through the URLs.
Just listen to the nonsense about SEO, blochat mainly because the sites that act as Garvey or grey with redirecting and other stuff.
And do not say that,
It is one and the same page. The path and main, but there is a redirect. Why other sites are on the same page to display text in different languages. Simfony and Lavarel have no problem with the cookies, and in the matter of an example of a site led that it is real and perhaps only reason for developers Yii is beyond fiction. Now to write the crutches or the bad code, not knowing it will hurt or so will do ))
Well, that sarcasm is always a way out. Another thing, if lazy, or don't want to understand. I have many times argued about Yii - it is impossible, impossible, so why do I then get?
Just a bunch of undercooked, am I a genius or what??? I'm even a programmer don't think.
And the funny thing is that I can write the functionality of the language changing without the url based on cookies, while they will write that it's not real )))
I apologize for the emotions. )))) - Gunnar_Wiegand64 commented on March 12th 20 at 08:22
March 12th 20 at 08:06
Able to cookies, in autopedigree Asia, with a prefix in the url or without.
@Jeffry.Jenkins, well, studying, but there are doubts, it is that the description of this solution. - Gunnar_Wiegand64 commented on March 12th 20 at 08:09
March 12th 20 at 08:08
The solution was found!
It is necessary to create the model, and it is the function of inheriting from the form class, override the variable responsible for the output language.
Of course it is necessary to declare two variables, one is the default, if no language is selected, and the second for storing cookies!
Unfortunately, I have not yet figured out how to separate translate backend and frontend, but I think soon it will do so as the documentation for multilingual already had an example with URLs, and then left to finish the conditions from which it is discharged representation of such a language.
If we can determine the controller Yii::app()->controller->id - so will be able to determine the origin of the view, because the backend and the frontend controllers are different.
If you do not understand, I will give instructions individually, to write to my email.

Find more questions by tags CachingYii