函数trans在Laravel

时间:2016-11-14 09:40:37

标签: php laravel laravel-5 localization

我正在尝试通过简单的文本更改在laravel中进行语言选择,我在语言文件夹中有两个php文件greetings,一个用英语,一个用德语。

此代码位于de文件夹:

return array(
  'hello' => 'Hallo'   
);

,这是在en文件夹

return array(
  'hello' => 'Hello'   
);

当我尝试使用函数trans在视图中显示单词时,它会从视图中提供代码,而不是单词。

{{ trans('greetings.hello') }}

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您可以通过创建中间件组来管理它。

//middleware
use Closure, Session;

class ManageLocalization {

protected $languages = ['en','de'];

public function handle($request, Closure $next)
{
    if(!Session::has('userLang'))
    {
        Session::put('userLang', $request->getPreferredLanguage($this->languages));
    }
    app()->setLocale(Session::get('userLang'));

    return $next($request);
}

}

将此添加到 kernel.php

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\ManageLocalization::class
    ],

protected $routeMiddleware = [
    'userLang' => \App\Http\Middleware\ManageLocalization::class
];

答案 1 :(得分:1)

您应该在config / app.php中更改您的语言环境,如下所示:

'locale'=> '去',

相关问题