My previous answer did not appear in this discussion. So I try again:
Hi,
you can use Java's ResourceBundle:
ResourceBundle.getBundle(resource, locale).getString(key)
You can retrieve the locale from the request header or from a
parameter in the request query (for example when you want the user to
choose his language in your application explicitly).
I have written a utility class to decide which locale to use:
/**
* decides which Locale to use
* @author andreas.knees
*/
public class LocaleDecider {
private static Logger log = Logger.getLogger(LocaleDecider.class);
/**
* Decide, which Locale to use. The default locale is ENGLISH.
* @param acceptedLanguages list of browser accepted languages
* @param userLocale the locale the user wants to have.
* @return the outknobeled Locale.
*/
public Locale decide(List<Locale> acceptedLanguages, String
userLocale) {
if (log.isDebugEnabled()) log.debug("Accepted languages by
browser:
" + acceptedLanguages);
Locale l = Locale.ENGLISH;
if (userLocale != null) {
l = getUserLocale(userLocale);
if (log.isDebugEnabled()) log.debug("Using locale
specified in
request param: " + l);
} else if (!acceptedLanguages.isEmpty()) {
l = acceptedLanguages.get(0);
if (log.isDebugEnabled()) log.debug("Using locale of
first language
accepted by client: " + l);
} else {
if (log.isDebugEnabled()) log.debug("Using default
locale: " + l);
}
return l;
}
/**
* try to retrieve the Locale object from
* the userLocale String.
* @param userLocale
* @return
*/
public Locale getUserLocale(String userLocale) {
Locale l = Locale.ENGLISH;
String[] localeFields = userLocale.split("_");
switch (localeFields.length) {
case 1: l = new Locale(localeFields[0]);
break;
case 2: l = new Locale(localeFields[0], localeFields[1]);
break;
default: l = new Locale(localeFields[0], localeFields[1],
localeFields[2]);
}
return l;
}
}
You can call this class for example like this:
List<Locale> languages = headers.getAcceptableLanguages();
final Locale finalLocale = new LocaleDecider().decide(languages,
locale);
Regards Andreas
On 6 Mrz., 07:39, vasu <[email protected]> wrote:
> How do I retrieve localized messages on server side? I tried the same
> way as client side but I got following exception
>
> Caused by: java.lang.UnsupportedOperationException: ERROR:
> GWT.create() is only usable in client code! It cannot be called, for
> example, from server code. If you are running a unit test, check that
> your test case extends GWTTestCase and that GWT.create() is not called
> from within an initializer or constructor.
>
> looking at this exception it seems that I cannot use GWT.create() on
> server side. So then how can retrieve localized messages on server
> side if I want to? or it is not possible with the current sdk?
>
> Thanks,
> -Pandurang.
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en.