2016-11-14 118 views
0

我正在嘗試使用簡單文本更改laravel中的語言選擇器,我在語言文件夾中有兩個php文件greetings,一個用英文,一個用德文。函數trans在Laravel中不起作用

此代碼是在de文件夾:

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

,這是在en文件夾

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

,當我嘗試使用功能的反式顯示在視圖中字它給了我代碼來自視圖,而不是單詞。

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

任何想法?

回答

0

您可以通過創建中間件組來管理此操作。

//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 
]; 
0

你應該改變你的配置區域設置這樣的/ app.php:

'區域'=> '德',