2

I am using multiple languages in Laravel. I am getting the results of some data via Javascript files. My problem is that I need to convert the results from javascript to language. For this reason, I need to define language variables in javascript.

My language variables are like this;

{{ __('folder/folder.file') }}

sample javascript content

if (response == 0) {
    e.innerHTML = `You have received no orders in the last 90 days.`;
} else {
    e.innerHTML = `{{ __('folder/folder.file') }}`;
}

As you can see I didn't define the {{ __('folder/folder.file') }} language variables but it didn't work. How can I use it correctly here?

4
  • Assign {{ __('folder/folder.file') }} to a JS variable and just reference to it. Commented Sep 26, 2022 at 9:55
  • Are you using external js or internal js? Commented Sep 26, 2022 at 9:58
  • i am using external Js file. Commented Sep 26, 2022 at 10:09
  • Why not write the code internally? Commented Sep 26, 2022 at 10:10

3 Answers 3

2

If you want to use PHP variable in the script tag inside the blade template your code is working well. But for external JS:

Use PHP and Laravel methods in Javascript

Define your language variable:( above your js code )

<script>    
    var myVar = {!! __('folder/folder.file') !!};
</script>

or use @JSON blade directive for objects and arrays :

var myVar = @json($array);

Then use myVar in your JS scripts

for your example :

if (response == 0) {
    e.innerHTML = `You have received no orders in the last 90 days.`;
} else {
    e.innerHTML = myVar;
}

I hope it helps

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

3 Comments

I tried these but it didn't work. I am using external Js file.
@Ontrable Answer updated, please re-read and make sure declare the variable in your blade template (not external) and above your main js source.
I think the problem is the Js file. It doesn't read because I can't write it between the ` ` quotes. The js file content continues from KTCardsWidgetWeekly = { ..
0

You can't directly access PHP variables or functions in JavaScript, since when the Browser delivers contents of the requested URL to the client, it doesn't deliver PHP code but HTML (from your routes/web.php) or JSON (from your routes/api.php) depending on the case.

So, you need a way to access your translation variables some way or another (one of these options):

  1. Make resources/lang publically accessable and use JSON files, that can be requested via fetch()
  2. Make an endpoint in your routes/api.php to deliver your translations
  3. echo them into your JS using blade as @Royal_MGH suggested

Comments

0

I had a similar roadblock and decided to create a global "Languages" variable in my app.js file:

window.Languages = {};

// dispatch event listener for scripts that will access Languages variable. 
// languages:init is an example, as I've used alpine:init 
// or you can skip the dipsatch event and listen for DOMContentLoaded
document.dispatchEvent(new Event('languages:init'));

This way I can have multiple scripts add text to this array.

In my case I needed to access language data in my "resources/js/tiptap.js" file. I then added a script to push to my scripts stack within my blade component:

@push('scripts')
    <script>
        document.addEventListener('languages:init', () => {
            Languages['Page Break'] = '{{ __('Page Break') }}';
        });
    </script>
@endpush

Now in my "resources/js/tiptap.js" file, I just call upon the Languages variable (when it's ready to be accessed). In my case I accessed the Languages variable after the user added a page break, so I didn't need to listen for any events that told me the languages variable was ready:

Languages['Page Break']

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.