Laravel translations for javascript

I’m writing a multilingual site using Laravel. I have a few messages that are generated by Javascript in the front end which I would also like to be multilingual.

I’d definitely like the translations for this front end stuff to come from the same place as the bulk of the translations in the back end - ie in the app/lang/{language} files.

I can immediately see a couple of options for getting translated messages from the back end into front end javascript, and I can imagine there are other options.

Rather than including the JS files the normal way with a

{{ HTML::script(‘assets/js/subscribe.js’, array(“type” => “text/javascript”)) }}

Parse them into the page as a blade include file, and embed {{ trans(‘file.message’) }} into the javascript.

Use the backend to fetch the required message translations and store them as hidden form fields in the page, or attach as data to some DOM object, then in the JS grab the message from there rather than coded into the JS itself.

(1) definitely works fine, I just tried that, but before I roll this out to all, I thought I should ask, which of those is recommended, or is there another better option I haven’t thought of yet?

Now I’ve started playing with this, another obvious option occurs.

  1. Keep the js as pure js files, but have them reference global js variables for the messages, which are written out by minimal blade templates.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.