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 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52