How to translate strings from jquery to blade on laravel

Viewed 1329

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?

1 Answers

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);
});
Related