简体   繁体   中英

How to translate strings from jquery to blade on laravel

I have two separated files on my laravel, a index.blade.php and a.js file. On the blade I have a string, something like this:

<p id="currentMessage" class="bold-700"></p>

In my JS I have:

$(document).ready(function() {
  $('#currentMessage').text("@lang('hello'));
});

Seems like it doesn't translate on runtime and I can't inject a text and wait it to be translated, but there is any way to get the string translated on jquery before sending or something else to do the trick?

You can inject the translation from blade to javascript this way:

<script>
var translations = {
   hello: "@lang('hello')",
   goodbye: "@lang('goodbye')",
   ...
};
</script>

Then in your js file,

$(document).ready(function() {
  $('#currentMessage').text(translations.hello);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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