問題描述
我已經使用 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.
這篇關于使用 JSF 從資源包傳遞本地化的 javascript 消息的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!