728x90
properties 파일을 이용한 다국어 처리 방식이다.
실습
① 파일명_국가별언어코드.properties 작성
한국어 버전
#shop_ko.properties
greeting=안녕하세요
greeting2={0},{1}
영어 버전
#shop_en.properties
greeting=hello
② Bean Configuration xml 파일에 등록
- 반드시 id="messageSource"
- 경로 지정 시 파일명만 지정
<!-- user.xml -->
<!-- properties 등록 -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:com/config/shop"></property>
<!-- properties 파일을 utf-8로 저장한 경우 사용 -->
<property name="defaultEncoding" value="utf-8"/>
</bean>
...
③ I18N 을 사용할 수 있는 곳
- Main 에서 사용
💡 getMessage("properties 키값", "properties에 전달할값" ,"일치하는 키값이 없을때 기본값", 지역명시)
public class TestMain {
public static void main(String[] args) {
MessageSource ctx = new GenericXmlApplicationContext("classpath:com/config/user.xml");
String mesg = ctx.getMessage("greeting", null, null, Locale.KOREA);
System.out.println(mesg);
String mesg2 = ctx.getMessage("greeting", null, null, Locale.ENGLISH);
System.out.println(mesg2);
String mesg3 = ctx.getMessage("greeting2", new String[] {"빵빵이", "감사합니다."}, null, Locale.KOREA);
System.out.println(mesg3);
mesg3 = ctx.getMessage("greeting2", new String[] {"옥지", "고맙습니다."}, null, Locale.KOREA);
System.out.println(mesg3);
}
}
- 임의의 빈에서 사용
💡 implements MessageSourceAware
💡 setMessageSource(MessageSource messageSource 재정의
public class UserService implements MessageSourceAware {
MessageSource ctx;
@Override
public void setMessageSource(MessageSource messageSource) {
this.ctx = messageSource;
}
public void info() {
String mesg = ctx.getMessage("greeting", null, null, Locale.KOREA);
System.out.println(mesg);
String mesg2 = ctx.getMessage("greeting", null, null, Locale.ENGLISH);
System.out.println(mesg2);
}
}
- jsp 에서 사용
💡 나중에
728x90
'WebServer > Spring' 카테고리의 다른 글
[Spring] AOP (Aspect Oriented Programming) (0) | 2023.09.14 |
---|---|
[Spring] SpEL (Spring Expression Language) (0) | 2023.09.14 |
[Spring] BeanFactoryPostProcessor를 이용한 설정 메타데이터 정의 (0) | 2023.09.13 |
[Spring] 빈 생명주기(LifeCycle) 콜백 메서드 (0) | 2023.09.12 |
[Spring] 의존성 주입(DI, Dependency Injection) - ② 어노테이션 설정 (0) | 2023.09.12 |