getting locale to service

28 views
Skip to first unread message

Mark Volkmann

unread,
Mar 23, 2007, 9:49:55 PM3/23/07
to Google-We...@googlegroups.com
I'm looking for suggestions for how I can propagate the desired
locale to a GWT RPC service.
I know I can specify a Spanish locale by appending ?locale=es on the
URL.
How can I obtain that locale in my client-side code so I can pass it
to an RPC service?
System.getProperty("locale") doesn't work.

Adam T

unread,
Mar 24, 2007, 3:21:26 AM3/24/07
to Google Web Toolkit
Hi Mark,

A really simple way to "cheat" with this is to define a specific
constant in your properties files, say called locale, and then set
that to the value you want, e.g.

MyConstants.properties
.....
locale : en
.....


MyConstants_es.properties
......
locale: es
......

etc etc. Then to know the locale you are in you would just use
myConstants.getLocale() to get the value which you can then pass to
the RPC service as a parameter. (you need to remember to define this
value in all of your properties files)

Other ways are to try and parse the href location using JSNI or to try
and search through the DOM of the page looking for the meta tag that
has set the property (but by far the easiest is the "cheating" way
above!). The JSNI approach would look something like the following
(taken for the GWT In Action example) except instead of the last
couple of lines getting rid of the existing locale, you'd want to
extract it instead, so I guess you'd be interested in locArray[1].

/**
* JSNI method to change the locale of the application - it
effectively
* parses the existing URL and creates a new one for the chosen
locale.
*
* It additionally launches any JavaScript debugger that might be
* attached to the system (Windows only). To disable this
functionality
* just remove the "debugger" line.
*
* @param newLocale String value of the new i18n locale to go to.
*/
private native void changeLocale(String newLocale)/*-{
// Uncomment the "debugger;" line to see how to set debug
statements in JSNI code
// When in web mode, if your browser has a JavaScript debugger
attached, it will
// launch at this point in the code (when the user changes
locale through the menu system).

//debugger;

// Get the current location
var currLocation = $wnd.location.toString();
// Get rid of any GWT History tokens that might be present
var noHistoryCurrLocArray = currLocation.split("#");
var noHistoryCurrLoc = noHistoryCurrLocArray[0];
// Get rid of any locale string
var locArray = noHistoryCurrLoc.split("?");
// Build the new href location and then send the browser
there.
$wnd.location.href = locArray[0]+"?locale="+newLocale;
}-*/;


Hope that gives you some ideas!

//Adam
Co-author of GWT In Action http://www.manning.com/hanson
GWT Example http://dashboard.manning-sandbox.com

Mark Volkmann

unread,
Mar 24, 2007, 5:56:15 PM3/24/07
to Google-We...@googlegroups.com
On Mar 24, 2007, at 2:21 AM, Adam T wrote:

> Hi Mark,
>
> A really simple way to "cheat" with this is to define a specific
> constant in your properties files, say called locale, and then set
> that to the value you want, e.g.
>
> MyConstants.properties
> .....
> locale : en
> .....
>
>
> MyConstants_es.properties
> ......
> locale: es
> ......
>
> etc etc. Then to know the locale you are in you would just use
> myConstants.getLocale() to get the value

I assume you mean myConstants.locale().
Clever trick. That works great!

> which you can then pass to
> the RPC service as a parameter. (you need to remember to define this
> value in all of your properties files)
>
> Other ways are to try and parse the href location using JSNI or to try
> and search through the DOM of the page looking for the meta tag that
> has set the property (but by far the easiest is the "cheating" way
> above!). The JSNI approach would look something like the following

Since the solution above does what I need and is far easier to
implement, I'll stick with that.
Thanks!

Reply all
Reply to author
Forward
0 new messages