Answer the question
In order to leave comments, you need to log in
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', //основной язык (по-умолчанию)
],
public function getArrayLang()
{
return [ //Языки используемые в приложении
'English' => 'en',
'Русский' => 'ru',
]; // если понадобится можно впихнуть массив из БД, но пока такой нужды нет
}
$items = Yii::$app->language->languages;
<?=Html::dropDownList('lang', 'null', $items);?>
<?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;
}
}
=Lang::langManager();
=Yii::$app->language;<?=Html::a('EN', '/siteen', ['data-method' => 'post']);?>
<?=Html::a('RU', '/siteru', ['data-method' => 'post']);?>
Answer the question
In order to leave comments, you need to log in
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.
https://github.com/codemix/yii2-localeurls
Able to use cookies, language auto-detection, with or without prefix in url.
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 questionAsk a Question
731 491 924 answers to any question