问题描述
我已经使用 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.
你只需要考虑 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.