Spring/Spring

day73) 다국어 처리(국제화)

code_learner 2022. 4. 13. 15:56

[다국어 처리]

하나의 페이지를 여러언어로 서비스.

이번 포스팅에서는 한국어와 영어 2개 국어를 사용해보자. 

 

-사용할 페이지 : login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login.do" method="post">
<table border="1">
<tr>
<td>아이디</td>
<td><input type="text" name="id" value="${user.id}" /></td>
</tr>
<tr>
<td>비밀번호</td>
<td><input type="password" name="password" value="${user.password}" /></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" value="로그인"></td>
</tr>
</table>
</form>
<hr>
<a href="signup.jsp">회원가입</a>

</body>
</html>

[1. 메세지 파일 설정]

다음과 같은 뎁스로 a_en.properties(영어 서비스), a_ko.properties(한국어 서비스) 파일 2개를 생성한다.

각각의 파일에 [메세지키 = 메세지 값] 을 설정한다.

 

*메세지 키는 다른 페이지를 국제화 할때 중복 될 수 있으므로 3레이어 정도로 하는 것이 바람직하다.

 

-a_en.properties

#login.jsp
message.login.title=COFFEE_STORAGE | LOGIN
message.login.user=USER
message.login.pw=PW
message.login.login=LOGIN
message.login.sign=SIGNUP

-a_ko.properties

# login.jsp

message.login.id=\uC544\uC774\uB514
message.login.pw=\uBE44\uBC00\uBC88\uD638
message.login.login=\uB85C\uADF8\uC778
message.login.sign=\uD68C\uC6D0\uAC00\uC785

영어를 제외한 언어는 자동으로 유니코드로 변환된다.

 


[2. 메세지 파일을 읽어오는 클래스 MessageSource 클래스 등록]

-DispatcherServlet-servlet.xml

<!-- 메세지 파일을 읽어오는 클래스 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
	<property name="basenames">
		<list>	
		<!-- 메시지 패키지 안에 있는 a* 파일을 읽어달라 -->
			<value>message.a</value>
		</list>
	</property>
</bean>

-<value></value> 내부에 message.a_en.properties를 각각 선언하지않는 이유

 

1.  message.a_ko.properties 메세지의 확장자는 무조건 properties이기 때문에 굳이 붙이지 않는다. 
2. 지역정보를 추출해서 사용하기 때문에 .ko등이 필요없다
3.  위의 정보들을 사용하면 오히려 충돌이 생길 위험이 있으므로 message.a같은 패키지만 선언하는 방법이 오류를 최소화하기 위한 방법 


[3. localResolver클래스 등록]

브라우저에서 HTTP 요청 헤더에 접속지역정보가 존재함
-> 이용하고 있는 지역정보가 session안에 들어가 있으므로 그것을 추출해서 내 위치가 어디인지 파악할 예정

-> 지역정보를 추출하여 MessageSource에게 전달해줄 클래스 => localResolver클래스

 

-DispatcherServlet-servlet.xml

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />

 

-localResolver가 현재 내위치를 추출해주는 클래스였다면, LocaleChangeInterceptor 클래스는 위치변경정보를 변경시켜 값을 전달해준다.

1. 네임스페이스에 mvc등록(인터셉터류)

<mvc:interceptors>
   	<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <!-- lang파라미터 변수에 접속정보를 준다면 그에 따라 바뀌게 하겠습니다. -->
     	<property name="paramName" value="lang" />
  	</bean>
</mvc:interceptors>

:lang 이름으로 된 파라미터 값을 받으면 위치정보변경됨

 

[4. view변경]

-<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>상단에 추가

-바꿀 메시지 <spring:message code="message.login.--" />형식으로 변경

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--한국어와 영어로 서비스할 예정-->
<a href="login.do?lang=ko">한국어</a> | <a href="login.do?lang=en">English</a>
<hr>
<form action="login.do" method="post">
   <table border="1">
      <tr>
         <td><spring:message code="message.login.id" /></td>
         <td><input type="text" name="id" value="${user.id}" /></td>
      </tr>
      <tr>
         <td><spring:message code="message.login.pw" /></td>
         <td><input type="password" name="password" value="${user.password}" /></td>
      </tr>
      <tr>
         <td colspan="2" align="right"><input type="submit" value="<spring:message code="message.login.login" />"></td>
      </tr>
   </table>
</form>
<hr>
<a href="signup.do"><spring:message code="message.login.sign" /></a>

</body>
</html>

 

*유의사항: 위치정보를 전달해야 하므로 반드시 컨트롤러를 거쳐 lang값을 받아와야한다.(forward)

 

 

-결과-

-> index.jsp 를 통해 login.jsp를 가게 된다면 현재 내 위치인 한국을 기준으로 한국어 서비스가 제공되고, English를 누르게 된다면 영어 서비스가 제공되는 것을 볼 수 있다.