struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From 3844370 <3844...@163.com>
Subject bug On struts1: MessageResources.getMessage locale , whether has the problem?
Date Tue, 11 Aug 2009 18:04:54 GMT
 
On struts1:  MessageResources.getMessage(Locale locale, String key, Object[] args)  , don't
use client's locale to format !
for example: my webApp  has a myResources_en_US.properties : test =your money is {0, number,
currency} 
             client's  locale is en_US, server's locale is zh_CN
my action : 
   MessageResources resource = getResources(request, "myResources");
   //client's locale
   Locale userLocale = getLocale(request);
   //use client's locale to format value , problem is here!
   String message = resource.getMessage(userLocale, "test", new Object[]{ new Long(10000)})
 
   the result is : your money is ¥10,000.00 , 
   problem is: I use english locale to format  “ your money is {0, number, currency}”
,but now why is ¥??
 
   so, I read  src about getMessage(Locale locale, String key, Object[] args),
                format = new MessageFormat(escape(formatString));
                format.setLocale(locale);
                format.format(args)
 
for MessageFormat setLocale() , Subformats that have already been created are not affected,

so, struts1 getMessage(Locale locale, String key, Object[] args),  original idea is use client's
locale to format,
but now use server's locale,  this is the problem! 
 
following is my test procedures:
  Locale localeEN = new Locale("en", "US");
  
  // simulate struts1 MessageResources.getMessage(Locale locale, String key, Object[] args)
  MessageFormat format1 = new MessageFormat(" your money is {0, number, currency}");
  //now set EN locale already later to format  "your money is {0, number, currency}"
  format1.setLocale(localeEN);
  System.out.println(format1.format(new Object[]{ new Long(10000)}));   // your money is ¥10,000.00
  
  //correct method 1
  MessageFormat format2 = new MessageFormat(" your money is {0, number, currency}", localeEN);
  System.out.println(format2.format(new Object[]{ new Long(10000)}));  // your money is $10,000.00
  
  //correct method 2
  MessageFormat format3 = new MessageFormat("");
  format3.setLocale(localeEN);
  format3.applyPattern(" your money is {0, number, currency}");
  System.out.println(format3.format(new Object[]{ new Long(10000)})); // your money is $10,000.00
 
please write back!  Thank you!    
         yours, wangyanning             from  JiNan ShanDong  Province in CHINA

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message