Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Determining Locale .... How?

 
Joe Simone
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an EJB app and would like to know how I determine what the current locale setting is. If I call Locale.getDefault() I get the default JVM Locale. All of my resource strings have been externalized into resource bundles.

Any help is greatly appreciated.

Regards,
Joe
 
Greg Charles
Sheriff
Posts: 2987
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally speaking, you don't get the locale in EJB code. One of the strengths of the J2EE platform is the separation of business and presentation logic. The EJBs should be doing business logic, and the locale is only relevant for presentation done by JSPs or servlets (if presentation is handled on the server at all).

Usually, the user will set a preferred language for the site, which you can save in a user profile on the server or in a cookie. Something in the HTTP request may indicate the user's locale, but I'm not sure.
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with the previous poster. Locale is usually related to internationalization, which EJB should be independent of. I can think of some usecases where you will have different logic based on locale. If you must, you can pass locale from the presentation layer.

Locale locale = request.getLocale();

This method is actually defined in the ServletRequest interface. The value you receive will depend on the the locale set when the http request (or user's browser). Locale.getDefault() always returns the locale of the JVM and hence should not be trusted.

Long story short, pass the locale to the business layer if you have to. BUT try not to pass the Locale object itself, pass the String value that way business layer is less dependent on the [http] presentation layer

C
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic