Zbyszek Kisły
Zbyszek Kisły

Reputation: 2238

How can I get laravel's locale in javascript?

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

Upvotes: 5

Views: 9149

Answers (3)

Dato Sarishvili
Dato Sarishvili

Reputation: 185

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>

Upvotes: 13

Vipul
Vipul

Reputation: 941

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);

Upvotes: 3

user10473086
user10473086

Reputation:

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

Upvotes: 3

Related Questions