0

this is my lang messages

'Next' => 'Keyingisi',

this is my code which is I used in blade.php , it is worked

{{__('main_trans.Next')}}

but I want to write this code inside js file like this

sNext: "<?php __('main_trans.Next') ?>",

But it is not working , can you give me advise.

0

2 Answers 2

3

You can't; <?php ?> and {{ }} cannot be used in a .js file. If this is a <script> within a .blade.php file, then you can.

Or, you can assign it as a variable before importing the JS file, like so:

file.blade.php:

<script>var translation = "{{ __('main_trans.Next') }}";</script>
<script src="{{ asset('path/to/file.js') }}"/>

Then, in your .js file, you access it via the JS variable sNext: translation

Sign up to request clarification or add additional context in comments.

3 Comments

I'm not clear with your solution...could you explain how to assign the variable before importing the js file
Hi, about the previous comment, i read this thewebtier.com/implementing-laravel-localization-in-javascript and now it became clear... you can access the variable using window.translation
This is a bit of an older answer, and in modern JS, this would be const translation = "{{ __('main_trans.Next') }}";, but that is assignment. In file.js, translation would be available as a variable, or, as you stated, window.translation, and you'd be able to do { sNext: translation } (in the context of the question being asked)
1

You can do something like this:

sNext: "{{ __('main_trans.Next') }}"

OR

sNext: "@lang('main_trans.Next')"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.