dongpo8702 2016-04-22 23:53
浏览 135
已采纳

尝试在Resources / lang / en中放置文件夹时出错:Laravel 5.2

I have below code in my controller.

public function AllCountries() {
    $Countries = (new \App\DataAccess\CountryData())->GetAllCountries();
    app()->setLocale('fr');
    return view('Country.List')->with('Countries', $Countries->getData()->CountryList);
}

Definition of method to fetch data is below.

public function GetAllCountries() {
    return response()->json(['CountryList' => \App\Models\CountryModel::all()]);
}

Below is the screenshot which shows Translation array

enter image description here

Below is the screenshot that shows the View called by Controller has just one word to show translation only.

enter image description here

When I run the application, I get following error.

Use of undefined constant CountriesList - assumed 'CountriesList' (View: C:\xampp\htdocs\MyAccountesources\views\Country\List.blade.php)

Normally when we see websites which is not a default language set in our browser. We see a notification to translate the page on Page load. See the screenshot below.

enter image description here

But this does not come on my side. Although I can see French language but Translation notification is not coming. Why?

  • 写回答

2条回答 默认 最新

  • doufu9521 2016-04-25 00:47
    关注

    Read here for more about how localization works in Laravel:

    https://laravel.com/docs/5.2/localization#introduction

    If you really want to override the entire template, you can do this yourself:

    $app->getLocale()
    App::getLocale()
    

    (but, I'm not suggesting this is usually a good idea - usually translating strings is a better idea.)

    Those functions return the locale, so you can use this to fetch the appropriate view (e.g. placing all jp views inside a jp/ directory.)

    You can use a ViewFactory's exists() method to check if it exists, and implement fallback logic as you like. It might make a lot of sense to implement this on your own controller base class (which would inherit from the framework one), and have all your controllers subclass this new base class. This way the logic can be shared.

    Something roughly like this, for example:

    namespace App\Http\Controllers;
    
    use Illuminate\Foundation\Bus\DispatchesJobs;
    use Illuminate\Routing\Controller as BaseController;
    use Illuminate\Foundation\Validation\ValidatesRequests;
    use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
    use Illuminate\Foundation\Auth\Access\AuthorizesResources;
    
    class Controller extends BaseController
    {
        use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;
    
        protected function localeViewResponse($view, $data = [], $mergeData = [])
        {
            $factory = view();
            $locale = app()->getLocale();
    
            if ($view->exists($locale . '.' . $view)) {
                return $view->make($locale . '.' . $view, $data, $mergeData);
            }
    
            return $view->make($view, $data, $mergeData);
        }
    }
    

    That being said, again, you should really consider if you have to do this. Angad's method will make sense to other people familiar with Laravel, and is usually the better approach. If you're having problems with 'undefined constant', make sure you're quoting things correctly and try updating your question with the code you've attempted.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址