아키텍처와 함께

블로그 이미지
by gregorio
  • Total hit
  • Today hit
  • Yesterday hit

이번에는 Spring MVC와 Spring Boot의 다국어를 처리하기 위한 방법에 대해 비교한다.


먼저 Spring MVC를 사용하여 다국어 처리하는 경우는 다음과 같이 XML 설정이 필요하다.


■ mvc-context-servlet.xml


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

    

    <!-- 쿠키를 이용한 Locale 이용시 

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


    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">

        <property name="paramName" value="locale" />

    </bean>


다국어를 처리하기 위해 localeResolver와 localeChangeInterceptor를 bean으로 설정한다.

그리고 다국어 메세지를 정의한다.


■context-message.xml


 <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">

<property name="basenames">

<list>

<value>classpath:/message/message</value>

</list>

</property>

<property name="defaultEncoding" value="UTF-8" />

<property name="cacheSeconds" value="60" />

</bean>

    

</beans>


Spring에서 사용하느 메세지를 정의하기 위해 ReloadableResourceBundleMessageSource에 basenames에 메세지 파일이 정의되어 있는 위치를 등록한다.



Spring boot에서는 Annotation을 이용하여 localeResolver와 localeChangeInterceport를 정의한 후 localeChangeInterceptor를 등록해야한다.



■WebConfig.java


import java.util.ArrayList;

import java.util.List;

import java.util.Locale;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.web.servlet.FilterRegistrationBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.support.ReloadableResourceBundleMessageSource;

import org.springframework.core.Ordered;

import org.springframework.web.servlet.LocaleResolver;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import org.springframework.web.servlet.view.json.MappingJackson2JsonView;


import dymn.demo.filter.ABCFilter;

import dymn.demo.interceptor.SessionInterceptor;

import dymn.demo.util.PropertiesUtil;


@Configuration

// @EnableWebMvc

// public class WebConfig extends WebMvcConfigurerAdapter {

// public class WebConfig extends WebMvcConfigurationSupport {

public class WebConfig implements WebMvcConfigurer {


/** Register intercepter **/

@Override

public void addInterceptors(InterceptorRegistry registry) {

registry.addInterceptor(localeChangeInterceptor());

}


@Bean

public ReloadableResourceBundleMessageSource messageSource() {

ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();

messageSource.setBasename("classpath:message/message");

messageSource.setCacheSeconds(10);

messageSource.setDefaultEncoding("UTF-8");


return messageSource;

}


@Bean(name = "localeResolver")

public LocaleResolver sessionlocaleResolver() {

SessionLocaleResolver localeResolver = new SessionLocaleResolver();

localeResolver.setDefaultLocale(new Locale("en_US"));

return localeResolver;

}


@Bean

public LocaleChangeInterceptor localeChangeInterceptor() {

LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();

localeChangeInterceptor.setParamName("locale");


return localeChangeInterceptor;

}

}



WebMvcConfigurer를 구현하는 Class를 생성한다.


먼저 ReloadableResourceBundleMessageSource를 이용하여 messageSource Bean을 생성한다.


messageSource bean에 메세지 파일의 위치 등 필요한 정보를 등록하여 messageSource를 생성한다.


SessionLocaleResolver를 이용하여 localeResolver bean을 생성 시 bean이름을 localeResolver를 정의한다.


LocaleChangeInterceptor를 이용하여 localeChangeInterceptor bean을 생성할 때 parameterName을 locale로 정의한다.


파라미터가 locale로 입력되는 경우 localeChangeInterceptoer에 의해 Local이 변경된다.


마지막으로  WebMvcConfigurer에 정의되어 있는 addInterceptors 메소드를 재정의하여 localeChangeInterceptor를 Interceptor로 등록한다.




AND

ARTICLE CATEGORY

분류 전체보기 (56)
Spring Framrwork (33)
Linux (1)
APM (1)
Java (8)
python (0)
ant (1)
chart (1)
OS (1)
tomcat (1)
apache (1)
database (0)

RECENT ARTICLE

RECENT COMMENT

CALENDAR

«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

ARCHIVE

LINK