| 목차 >> Globalization & Multi TimeZone +- Globalization +- Multi TimeZone +- User Locale & TimeZone 설정 |
다국어와 시간대를 표현하기 위해서는 GlueStaticContext 의 다음 API를 제공하고 있습니다.
Glue Framework에서는 다국어 표현을 위해 Spring 에서 제공하는 ResourceBundleMessageSource를 사용합니다. 그렇기 때문에 다국어를 표현하기 위해서는 다음 2가지가 선행되어야 합니다.
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>message</value>
<value>format</value>
</list>
</property>
</bean>
<Resource명> "_" language "_" country "_" variant <Resource명> "_" language "_" country <Resource명> "_" language <Resource명>
Resource Bundle file은 language, country가 포함된 2번째 규칙을 기본으로 하나, Locale정보에 따라 그외도 가능합니다. 다음은 파일 명명 규칙을 따른 예입니다.
message_ko_KR_IBMeucKR.properties message_ko_KR.properties message_ko.properties message_en_US.properties message_en.properties message.properties
GlueStaticContext 의 getResourceMessage() 사용법은 Java Doc을 참고합니다. (GlueAPI)
Resource Bundle 파일은 Locale 정보에 근거하여 사용되며, Locale 적용 우선 순위는 다음과 같습니다.
다음은 사용예이다.
Java Code
String key = "showtable.header.empinfo";
System.out.println("Default::::" + GlueStaticContext.getResourceMessage(key));
System.out.println("English::::" + GlueStaticContext.getResourceMessage(key, Locale.US));
System.out.println("한국어::::" + GlueStaticContext.getResourceMessage(key, Locale.KOREA));
JSP Code
서버 기본값 : : <%= GlueStaticContext.getResourceMessage(key) %><BR/>
English : : <%= GlueStaticContext.getResourceMessage(key, Locale.US) %><BR>
한국어 : : <%= GlueStaticContext.getResourceMessage(key,Locale.KOREA) %><BR>
다국어를 지원하기 위해, 한글을 추출해서 properties 작업을 하게 됩니다. 그러나 이렇게 작업한 properties 파일은 실제 사용시에 한글이 깨지는 문제가 발생하므로 다음 방법을 선택할 수 있습니다.
native2ascii는 %JAVA_HOME% / bin / 에서 발견할 수 있고, plugin은 각 사이트의 설치 가이드를 따릅니다.
Glue Framework에서는 Multi Time Zone을 보여주기 위해 GlueStaticContext.getTimeZoneFormat() 을 제공합니다. getTimeZoneFormat() 사용법은 Java Doc을 참고합니다. (GlueAPI)
다음은 사용예이다.
Java Code
Date date = new Date();
String textformat = "yyyy-MM-dd HH:mm:ss (z Z)";
System.out.println("Default::::" +
GlueStaticContext.getTimeZoneFormat(date, TimeZone.getDefault().getID(), textformat));
System.out.println("Asia/Seoul::::" +
GlueStaticContext.getTimeZoneFormat(date, "Asia/Seoul", textformat));
System.out.println("Canada/Eastern::::"
+GlueStaticContext.getTimeZoneFormat(date, "Canada/Eastern", textformat));
System.out.println("Asia/Shanghai::::"
+ GlueStaticContext.getTimeZoneFormat(date, "Asia/Shanghai", textformat));
System.out.println("+05:00::::"+GlueStaticContext.getTimeZoneFormat(date, 5*60, textformat));
System.out.println("-06:00::::"+GlueStaticContext.getTimeZoneFormat(date, -6*60, textformat));
Jsp Code
서버 기본값 : : <%= GlueStaticContext.getTimeZoneFormat(date,TimeZone.getDefault().getID()) %><BR> Asia/Seoul : : <%= GlueStaticContext.getTimeZoneFormat(date,"Asia/Seoul") %><BR> Europe/London : : <%= GlueStaticContext.getTimeZoneFormat(date,"Europe/London") %><BR> America/New_York : : <%= GlueStaticContext.getTimeZoneFormat(date,"America/New_York") %><BR>
Spring MVC 기반의 웹 application을 개발하는 경우, interceptor 를 이용해 User Locale과 TimeZone을 설정 할 있습니다.
다음 예제 interceptor 에서는 User Locale & TimeZone 정보를 HttpSession 에 저장합니다.
package sample;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class CheckSessionInterceptor extends HandlerInterceptorAdapter
{
@Override
public boolean preHandle( HttpServletRequest request, HttpServletResponse response, Object handler ) throws Exception
{
Locale userLocale = request.getLocale();
if(userLocale != null){
Locale locale = userLocale;
String timeZone = getTimeZone(locale);
request.getSession().setAttribute( "user_locale", locale );
request.getSession().setAttribute( "user_timezone", timeZone );
return true;
} else {
response.sendRedirect("setting.jsp");
return false;
}
}
private String getTimeZone(Locale locale){
. . .
}
}
이렇게 작성된 interceptor는 dispatcher-servlet.xml에 다음과 같이 정의할 수 있습니다.
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="sessionChecker"/>
</list>
</property>
<property name="mappings">
<props>
/*.mvc=controller
</props>
</property>
</bean>
<bean id="sessionChecker" class="sample.CheckSessionInterceptor" />
sessionChecker interceptor가 적용되면, JSP에서는 다음과 응용할 수 있습니다.
Message : <%= GlueStaticContext.getResourceMessage(key, (Locale)ctx.get("user_locale", GlueWebContext.SESSION_ATTR)) %><BR/>
Time : <%= GlueStaticContext.getTimeZoneFormat(date, (String)ctx.get("user_timezone", GlueWebContext.SESSION_ATTR)) %><BR/>