Hi,
it is possible using the Java API:
String text = ResourceBundle.getBundle(resource,
locale).getString(key);
where
resource is the name of the .properties-file,
locale is the locale of the user request
key is the key of the message in the .properties-file
You can retrieve the locale from the request header parameter. I have
written a small utility class to decide which locale to use:
/**
* decides which Locale to use
* @author andreas.knees
*/
public class FranklinLocaleDecider {
private static Logger log =
Logger.getLogger(FranklinLocaleDecider.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
* ther 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;
}
}
The utility can be used in this way:
List<Locale> languages = headers.getAcceptableLanguages();
final Locale finalLocale = new
FranklinLocaleDecider().decide(languages, locale);
The String parameter locale my be a request parameter (if the user
wants to override the browser specific locale for example by clicking
another language in your application).
Best 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.