Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multilanguage Problems (Property Files) 1

Status
Not open for further replies.

fatcodeguy

Programmer
Feb 25, 2002
281
CA
Hi,

I have an application that implements the Struts framework on several pages (forms basically). I have to program the app in English and French, so I use PropertyResourceBundles and property files to get the tags to output, so I only need to have different property files.

Code:
<% 
  Locale currentLocale=(Locale)session.getAttribute("locale"); 
  PropertyResourceBundle labels=(PropertyResourceBundle)PropertyResourceBundle.getBundle("itp.resources.login",currentLocale);
  //output with
  out.println(labels.getString("label.login")); 
>%

However, when I try to imbed this in a Struts tag (like a button name), I get errors and have to do this

Code:
<% if((Locale)session.getAttribute("locale")==Locale.ENGLISH)  {%>
         <td><html:submit value="Login" /></td>
<% }else if((Locale)session.getAttribute("locale")==Locale.FRENCH){  %>
         <td><html:submit value="Accès" /></td>
<% } %>

instead of

Code:
<td><html:submit value="<% out.println(labels.getString("label.login.submitbutton")); %>" /></td>

Should I be doing this another way? Is there a better way to do multilanguage support?

All help much appreciated. Thanks
 
Better to use Struts message resource. Just set your current properties files as stucts application resource file, then you don't even need to get the property bundle. and just set the locale to struts:

Code:
request.getSession().setAttribute(org.apache.struts.Globals.LOCALE_KEY, <whatever locale>);

then in your JSP:

Code:
<td>
   <html:submit titleKey="label.login.submitbutton"/>
</td>
 
Hey Byam,

If I do this, how does struts know which property bundle to read from, because as it is I have one for each page.

Thanks.
 
Struts support multiple message resource. But that only allows one resource per struts module, not per page.

If you want to use struts resource for localization, unless you implement your own MessageResourcesFactory to pick properties file according page, I'm afraid you'll have to put all resource in one property file.
 
I have my other tags set with different property files, like welcome messages and labels.

I added the following to the struts-config.xml file
Code:
<!-- ========== Message Resources ============ -->
<message-resources key="home" parameter="itp.resources.home" null="false"/>
<message-resources key="contact" parameter="itp.resources.contact" null="false"/>
<message-resources key="login" parameter="itp.resources.login" null="false"/>

And in my jsp page i use
Code:
<bean:message bundle="home" key="title.text.t1"/>
to output the message.

The key attribute in the message-resource tag determines how the resource is accessed. So in the bean:message tag, the bundle attribute indicates which resource to read from. However, the html:submit tag
Code:
<html:submit titleKey="button.text.t1" />
does not support the bundle attribute.

This makes sense?
 
Interesting. I didn't realize there is a bundle attribute to select which bundle to use. Good to know that.

As for <html:submit/> attribute "bundle" has been added since Struts 1.2.5. You may want to download the latest Struts release.

 
Hum... 1.2.5 won't be release soon. But that what it says in current struts' online documentation about the bundle attribute.

I checked the nightly build, even the lastest yesterday build doesn't have support for bundle attribute yet.

May be you can do it with a Javascript hack for now:

Code:
<html>
<head>
<script language="javascript>
   function initSubmitButtonText() {
     document.forms[0].submit.title = '<bean:message bundle="home" key="title.text.t1"/>';
   }
</script>

</head>
<body onload="initSubmitButtonText()">
....

<html:submit/>

...
</html>
 
Turns out there's a MUCH simpler way, I just found it in the apache struts site (one of the guides, i don't know which)

Code:
<html:submit><bean:message bundle="login" key="button.text.t1"/></html:submit>

Wish I'd know that from the begining!

Anyway, thanks for all your help and input Byam!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top