1

**Possible Duplicate: The solution to this post is not feasible in this case, re-open this post please! **
JSF bean property not evaluated in external JavaScript file

The EL parser do not translate files including with the <outputScript> tag (JavaScript files). When trying to access the resource bundle.

<h:outputScript library="js" name="myScripts.js" target="head"/>;

myScripts.js file:

alert("#{msg.browser_not_html5_compatible}");

The result is that I get the same string back when the alert window renders.

If I have this line of JavaScript in a <script> tag in my XHTML file, the EL parser will replace it with the correct resource string from the bundle;

<script>
    alert("#{msg.browser_not_html5_compatible}");
</script>

Can I make the EL parser translate my included (<h:outputScript>) JavaScript file to access the resource bundle?

0

1 Answer 1

1

Servlet

One option could be creating a servlet which loads your message bundle and creates a Javascript file containing a JSON object containing the message keys and values. It might look like:

var messages = {
  "browser_not_html5_compatible": "You browser..."
, "other_key": "Other value"
};

If you have a large resource bundle and you don't want to expose all messages to Javascript you could prefix specific Javascript messages with something like js_. That way you can easily iterate over the resource bundle's keySet and only use the Javascript keys:

for (String key : resourceBundle.keySet()){
  if (key.startsWith("js_")){
    // Do something with resourceBundle.getString(key);
  }
}

On your JSF page you have to add the generated Javascript. If you need it, you can pass your view's locale to your servlet using #{view.locale}:

<h:outputScript library="js" name="/path-to-servlet/#{view.locale}" target="head"/>
<h:outputScript library="js" name="myScripts.js" target="head"/>

And in myScripts.js you can use:

alert(messages.browser_not_html5_compatible);

JSP

A variation on the servlet option is using JSP to create the Javascript containing JSON. You can by setting the content type to application/javascript:

<%@ page contentType="application/javascript; charset=UTF-8" %>

You need EL 2.2 to iterate over the resource bundle's keySet though (since there is no getter to access it).

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

5 Comments

Interesting solution, but don't know how this solution would hold when the application have multiple language support?
You can use #{view.locale} to pass the locale to your servlet as a get parameter.
I have a work-around so that the EL parser translate the JavaScript, which are in a separate file. The solution involves using the <ui:include> tag which the EL parses. But now I can not share that, since the busy people here at stackoverflow close that possibility. I can make a separate FAQ for the answer, if someone wants to see the solution.
What do you mean by "a separate FAQ for the answer"?
This post is closed, so can't post the solution here. Thanks for the effort.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.