Thymeleaf 에서 메세징 처리를 위해서는 다음과 같이 적용할 수 있다.
servlet-context.xml
<interceptors>
<beans:ref bean="localeChangeInterceptor"/>
</interceptors>
<beans:bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<beans:property name="basename" value="META-INF/message/messages" />
<beans:property name="defaultEncoding" value="UTF-8" />
</beans:bean>
<beans:bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<beans:property name="paramName" value="lang" />
</beans:bean>
<beans:bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<beans:property name="defaultLocale" value="en"/>
</beans:bean>
META-INF/message/messages 에 파일 추가
messages_en.properties
test=test
test.param=Test {0}
messages_ko.properties
test=테스트
test.param=테스트 {0}
Test.html 에서의 thymeleaf 문법
<div th:text="#{'test'}"></div>
<div th:text="#{'test'}"></div>
또는
<div th:text="${#messages.msg('test')}">기본값</div>
<div th:text="${#messages.msg('test1', '?')}">기본값</div>
<div th:text="${#messages.msgOrNull('test1')?:'대체값'}">기본값</div>
${#messages.msg('test')} 는 test 키 값이 없을 경우 ??test?? 라는 기본 메세지 형태로 출력이 되고
${#messages.msgOrNull('test')} 의 경우에는 null 을 리턴한다.
위의 설정대로 진행하면 URL 요청시 파라메터로 ?lang=en, ?lang=ko 의 형태로 언어셋을 변경할 수 있다.
참고 |
http://www.thymeleaf.org/doc/html/Thymeleaf-Spring3.html#listing-seed-starter-data
http://www.thymeleaf.org/doc/html/Using-Thymeleaf.html#messages-1
'Tip > Spring' 카테고리의 다른 글
jsdoc3 maven plugin 사용법 (0) | 2014.06.23 |
---|---|
Controller 에서의 Async 사용시 Spring security 와의 충돌(?) (0) | 2013.12.16 |
Redirect를 Post 요청으로? (0) | 2013.10.17 |
스프링(Spring) 에서의 리다이렉트(redirect:) (0) | 2013.10.11 |
[Thymeleaf] Thymol Static Imports (0) | 2013.08.19 |