Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a Locale in Java Web application?

I am passing language code and country code from UI to create Locale object. How to set this Locale object in HttpServletRequest, so that any where in the web application I can this Locale object by using HttpServletRequest.getLocale()?

like image 211
Mayank Gupta Avatar asked Apr 25 '26 20:04

Mayank Gupta


1 Answers

The value returned by HttpServletRequest.getLocale() is set automatically, by the Servlet container. From the Javadocs:

Returns the preferred Locale that the client will accept content in, based on the Accept-Language header. If the client request doesn't provide an Accept-Language header, this method returns the default locale for the server.

If you have a web application, you should preferably not ask the country code etc in your application, but rely on the browser preferences/settings (i.e. set by each user in the browser itself, and applicable to ALL web pages he visits). It's this value that is communicated via the above mechanism and returned by your HttpServletRequest.getLocale().

like image 187
geert3 Avatar answered Apr 27 '26 11:04

geert3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!