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
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
Share Improve this question asked Oct 8, 2018 at 12:39 Zbyszek KisłyZbyszek Kisły 2,2386 gold badges29 silver badges53 bronze badges3 Answers
Reset to default 13If 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>
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
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);