Laravel 语言翻译未在第一条路线加载

Posted

技术标签:

【中文标题】Laravel 语言翻译未在第一条路线加载【英文标题】:Laravel Language Translation Not loading at first route 【发布时间】:2017-01-04 23:59:31 【问题描述】:

我一直在从事本地化项目,在我的应用程序中,某些页面中没有加载语言翻译文件。我不知道我的 loadPath 函数有什么问题。 在我的应用程序中,用户可以在个人资料部分更改语言,并且更改在同一个会话中完美运行。但是当用户首先注销并登录应用程序时,用户看不到他/她的首选语言。

这是我的代码

protected function loadPath($path, $locale, $group)


    if ( App::runningInConsole() ) 
        return parent::loadPath( $path, $locale, $group );
    

    $domain  =  get_subdomain();
    $dir  =  "lang/$locale/$domain";
    $key  =  $dir.'/'.$group.'.php';


    if(\Session::has($key))
        $results = \Session::get($key);
        $d = json_encode($results);

        view::share('lang',$d);
        return $results;

    else

        $this->s3 = App::make('aws')->factory(tenent_aws_config())->get('s3');

        $domain = get_subdomain();
        $bucket = "localbulkload";
        $dir = "lang/$locale/$domain";

        $langList = $this->s3->getIterator('ListObjects',[
            "Bucket"    => $bucket,
            'Prefix' => "lang/$locale/$domain"
        ]);

        foreach ($langList as $langObject)
            $object = $this->s3->getObject([
                "Bucket" => $bucket,
                "Key" => $langObject['Key']
            ]);
            $key = $langObject['Key'];
            $string = ($object['Body']);
            $results = eval("?>$string");
            \Session::put($key,$results,60);
        

        $info = $this->s3->doesObjectExist(
            $bucket,
            $dir . "/" . $group . ".php");
        if ($info === false) 

            if($this->files->exists($full = "$path/template/$group.php")) 

                $results = $this->files->getRequire($full);
                $d = json_encode($results);
                view::share('lang',$d);
                return $results;
            
            else
                $this->files->exists($full = "$path/en/$group.php");
                $results = $this->files->getRequire($full);
                $d = json_encode($results);
                view::share('lang',$d);

                return $results;
            

        
    

    return array();

我该如何解决这个问题?

【问题讨论】:

【参考方案1】:

我发现在我的函数上加载了一些不需要的语言文件,我删除了它们,我只按需加载文件。现在它工作得很好。这是我的工作代码

protected function loadPath($path, $locale, $group)

    if (App::runningInConsole()) 
        return parent::loadPath($path, $locale, $group);
    


    $domain  =  get_subdomain();
    $dir  =  "lang/$locale/$domain";
    $key  =  $dir.'/'.$group.'.php';

    if(\Session::has($key))
        $results = \Session::get($key);
        $d = json_encode($results);
        view::share('lang',$d);
        return $results;

    else

        $this->s3 = App::make('aws')->factory(tenent_aws_config())->get('s3');

        $domain = get_subdomain();
        $bucket = "localbulkload";
        $dir = "lang/$locale/$domain";

        $info = $this->s3->doesObjectExist(
            $bucket,
            $dir . "/" . $group . ".php");

        if($info )
            $object = $this->s3->getObject([
                "Bucket" => $bucket,
                "Key" => $dir . "/" . $group . ".php"
            ]);
            $key = $object['Key'];
            $string = ($object['Body']);
            $results = eval("?>$string");

            \Session::put($key,$string);
            $d = json_encode($results);
            view::share('lang',$d);
            return $results;
        
        else

            if($this->files->exists($full = "$path/template/$group.php")) 

                $results = $this->files->getRequire($full);
                $d = json_encode($results);
                view::share('lang',$d);
                return $results;
            
            else
                $this->files->exists($full = "$path/en/$group.php");
                $results = $this->files->getRequire($full);
                $d = json_encode($results);
                view::share('lang',$d);
                return $results;
            

        
    

    return array();

【讨论】:

以上是关于Laravel 语言翻译未在第一条路线加载的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 5.2 - 会话不会在路由更改中持续存在

Laravel 路由问题:自动重定向到根文件夹

Laravel 多语言站点和路线

根据当前翻译的 Laravel 路线在刀片中添加“活动”类

Laravel 4如何检查一条路线是不是仅来自另一条路线/重定向?

在我的 laravel 4 和 angularjs 应用程序中从一条路线重定向到另一条路线