Laravel 5 配置语言环境,不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29524297/
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 11:19:28  来源:igfitidea点击:

Laravel 5 config locale, does not works

phpvalidationlaravellocalelaravel-5

提问by felipe_dmz

Modifications done:

所做的修改:

on config/app.php

config/app.php

'locale' => env('APP_LOCALE', 'en'),
'fallback_locale' => 'en',

on .env

.env 上

APP_LOCALE=pt

I've also duplicated the /resources/lang/enfiles to /resources/lang/ptfiles as docs suggests.

我还按照文档的建议/resources/lang/en文件复制到/resources/lang/pt文件。

The problem:

问题:

All my validation messages are still in english, and running php artisan tinker:

我所有的验证消息仍然是英文的,并且正在运行php artisan tinker

>>> Lang::getLocale()
=> "en"

>>> env('APP_LOCALE')
=> "pt"

Help?

帮助?

回答by felipe_dmz

I've already figured out how to solve that:

我已经想出了如何解决这个问题:

<?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(env('APP_LOCALE'));
        return $next($request);
    }

}

On Kernel.php

Kernel.php 上

protected $middleware = [
    .
    .
    .

    'App\Http\Middleware\Locale'
];

UPDATE:

更新:

My config was cached by command:

我的配置是通过命令缓存的:

php artisan config:cache

So, I've done:

所以,我已经完成了:

php artisan config:clear

And the middleware above isn't necessary anymore.

并且不再需要上面的中间件。

回答by Maulik Shah

    php artisan config:clear

    php artisan cache:clear 

    php artisan config:cache

If you are using the config:cache command during deployment, you must make sure that you are only calling the env function from within your configuration files, and not from anywhere else in your application.

如果在部署期间使用 config:cache 命令,则必须确保仅从配置文件中调用 env 函数,而不是从应用程序的其他任何位置调用。

If you are calling env from within your application, it is strongly recommended you add proper configuration values to your configuration files and call env from that location instead, allowing you to convert your env calls to config calls.

如果您从应用程序中调用 env,强烈建议您向配置文件添加适当的配置值,然后从该位置调用 env,从而允许您将 env 调用转换为配置调用。

Read whole thread over https://github.com/laravel/framework/issues/21727

通过https://github.com/laravel/framework/issues/21727阅读整个线程