You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Denis Hanson <de...@mediaone.net> on 2001/01/16 15:11:03 UTC

Javascript + localized messages

Hello,

I'm using the i18n support in Struts in my applications, and am trying to
localize my client-side Javascript validation messages using
<bean:message/>.  I've tried

   var msg='<bean:message key="login.error.username"/>';
   alert(msg);

and

   alert(<bean:message key="login.error.username"/>);

and many other combinations, none of which worked for me.  Has anyone been
able to use <bean:message/> with Javascript?


I searched the archives and found a message from Pierre Metras dated
10-Nov-2000 where he mentions creating a utility class to retrieve localized
messages.  I'm about to try to create such a class, but how would the
utility class access the Struts Locale setting in the session, and retrieve
the name of the properties file to use?  Its not a tag so the pageContext is
not supplied.

Thanks,

Denis Hanson




RE: Javascript + localized messages

Posted by Denis Hanson <de...@mediaone.net>.
Till,

You're absolutely correct - I was using <!-- //--> comments around the
Javascript functions.

I removed them as you suggested and <bean:message/> worked perfectly.
Thanks very much.

Denis


-----Original Message-----
From: Till Nagel [mailto:till@netzministerium.de]
Sent: Wednesday, January 17, 2001 8:09 AM
To: struts-user@jakarta.apache.org
Subject: Re: Javascript + localized messages


Hi Denis,

you have HTML-comments around the JavaScript code, right?
So just delete them and it will work. It is not a Struts problem but a JSP
one.

If you want to support old browsers and include those HTML comments you must
use JSP Script to print it.

 <script language="JavaScript" type="text/javascript">
  <%
  out.println("<!--");
  %>

  alert("<bean:message key="whatever"/>");

  <%
  out.println("// -->");
  %>
 </script>

Regards,
Till


Re: Javascript + localized messages

Posted by Till Nagel <ti...@netzministerium.de>.
Hi Denis,

you have HTML-comments around the JavaScript code, right?
So just delete them and it will work. It is not a Struts problem but a JSP
one.

If you want to support old browsers and include those HTML comments you must
use JSP Script to print it.

 <script language="JavaScript" type="text/javascript">
  <%
  out.println("<!--");
  %>

  alert("<bean:message key="whatever"/>");

  <%
  out.println("// -->");
  %>
 </script>

Regards,
Till


Re: Javascript + localized messages

Posted by Pierre Métras <ge...@sympatico.ca>.
Hi Denis,

> I'm using the i18n support in Struts in my applications, and am trying to
> localize my client-side Javascript validation messages using
> <bean:message/>.  I've tried
>
>    var msg='<bean:message key="login.error.username"/>';
>    alert(msg);
>
> and
>
>    alert(<bean:message key="login.error.username"/>);
>
> and many other combinations, none of which worked for me.  Has anyone been
> able to use <bean:message/> with Javascript?

It should work both forms. I've all my JavaScript messages localized that
way.
Check that you have set the locale init-param in your action servlet, in the
web.xml file:

    <servlet>
      <servlet-name>
        action
      </servlet-name>
      <servlet-class>
        org.apache.struts.action.ActionServlet
      </servlet-class>
...
      <init-param>
        <param-name>
          locale
        </param-name>
        <param-value>
          true
        </param-value>
...

Pierre Métras