Spring 3 MVC: Internationalization & localization
Application Context Configuration
Message Resource Files
org.springframework.context.support.ReloadableResourceBundleMessageSource
to the application context:DispatcherServlet
enables you to automatically resolve messages using the client’s locale. This is done with LocaleResolver
objects. You can select between- an
AcceptHeaderLocaleResolver
, which inspects theaccept-language
header in the request that was sent by the client (e.g., a web browser). Usually this header field contains the locale of the client’s operating system. - a
CookieLocaleResolver
, - and a
SessionLocaleResolver
, which allows you to retrieve locales from the session that might be associated with the user’s reques
CookieLocaleResolver
CookieLocaleResolver
made the most sense for Podcastpedia.org . It inspects a cookie namedpodcastpediaPreferredLanguage
, that might exist on the client to see if a locale is specified:defaultLocale
is set to American English. The cookieMaxAge
(the maximum time a cookie will stay persistent on the client) is set to 604800 seconds (one month).
LocaleChangeInterceptor
LocaleResolver
is normally used in combination with the LocaleChangeInterceptor
, which allows you to change of the current locale by using a defined parameter in the request (in this case the lang
parameter). So, for example, a request for the following URL,http://www.podcastpedia.org/categories?lang=de
, will change the site language to German:Access the Locale in Spring
org.springframework.context.i18n.LocaleContextHolder.LocaleContextHolder.getLocale()
, which returns the Locale associated with the current thread, if any, or the system default Locale else. In the following code snippet you can see how I use this in the start page’s controller to display the newest and most popular podcasts based on the language selected:Browser Caching optimization
expiring
and cache control
for the html pages to 0 seconds:We promise to only share high quality podcasts and episodes.
Post a Comment
Click to see the code!
To insert emoticon you must added at least one space before the code.