最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I get laravel's locale in javascript? - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a comment  | 

3 Answers 3

Reset to default 13

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>

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);
发布评论

评论列表(0)

  1. 暂无评论