Last Modified 2017.10.27

i18n

Source : https://github.com/kimjonghoon/i18nOnSpringMVC
How to run : Go to 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.

ResourceBundleMessageSource can only access resources in the classpath.
ReloadableResourceBundleMessageSource can access anywhere the resource is on the file system.

Add the following to 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 are broken.

LocalResolver

LocaleResolver is a component that determines the locale.
If you omit LocaleResolver, AcceptHeaderLocaleResolver is selected by default.
AcceptHeaderLocaleResolver uses the locale set in the "accept-language" of the request header.
The operating system's locale is set in the accept-language header.
In this case, the user can not change the locale.
If you want the user to be able to change the locale, you must select SessionLocaleResolver or CookieLocaleResolver as LocaleResolver and you need a LocaleChangeInterceptor.

Add the SessionLocaleResolver and LocalChangeInterceptor settings 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>