반응형

App 다국어 대응 중 서버에서 내려주는 값에 대해 지역화를 진행할 필요가 있었다.

 

DB에 language code 별로 row를 추가하여 localization을 처리하는 부분도 있었으나 단순히 국가명 등의 텍스트들까지 굳이 ERD를 바꿔가며 적용을 해야하나라는 고민을 하던 중 message property를 이용하여 지역화 대응을 하게 되었다.

 

MsgConfig.java

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;

@Configuration
public class MsgConfig {

	@Bean
	public MessageSource messageSource() {
		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
		messageSource.setBasenames("message/message");
		messageSource.setDefaultEncoding("UTF-8");
		
		return messageSource;
	}
}

위와 같이 설정하여 src/main/resources/message/ 하단에

이런식으로 지역화 메세지 프로퍼티를 넣었다.

 

사용부 예시

@Autowired
MessageSource msgSource;


Srting countryName = msgSource.getMessage("KR", null, Locale.forLanguageTag("en"))

MessageSouce를 걸어주고

 

"KR"은 properties의 key 값,

LanguageTag는 어느 파일에서 값을 가져올 것인지를 결정한다

만약 LanguageTag값에 맞는 프로퍼티 파일이 없는 경우 default 프로퍼티 (message.prorperties)의 Key 값에 매핑하여 값을 가져온다.

 

프로퍼티 파일 예시

message_ko.properties

# Country Name
KR = 대한민국
PH = 필리핀
VN = 베트남

message_en.properties

# Country Name
KR = Korea
PH = Philippines
VN = Vietnam

 

'Programing' 카테고리의 다른 글

Docker 초기 셋팅  (1) 2021.01.11
[iOS] Splash Image 파일로 셋팅할때 주의할점  (0) 2020.06.08
iOS 13 Modal FullScreen 이슈  (0) 2020.04.27
[iOS] UserDefault Setting  (0) 2016.05.17
[iOS] UITableView 좌측 여백 제거  (0) 2016.04.22
Posted by npre
,