简体   繁体   中英

Laravel 5.4 translation string doesn't work

I'm new to laravel and I tried to use the translation string to do the localization work.

I have done the setlocale work and tested by using App::getLocale() which returned 'zh-cn'. (I have added a middleware which set the locale to it and added it to the kernel.php)

And I created the resources/lang/zh-cn.json and simply added {'a':'b'}

then I post an request from frontend to an controller which should return like

return response()->json([
    'status' => __('a')
]);

however I still get an response as {'status':'a'}. I don't know where the problem is, or if I misunderstood the way translation string works?

In JSON it is standard to use double quotes. If you change the single quotes to double quotes it will return the correct value of {"status": "b"} .

So your zh-cn.json file should look like this:

{
    "a": "b"
}    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM