How to get the locale during runtime from server side or client side

310 views
Skip to first unread message

Ark Xu

unread,
Nov 20, 2006, 12:45:59 AM11/20/06
to Google-We...@googlegroups.com

Hi all,

  Is there any way to get the locale during runtime? I searched the answers here but none of them are usable, follows my search result:

1. from client side I got this JS:
    public static native String getProperty(String name) /*-{
         return parent.__gwt_getMetaProperty(name); <BR>        }-*/;
Yes it works fine in host mode, but in web mode it doesn't . I thought it maybe that in web mode the <head> information never be overwritten this property "<meta name='gwt:property' content='locale=XX'>"


2. from server side I got this method:

getThreadLocalRequest().getSession().getLocale()

But I can not find the method getLocale in HTTPSeesion class...


So I need help to get the locale information during runtime. Any information is appreciate!
Thanks!

--
Best regards,

Ark Xu

Mat Gessel

unread,
Nov 20, 2006, 1:51:29 AM11/20/06
to Google-We...@googlegroups.com
A while ago I suggested a technique to determine the user agent on the
client at runtime:
http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/fbf61ff4e63d207

You should be able to use the same technique to determine the locale.

-= Mat

Emily

unread,
Nov 20, 2006, 8:07:24 AM11/20/06
to Google Web Toolkit
Could you post an example? As long as you have a
<meta name = 'gwt:property' content='locale=???'/> in your html
page, your code should return the locale.

Another stratagem is to have the server return the locale as a URL
argument. Check out the I18N example for an example of that. The
I18N.gwt.xml property provider shows how to parse the URL arguments to
get the locale.

Finally, are you sure you need to access the locale in your program?
If you can use the built in GWT I18N support instead, the runtime
checks get pushed back into compile time optimizations, which leads to
smaller, faster code.

Emily

unread,
Nov 20, 2006, 1:55:07 PM11/20/06
to Google Web Toolkit
Could you post the code? As long as your client html has the correct
meta tag, it should work in both hosted or web mode. Also consider if
you can get away with just using the built in I18N support, as that is
done compile time, so is faster and smaller than runtime checks.

Ark Xu

unread,
Nov 20, 2006, 10:29:03 PM11/20/06
to Google-We...@googlegroups.com
Hi Emily,
 
  Yes. I am using the i18N with the standard  static string internationalization  . I generate the .java file and property file via the provided i18ncreator and everything works fine. That means adding the ?locale=XX to the URL, the GWT module really choose the correct cache file to load.
 
  But my problem is: in host mode, when I add the ?locale=XX to the URL, the meta data <meta name = 'gwt:property' content='locale=???'/> is automatically added to the HTML file but in web mode it doesn't. Due to that, the native JS can not get the right locale from the client side in web mode because there is not such meta data exist. I don't know what's going wrong...
 
  Get the locale during rumtime is important because some UI controls need it when rendering there own content such as currency or date ETC...
 
Ark
 

Amit Kasher

unread,
Jan 15, 2007, 8:28:09 AM1/15/07
to Google Web Toolkit
I can suggest a workaround to knowing the locale at runtime:
You can put the locale as an entry in the constants/messages property
files. For example, in your en_US file (could be called
UserMessages_en_US), put the following entry:

locale=en_US

Also, put this value in each of your other locale-sensitive-property
files. Then you can just use UserMessages.locale() method to get the
locale at runtime.

Nevertheless,
I agree that this small feature is necessary: getting the locale at
runtime.

Reply all
Reply to author
Forward
0 new messages