3

I can fetch the locale app in blade like this {{ config('app.locale') }}, however, I want to be able to fetch the locale in javascript, specifically in Vue in order to pass it to moment().locale() I tried the following on the view home.blade.php it didn't work

moment().locale( {{ config('app.locale') }}

How can I fetch the locale app in Vue?

Mike
  • 111
  • 1
  • 10
  • What output do you get from the blade template? Are you missing quotes around the locale perhaps? – James Oct 24 '17 at 16:18
  • You're missing a `)` there. Also, where/how do you call that line? And I *think* it should be `@{{ config('app.locale'}}` – brombeer Oct 24 '17 at 16:26
  • with the current code, in blade it gets converted to `moment().locale( en )`. You need quote/double quote around **en**, so use `moment().locale( "{{ config('app.locale')" }})` – ljubadr Oct 24 '17 at 16:49

2 Answers2

4

You would have to set that value on the window. You could create a script tag in your blade template that creates global variables on the window that you could access.

You could also use https://packagist.org/packages/laracasts/utilities in controller functions that render your view to pass the values to the window with something like this:

JavaScript::put(['locale' => config('app.locale')]);

And then access it in your JS at window.locale

Mike
  • 111
  • 1
  • 10
webdevdani
  • 1,062
  • 7
  • 11
  • 2
    Great answer! To add to it, you can read about different methods of accessing server side variables with vue at https://zaengle.com/blog/layers-of-a-laravel-vue-application – vane Oct 24 '17 at 16:41
  • 1
    That's a great approach. I generally create meta tags `` and grab them with javascript when I need them but this is so much cleaner! – user1669496 Oct 24 '17 at 17:41
0
use Lang;

$locale = Lang::locale(); (gives you the locale detected on the frontend)
$locale = Lang::getLocale(); (gives you the locale set default in the config)
  • 1
    Although your code snippet might solve the issue, you should describe what’s the purpose of your code (how it solves the problem). Furthermore, you might want to check https://stackoverflow.com/help/how-to-answer – Ahmad F Mar 18 '18 at 10:00