• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

i18n using MessageResources in action class

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way to obtain MessageResources in the action class is

PageContext pageContext = (PageContext) JspFactory.getDefaultFactory().getPageContext( this.getServlet(), request, response, "", true, 1, true);

MessageResources resources = (MessageResources) pageContext.getAttribute(Action.MESSAGES_KEY,PageContext.APPLICATION_SCOPE);


and i use this **resources** object to read the localised value for a key like shown below


Locale locale = new Locale("ja");
String localizedSubject = resources.getMessage(locale, "email.subject.text1");


And this localizedSubject string is being passed as part of a serialized object to EJBs. The value is being used by java mail API to send a mail with this subject. I have tried printing this value in the server logs and found them to be nothing more than ???(question marks).

In order to check that there is no other conversion happening in the latter layers beond the action class, i tried creating a ResourceBundle object abd manually loaded the Messages.properties file. Now i read the same key using this resourceBundle and had passed it to the other layer. This process works fine and displays characters as required.

I have tested both the above mentioned steps with japanese and chinese laguage.

**Note: I have tested this in both Windows/Unix boxes, the bahaviour is the same.

Has anyone been able to achieve this before, please let me know if further information is required.

Thanks
kavitha
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get resources using the getResources() method of any Action.

??? usually appears when the value returned is null, but I would expect it to actually return something more like ???ja_.email.subject.text1??? as the null value's replacement. Perhaps it does not recognize either the locale or the key.
 
kavitha lopendrakumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marc,

Thankyou for that reply. The example that you had mentioned will happen if the key/value pair is not found in the Messages.properties file. But the issue that i am facing is not actually related to this. The question mark that i had mentioned were a result of wrong charset decoding/encoding. BTW they i correct now saying that they were not just ??? (3 question marks, if that deviated you), but they were question marks(several) in place of actual characters.

Kavitha
 
Marc Peabody
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. I see character encoding questions in many of the Javaranch forums. I know little to nothing about it myself. I'm starting to wonder if it be worth creating an entire forum here dedicated to the topic. Might be worth suggesting.
 
Beware the other head of science - it bites! Nibble on this message:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic