问题描述
我已经使用 JSF 构建了一个应用程序,服务器发出的所有消息都使用资源包进行了本地化.
I have built an application with JSF and all messages issued by the server are localized with resource bundles.
我的问题是:如何使用存储在资源包中的消息本地化 javascipt 在客户端浏览器中发出消息?
My question is: how to get messages issued in the client browser with javascipt localized with the messages stored in the resource bundles?
我是否必须动态生成 javascript,如果需要,如何实现?
Do I have to generate javascript dynamically and if so how can this be done?
例如,我如何让服务器在以下表单验证方法中本地化 ?alert? javascript 消息:
For example, how can I have the server localize the ?alert? javascript message 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; }
推荐答案
只要让JSF打印出想要的JS代码即可.例如
Just let JSF print the desired JS code. E.g.
<script> var message = "#{bundle['some.key']}"; </script>
你只需要考虑 JS 特殊字符,例如单引号和换行符.为此,您可以注册一个 自定义 EL 函数委托给 Apache Commons Lang StringEscapeUtils,或使用 OmniFaces of:escapeJS() 函数.
You only need to take JS special characters such as singlequotes and newlines into account. For that, you could register a custom EL function which delegates to Apache Commons Lang StringEscapeUtils, or use OmniFaces of:escapeJS() function.