Reputation: 2272
I want to use the laravel language data at the frontend part in the browser.
So I can use sprintf in js to show the same translation in frontend part.
I wish it could be an big javascript object. It would be very easy and nice to use.
Thanks in advance.
Upvotes: 1
Views: 1437
Reputation: 163898
I guess easiest way to do that is to use cookies. Set coockie with Laravel:
$cookie = Cookie::make($name, $value);
Then read and parse it with JS:
var x = document.cookie;
Upvotes: 0
Reputation: 3288
This is a situation I've dealt with myself and the answer is a bit awkward.
You can use Lang::get('file')
to output the entire file as an array, which you can use with json_encode
to do exactly what you want. But, as far as I know, you cannot get all language files. This probably has to do with the fact that Laravel cannot scan the directory for all file names without consuming a lot of disk resources, and does not do so normally. Language is handled with lazy loading in all applicable cases.
But! What you can do, is put all your JavaScript-related language in one file. I called mine widgets.php
, and then do json_encode( Lang::get('widget') )
to get your JavaScript relevant language in one object.
Or, if you really do need all of it, you can build it manually.
$lang = ['auth', 'validation', 'etc'];
for ($files as $file)
{
$lang[$file] = Lang::get($file);
}
return json_encode($lang);
Upvotes: 3