Spring/Spring framework

Spring message properties

원2 2022. 1. 7. 18:39
728x90
반응형

spring framework 에서 사용하는 다국어 지원기능

 

일반적으로 하드코딩 식으로 다국어를 지원하려고 하면 일일이 변경해줘야 하는 상황이 발생하고

다국어를 지원 안하더라도 어떠한 하나의 단어를 변경해야 할 경우 모든 페이지를 찾아서 바꿔줘야하는 번거로움이 생긴다.

그럴때 Spring message 를 사용한다면 설정 파일에서 해당 단어를 찾아 바꿔주기만 한다면 모든 페이지에 적용이 가능하다.

따라서 spring message를 쓰자.

일일이 찾고 싶다면 말리지는 않겠다..

 

 

Spring 에서 제공하는 MessageSource 를 bean으로 등록해서 사용하면 관리가 편해지고

>웹어플리케이션의 리로딩 없이도 메시지 변경이 가능 하다고 한다.

 

 

Spring 에서 제공하는 주요 Messagesource 구현체

 

  •  org.springframework.context.support.StaticMessageSource 
  • MessageSource 의 간단한 구현체
  • 기본적인 다국어 기능을 지원하면서 테스트요도로 사용된다.

 

  •  org.springframework.context.support.ResourceBundleMessageSource
  • ResourceBundle Class, MessageFormat Class 기반으로 만들어져 Bundle에 특정 명칭으로 접근이 가능하다.

 

  • org.springframework.context.support.ReloadableResourceBundelMessageSource
  •  Property  설정을 통해 Reloading 정보를 입력해 주기적인 Message Reloading을 수행한다.
  • 따라서 Application 종료 없이도 실행 도중에 변경이 가능한 장점이 있다.

이런식으로 구성을 하자 

ar은 빼자 아랍어임;


 

message.properties

 

message_en.properties

message_ko.properties

 

 

이런식을 구성을 해준다.

 


dispatcher-servlet.xml

 

인터셉터로 요청이 들어오면 paramName lang 의 값으로 해당 언어값을 가져오게 처리

 

아래 bean의 property  name=basename 은 고정값이며 value 는 적용할 properties의 주소가 되겠다

 

ex) requeset.getParameter(getParamNAme("lang"))을 검색해서

url?lang=ko >> 한글 언어 값

url?lang=en >> 영어 언어 값

 

 

그 후 다국어 메세지 파일 messages.message에 있는 properties 파일을 읽고 가져오는 객체를 spring bean에 등록

 

자바에서 쓸 때는 @Inject로 객체 주입을 받는다.

    <mvc:interceptors>
        <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="lang" />
        </bean>
    </mvc:interceptors>

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

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="message.messages" />
        <property name="defaultEncoding" value="UTF-8" />
        <property name="cacheSeconds" value="60" />
    </bean>

JSP 에서 해당언어 호출 방법

 

이런식으로 받아오면 된다.

 

code 로 받거나 key로 받아주면 된다.ㅇㅇ

 

아래의 태그에서는 lang 값을 ko, en 을 줘서 해당 언어를 받아오게 링크를 걸어뒀다.

 


한글 가져오기

 


영어 가져오기

 

굿?

 

혹시나 하는 추가설명 :: key vlaue 형식(parameter)으로 보냈지만 interceptor로 처리를 해버리기 땜누에 따로 페이지마다 설정을 하지않아도 전체 언어는 선택 한 값이 계속 유지되므로 매 페이지엔 설정을 해주지 않아도 된다.

 

2탄 >

https://210one2.tistory.com/373

 

spring message properties 2탄

요로코롬 메뉴바가 있다. 허졉하지만 메뉴바임ㅇㅇ/.. 앞에 했던 spring message로 변경을 한다고 치면 아마 url에 param이 붙어서 엉망이될 것이다.. 아마도 예를들어 현재 영어인상태에서 영어를 한

210one2.tistory.com

 

 

728x90
반응형