5

I'm creating an app in four languages and every url goes like this:

app.name/en/rest/of/the/url
app.name/de/rest/of/the/url
app.name/nl/rest/of/the/url
app.name/fr/rest/of/the/url

How can I get this in the javascript? I need to translate some strings, but I need the locale to know which lang to choose

Zbyszek Kisły
  • 2,110
  • 4
  • 26
  • 48

3 Answers3

12

If you dont want to use PHP inside script tags. You can do like this

layout blade.php

<!DOCTYPE html>
<html lang="{{ config('app.locale') }}">
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
</head>

<body>
Content of the document......

<script>
// Plain javascript
var locale = document.getElementsByTagName("html")[0].getAttribute("lang");
// jQuery
var localeJquery = $('html').attr('lang');
</script>
</body>
</html>
Dato Sarishvili
  • 175
  • 1
  • 9
3

find locate in laravel by facade

$locate = App::getLocale()

by helper function

config('app.locale')

by JavaScript to add

<script>
 var locate = {!! config('app.locale') !!};
</script>

for more information about locate see

3

In JavaScript just add

<script>
 var locale = '{{ config('app.locale') }}';
</script>

and you can used "locale" variable in JS file.

or you can check by

console.log(locale);
Vipul
  • 896
  • 7
  • 14