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

尝试在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 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。