简体   繁体   English

如何使用 Laravel 5.5 在 jQuery 中使用 @lang?

[英]How do I use @lang within jQuery using Laravel 5.5?

I am trying to make a dynamic input field that gives a warning when a value no longer matches the value stored in the DB.我正在尝试制作一个动态输入字段,当值不再与存储在数据库中的值匹配时发出警告。 I want to use Laravel's Localization @lang('messages.error') or {{ __('messages.error') }} to make this message available for the selected languages for my app.我想使用 Laravel 的本地化@lang('messages.error'){{ __('messages.error') }}使此消息可用于我的应用程序的选定语言。

My question is simple, how do you use this within jQuery?我的问题很简单,你如何在 jQuery 中使用它? I've found the answer to be hard for my current level of experience, so I wonder if anyone here knows the answer.我发现就我目前的经验水平而言,答案很难,所以我想知道这里是否有人知道答案。

Partial code:部分代码:

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<title></title>

<form action="" method="post">

    <div class="ibox float-e-margins">

        <div class="ibox-title">
            <h4>@lang('form.kvk')</h4>
        </div>

        <div class="ibox-content">
            <div class="form-group{{ $errors->has('kvk') ? ' has-error' : '' }}">
                <label for="kvk">@lang('form.kvk')<span class="redFont">*</span></label>
                <input type="text" class="form-control" name="kvk" id="kvk" value="{{ ( $errors->has('kvk') ? old('kvk') : ( $debtor != NULL ? $debtor->kvk : old('kvk') ) ) }}">
            </div>
        </div>

        {{-- Remains empty if no error --}}
        <div id="noMatchKvk"></div>

    </div>

</form>

<script>

/* added after answer
var kvk = '{{ __("add/customer/messages.noMatchKvk") }}';
*/

    $('#kvk').keyup().on('change', function() {
        // If the value is no longer equal to the DB
        if ( $('#kvk').val() != '{{ $debtor->kvk }}' ) {
            $('#noMatchKvk').html('<span class="help-block"><strong>' + kvk + '</strong></span>');
        }
        // If the value remains the same
        if ( $('#kvk').val() == '{{ $debtor->kvk }}' ) {
            $('#noMatchKvk').html('');
        }
    });

</script>

PS.附注。 I have searched around, but could not find the answer.我四处寻找,但找不到答案。 However, I might be asking the wrong question.但是,我可能问错了问题。

Thank you in advance!先感谢您! :) :)

EDIT + Solution编辑 + 解决方案

I failed to mention that the messages were in another folder within the en directory .我没有提到消息位于en目录中的另一个文件夹中 The reason it failed was because I kept mentioning the route with dots (.), when I should've done slashes (/).它失败的原因是因为我一直用点 (.) 提到路线,而我应该做斜线 (/)。 I have edited my question.我已经编辑了我的问题。

Guess this was a rookie mistake, thank you to everyone who replied!猜猜这是菜鸟的错误,谢谢大家的回复! And thank you @Theodoros Alexopoulos for your answer谢谢@Theodoros Alexopoulos 的回答

<script type="text/javascript">

    var something = @json( __('file.variable') );

</script>

The script should be inside your blade脚本应该在你的刀片里面

Then use the variable as you like然后根据需要使用变量

In laravel 8, You can use @lang without any bracket like this在laravel 8中,您可以使用@lang而无需像这样的任何括号

<script type="text/javascript">

    var message = "@lang('file.variable')";

</script>

Use this inside scripts tags NOT inside .js file then you can you the variable inside the script tags or in .js在脚本标签中使用它而不是在 .js 文件中,然后你可以在脚本标签或 .js 中使用变量

 <script>
 var v1 = '{{ @lang('file.value') }}';

$('#kvk').keyup().on('change', function() {
    // If the value is no longer equal to the DB
    if ( $('#kvk').val() != '{{ $debtor->kvk }}' ) {
        $('#noMatchKvk').html('<span class="help-block"><strong>' + v1 + '</strong></span>');
    }
    // If the value remains the same
    if ( $('#kvk').val() == '{{ $debtor->kvk }}' ) {
        $('#noMatchKvk').html('');
    }
});

I think you have to keep the @lang , or {{ }} call inside the JS single quotes:我认为您必须在 JS 单引号内保留@lang{{ }}调用:

$('#noMatchKvk').html('<span class="help-block"><strong>{{ __("messages.noMatchKvk") }}</strong></span>');

That, or you can set the value of {{ __("messages.noMatchKvk") }} to a variable and use it directly in your code:那,或者您可以将{{ __("messages.noMatchKvk") }}设置为变量并直接在您的代码中使用它:

var message = "{{ __('messages.noMatchKvK') }}";

$('#noMatchKvk').html('<span class="help-block"><strong>' + message + '</strong></span>');

The second method makes it a little more readable.第二种方法使它更具可读性。 Also, make sure this <script> is within a .blade.php file (which it looks like it is, but just something to be aware of; you can't use blade directives like @ or {{ }} in a straight .js file)另外,请确保此<script>位于.blade.php文件中(它看起来像这样,但需要注意一些事项;您不能直接在.blade.php文件中使用诸如@{{ }}类的刀片指令.js文件)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM