Laravel trans helper函数没有选择正确的文件

时间:2015-06-18 09:16:32

标签: php laravel translation language-translation registrar

我正在Laravel建立一个网站, 我正在使用浏览器语言并相应地设置网站的语言。

问题是,Registrar.phpEnglish

中似乎没有翻译Dutch中的任何内容

有人可以帮帮我吗? 谢谢, G3

1 个答案:

答案 0 :(得分:2)

要完成这项工作,您需要使用中间件来设置区域设置:

LocaleMiddleware.php目录

中创建app/http/middleware
//LocaleMiddleware.php

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Session\Store as Session;
use Illuminate\Contracts\Auth\Guard as Auth;


class LocaleMiddleware {

    public function __construct(Session $session)
    {
        $this->session      = $session;
    }


  //Languages available in your resources/lang

   protected $languages = ['en','es', 'nl-be'];



   public function handle($request, Closure $next)
   {
       $langlist = $_SERVER['HTTP_ACCEPT_LANGUAGE'];

       // We just want the main language
        $lang = substr($langlist,0,2);

        if(isset($this->languages[$lang])){
           app()->setLocale($lang);
        }else{
           //You may log this here
        }

        return $next($request);
     }

 }

然后在app\httpe.kernel.php数组中$middleware注册中间件

/**
 * The application's global HTTP middleware stack.
 *
 * @var array
 */
protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,

     .........

    \App\Http\Middleware\LocaleMiddleware::class,
];

创建lang文件

您需要为要支持的所有语言创建单独的文件夹,并相应地翻译其中的文件,例如nl-be\validation.php

您可以首先将resources/lang/en to resources/lang/nl-be, then translate the contents of nl-be \ validation.php`的内容复制到荷兰语等值

enter image description here