It seems to me that you want to process a JS file, such as a JSP file, and allow its contents using the spring: tag. I would not do that.
Typically, the JS i18n runs in one of two ways:
- Writing an Array of Translated Strings from a JSP Page
- By creating a translation filter and providing a previously translated JS file to the requesting client
Both work best if you create one central location for your client-side translation strings.
In your context, I would recommend the first method (much simpler). That is, if your project is quite large and you have many translatable strings on the client side. Thus, the modification will look like this:
<script type="text/javascript"> var strings = new Array(); strings['settings.toogle'] = "<spring:message code='proj.settings.toggle' javaScriptEscape='true' />"; strings['settings.toogle.description'] = "<spring:message code='proj.settings.toggle.description' javaScriptEscape='true' />"; </script> <spring:theme code="jsFile" var="js" /> <script type="text/javascript" src="${js}" />
And in your JS file:
buildList('settings', [{ name: strings['settings.toggle'], id:"setting1", description: strings['settings.toggle.description'], installed: true }]);
Keep in mind that I used double quotes to write translated strings. This is due to some words in French or Italian that may contain an apostrophe.
Edit: auxiliary input
We provide translations to JS files for this reason. Usually the reason is because we want to dynamically create part of the user interface. There are also cases where we need to localize some third-party component, my answer above concerns them pretty well.
For cases where we want to dynamically create user interface elements, it actually makes sense to use templates, rather than concatenating HTML tags in JavaScript. I decided to write this because it makes a much cleaner (and possibly reusable) solution.
Therefore, instead of transferring translations to JavaScript, you can create a template and put it on the page (my example will use Handlebars.js , but I believe that you can use any other engine):
<script id="article" type="text/x-handlebars-template"> <div class="head"> <p> <span> <spring:message code="article.subject.header" text="Subject: " /> </span>{{subject}}</p> </div> <div class="body"> {{{body}}} </div> </script>
On the client side (i.e. in JavaScript), all you have to do is access the template (the example below explicitly uses jQuery) and compile:
var template = Handlebars.compile($("#article").html()); var html = template({subject: "It is really clean", body: "<p>Don't you agree?</p><p>It looks much better than usual spaghetti with JavaScript variables.</p>" }); $("#someDOMReference").html(html);
A few things to note here:
- Tags
<spring:message /> by default excludes both HTML and JS, we do not need to specify the javaScriptEscape attribute- It makes sense to provide the
text attribute for the <spring:message /> , since it can be used as a recession (if there is no translation for this language), as well as a comment (which means this element). You can even create a tool to scan files for <spring:message /> tags and automatically create property files - To prevent Handlebars from escaping HTML content, I used triple
{{{curly braces}}}
This is basically it. I recommend using templates if possible.