为什么 Laravel 的 App::getLocale() 方法不一致?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38229369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:06:28  来源:igfitidea点击:

Why is Laravel's App::getLocale() method inconsistent?

phplaravel

提问by lesssugar

I'm using 2 languages in my Laravel 5.2 app. There is a simple password-reminder page I'm implementing currently, and for reasons unknown to me I have problems in sending the new-password email in the correct language.

我在 Laravel 5.2 应用程序中使用了 2 种语言。我目前正在实施一个简单的密码提醒页面,由于我不知道的原因,我在以正确的语言发送新密码电子邮件时遇到了问题。

Let's say I see the page in German. In the view of the page, I echo 2 values, using Facades:

假设我看到德语页面。在页面视图中,我使用 Facades 回显 2 个值:

echo App::getLocale();
echo Session::get('locale');

The page is served in German, so bothvalues echo de.

该页面以德语提供,因此两个值都 echo de

Now, I enter an email address into the form and submit it. The input gets to a controller method and calls a library to send a new password to the user:

现在,我在表单中输入一个电子邮件地址并提交。输入到达控制器方法并调用库以向用户发送新密码:

public function resetPassword() {
    // Validate the input, retrieve the user...    

    Mailer::sendNewPasswordEmail($user); // Call to the library sending emails
}

Finally, in the library, I var_dump the same 2 values, like this:

最后,在库中,我 var_dump 相同的 2 个值,如下所示:

public static function sendNewPasswordEmail($user) {
    var_dump(App::getLocale());
    var_dump(Session::get('locale'));
    die;
}

In this case, Session::get('locale')still equals de, but App::getLocale()shows en.

在这种情况下,Session::get('locale')仍然等于de,但App::getLocale()显示en

Why, why, why?

为什么,为什么,为什么?

In my email template, I'm using the Blade's @lang()directive. As far as I know, the directive checks the application locale to determine which translation to serve. In my case, the email is being sent always in English and I have no clue why App::getLocale()returns a different value in the view and during the next POST request I'm making.

在我的电子邮件模板中,我使用了 Blade 的@lang()指令。据我所知,该指令检查应用程序区域设置以确定要提供的翻译。就我而言,电子邮件始终以英文发送,我不知道为什么App::getLocale()在视图中以及在我发出的下一个 POST 请求期间返回不同的值。

This is not the first time this happens, btw. At times is seems that views "know" more about the actual application locale, than the controllers, models or libraries. Confusing.

这不是第一次发生这种情况,顺便说一句。有时,与控制器、模型或库相比,视图似乎“更了解”实际应用程序区域设置。令人困惑。

Ideas?

想法?

采纳答案by Franklin Rivero

Laravel 5.2 App_Locale is not persistent. the only way I've found to make locales work properlly is creating a middleware that calls App::setLocale() like this:

Laravel 5.2 App_Locale 不是持久化的。我发现使语言环境正常工作的唯一方法是创建一个像这样调用 App::setLocale() 的中间件:

<?php namespace App\Http\Middleware;

use Closure;
use Session;
use App;
use Config;

class Locale {

   /**
    * Handle an incoming request.
    *
    * @param  \Illuminate\Http\Request  $request
    * @param  \Closure  $next
    * @return mixed
    */
    public function handle($request, Closure $next)
    {
        App::setLocale(Session::get('locale'));
        return $next($request);
    }

}

Register your middleware on Kernel.php

Kernel.php上注册您的中间件

protected $middleware = [
    .
    .
    .

   'App\Http\Middleware\Locale'
];