简体   繁体   English

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

[英]Laravel 5 config locale, does not works

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/en files to /resources/lang/pt files 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修补程序

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

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

Help? 救命?

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. 并且上面的中间件不再是必需的了。

    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调用转换为config调用。

Read whole thread over https://github.com/laravel/framework/issues/21727 通过https://github.com/laravel/framework/issues/21727阅读整个主题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM