Reputation: 2238
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
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
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
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