donglinxin8765 2018-09-01 14:14
浏览 38
已采纳

Yii 2:更改语言仅在刷新后工作

I'm using the Yii 2 framework to build a custom app. My website is available both in english and french. Strings are being translated just fine, but I'm running into an issue when I wish to actually change the language from the UI.

Basically my website has two subdomains, one is en.domain.com and the other being fr.domain.com. If the user access one of those domains for the first time, my code detects the subdomain, sets the right language into Yii and creates a cookie to store the language code. If the user already had a cookie and access any of those subdomains, the code will use the cookie to set the language. If the user clicks on "FR" or "EN" to change the language, a JS script deletes the cookie and redirects to the other subdomain (basically the same flow of the first time he's on the website).

Now, the issue is that when the user tries to access the french (translated) version of the website for the first time, it doesn't work. The code is executed, the cookie is set (I can see it), but nothing is translated. However, when I refresh the page, it works just fine and everything is translated. Then if I switch between one and the other, it works well. It's always the first time (which is problematic, because when Google and users ask for the french version, it's still in english until they reload).

Here's a code snippet of my BaseController (all my controllers extends from this one, so this code is executed everytime the page loads):

<?php
namespace app\controllers;

use Yii;
use yii\web\Controller;

/**
 * Class BaseController
 * @package app\components
 */
class BaseController extends Controller
{
    public function init() {
        parent::init();

        // If no cookie
        if (!isset($_COOKIE["language"])) {

            $parsedUrl = parse_url(Yii::$app->HelperComponent->getUrl());
            $host = explode('.', $parsedUrl['host']);
            $subdomain = $host[0];

            if ($subdomain == 'fr')
                $code = "fr-CA";
            else
                $code = "en-US";
        }
        else {
            $code = $_COOKIE["language"];
        }

        Yii::$app->language = $code;
        setcookie("language", $code, time() + 31556926, '/');
    }


}

Any idea what could cause this strange behavior? Thanks

  • 写回答

1条回答 默认 最新

  • douxie4583 2018-09-01 15:49
    关注

    The cookie will be created in browser after receiving first response from server. So it will never work on first request - technically not possible. It will work since second request assuming that user has enabled cookies. Please note that browsers may have cookies disabled - so your solution would not work.

    Better solution would be:

    1. store current language into server session (avoid using cookies completely)
    2. evaluate current lag setting very early so the application will properly translate strings while processing the response (e.g. in Controller init, or on beforeAction)
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂