i18n
Source: https://github.com/kimjonghoon/i18nOnSpringMVC
How to run: Go to the root directory and run mvn jetty:run and visit http://localhost:8080.
MessageSource
MessageSource is a component that looks at the locale and determines the message. ResourceBundleMessageSource and ReloadableResourceBundleMessageSource are implementations of MessageSource. ResourceBundleMessageSource can only access resources on the classpath, whereas ReloadableResourceBundleMessageSource allows resources to access any resources in the file system.
Add ReloadableResourceBundleMessageSource to the Spring configuration file.
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="WEB-INF/classes/messages" /> <property name="defaultEncoding" value="UTF-8" /> </bean>
Without <property name="defaultEncoding" value="UTF-8" />, Non english characters may be broken.
LocalResolver
LocaleResolver is a component that determines the locale. If you omit LocaleResolver in the Spring configuration file, Spring selects AcceptHeaderLocaleResolver by default. AcceptHeaderLocaleResolver uses the locale set in the "accept-language" of the request header in which the browser has already set the operating system's locale. In this case, users can not change the locale. If you want users to change the website's locale, you must select either SessionLocaleResolver or CookieLocaleResolver as LocaleResolver, additionally requires LocaleChangeInterceptor.
Add SessionLocaleResolver and LocalChangeInterceptor to the Spring configuration file.
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"> <property name="defaultLocale" value="en" /> </bean> <mvc:interceptors> <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> <property name="paramName" value="lang" /> </bean> </mvc:interceptors>