국제화 (i18n) 기능을 제공하는 인터페이스.

message source 만들기

두 개를 만들고 파라미터 또한 설정 가능

// messages.properties
greeting=Hello, {0}

// messages_ko_KR.properties
greeting=안녕, {0}

message source 사용하기

ApplicationContext의 MessageSource를 이용한다.

기본으로 MessageSource가 빈으로 등록되어 있고 message 관련 properties를 읽어 준다.

@Autowired
MessageSource messageSource;

messageSource.getMessage("greeting", new String[]{"mike"}, Locale.KOREA);
messageSource.getMessage("greeting", new String[]{"john"}, Locale.getDefault());

리로딩 기능이 있는 MessageSource

@Bean
public MessageSource messageSource() {
	MessageSource messageSource = new ReloadableResourceBundleMessageSource();
	messageSource.setBasename("classpath:/messages");
	messageSource.setDefaultEncoding("UTF-8");

	// 최대 3s 동안만 캐싱하도록
	messageSource.setCacheSeconds(3);

	return messageSource;
}
// test를 위한 소스
while(true) {
	String koM = messageSource.getMessage("greeting", new String[]{"mike"}, Locale.KOREA);
	String defaultM = messageSource.getMessage("greeting", new String[]{"john"}, Locale.getDefault());
	System.out.println(koM);
	System.out.println(defaultM);
	Thread.sleep(1000l);
}