dongmeiwei0226 2016-08-29 03:17
浏览 29
已采纳

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

I have been working in a localization project ,in my app the language translation files are not loading in some pages.I don't know what wrong with my loadPath function. In my app user can change there language on profile section and the changes works perfectly on the same session.But When the user logout and login to the app at first the user not seen the his/her preferred language.

Here my code

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();
}

How can I solve this?

  • 写回答

1条回答 默认 最新

  • dongyanggan3025 2016-08-29 04:57
    关注

    I found some unwanted language files loading on my function and I remove them and I do load files on demand only.Now its working perfectly.Here is my working code

    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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算