简体   繁体   中英

How to find the locale being applied in java mvc web application?

I am relatively new to java. I am trying to debug a spring web mvc app (spring mvc 4.2). There is this web page where when one of the controllers involved in populating the page returns the data, it does not pick the correct locale. (It picks the string value from the english properties file instead of spanish) In the rest of the page the locale get applied correctly. I have verified that the properties file exists in the correct location and it has the correct key.

When the code enters the controller, how can I tell which locale is being applied? Is there a way to force a locale inside the controller?

Look at Spring's LocaleResolver - implementation of this interface is responsible for locale resolution. This is one of Spring's extension points. Spring provides few implementations out of the box: AcceptHeaderLocaleResolver , CookieLocaleResolver , FixedLocaleResolver , SessionLocaleResolver and of course you can implement your own solution if no of the strategies above fit your requirements. If you want to debug how the locale is resolved for current request, look at DispatcherServlet - method buildLocaleContext .

如果您想要获取用户的语言环境,我认为LocaleContextHolder.getLocale()会做到这一点。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM