Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing localized javascript messages from resource bundles using JSF

I have built an application with JSF and all messages issued by the server are localized with resource bundles.

My question is: how to get messages issued in the client browser with javascipt localized with the messages stored in the resource bundles?

Do I have to generate javascript dynamically and if so how can this be done?

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;
}
like image 240
Pierre C Avatar asked Dec 11 '22 18:12

Pierre C


2 Answers

Just let JSF print the desired JS code. E.g.

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

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.

like image 77
BalusC Avatar answered Dec 28 '22 06:12

BalusC


if you want to provide all keys use something like that in yout main-template etc.

<script type="text/javascript">
    var msg = new Object();
    <c:forEach items="#{msg.keySet()}" var="key">
        msg.#{key} = "#{msg[key]}";
    </c:forEach>
</script>
like image 21
wutzebaer Avatar answered Dec 28 '22 07:12

wutzebaer