Passing localized javascript messages from resource packages using JSF

I created an application with JSF, and all messages issued by the server are localized using resource packages.

My question is: how to get messages issued in a client browser using javascipt localized with messages stored in resource packages?

Should I generate javascript dynamically, and if so, how can this be done?

For example, how can I localize the javascript alert message server in the following form validation method:

function valider() { typeActionRadio = document.getElementById("membres_editer_creer:typeActionAdr"); if (typeActionRadio.style.display == "block") { var boutonsRadio = document.forms["membres_editer_creer"]["membres_editer_creer:typeActionAdr"]; for ( var i = 0; i < boutonsRadio.length; i++) if (boutonsRadio.item(i).checked) return true; } alert ("Vous devez indiquer la raison du changement d'adresse (bouton radio à sélectionner)."); return false; } 
+4
source share
3 answers

Just let JSF print the desired JS code. For instance.

 <script> var message = '#{bundle[some.key]}'; </script> 

You only need to consider special JS characters, such as single quotes and newlines. To do this, you can register an EL custom function that delegates to Apache Commons Lang StringEscapeUtils or use OmniFaces of:escapeJS() .

+7
source

if you want all keys to use something similar in the main yout template etc.

 <script type="text/javascript"> var msg = new Object(); <c:forEach items="#{msg.keySet()}" var="key"> msg.#{key} = "#{msg[key]}"; </c:forEach> </script> 
+3
source

wutzebaer's answer is right, but it has a problem when then literal variables have any point, for example "person.name"

 <script type="text/javascript"> var msg = new Object(); <c:forEach items="#{msg.keySet()}" var="key"> try{ //msgTempl.#{key} = "#{msg[key]}"; msg['#{key}'] = "#{msg[key]}"; //works better than msgTempl.#{key} = "#{msg[key]}"; when the key contains dots like 'fields.name' }catch(e){ console.log("error fullfilling the msgForms resource from bundle " +e); } </c:forEach> </script> 

which worked for me, but netbeans shows this error:

 Error: The prefix "c" for the "c: forEach" element is not linked. 

because he put the insida a script JSTL tag but it works fine however

there is also another way to do this

 @ManagedBean(name = "ResouceBundle") @ApplicationScoped public class ResouceBundle implements Serializable { private static final long serialVersionUID = 1L; //needed because the bean is application|session|view and it needs to be Serializable public String msg; @PostConstruct public void init() { this.msg = createResourceBundleJSON("resourceName"); } public String createResourceBundleJSON(String resourceName) { FacesContext context = FacesContext.getCurrentInstance(); ResourceBundle bundle = context.getApplication().getResourceBundle(context, resourceName); JSONObject jsonObj = new JSONObject(); Set<String> keys = bundle.keySet(); for (String key : keys) { jsonObj.put(key, JSONObject.wrap(bundle.getString(key))); } return jsonObj.toString(); } public String getMsg() { return msg; } public static long getSerialVersionUID() { return serialVersionUID; } } 

and then in XHTML just write:

 <script type="text/javascript"> var msg = #{ResouceBundle.msg} </script> 
0
source

All Articles