Spring Boot3 & Spring Framework 6 강의 ::Section.8 REST API의 국제화 알아보기

suragryen·2024년 3월 13일
0

Udemy-Spring

목록 보기
20/25
  • REST API는 전세계에 소비자가 있을 수 있다.(다양한 언어)

-이때 국제화 또는 i18n을 사용해야 함

  • 영어로 국제화는 18개의 글자로 이루어져있기 때문에 i18n으로 간략히 줄여 쓰기도 한다.
  • 국제화 처리할 때마다 HTTP Request 헤더 사용.(Accept Language: 자연어와 소비자가 선호하는 로케일을 나타냄.)
  1. controller 생성
	@RequestMapping(path = "/hello-world-Internatlized") 
	public String helloWorldInternatlized() {
		return "Hello world V2";
		
		//1: 메세지 저장
		//2: 이 값을 가져올 수 있는 코드 작성  
	}
  1. message.properties

  1. controller에 MessageSource 메소드 활성화 및 생성자 주입
	private MessageSource msgSource;
	//메시지의 매개변수화나 국제화에 대한 지원을 통해 메시지를 처리  
	
	public HellowWorldController(MessageSource msgSource) {
		this.msgSource = msgSource;
	}
  1. Locale, MessageSource
	@RequestMapping(path = "/hello-world-Internatlized") 
	public String helloWorldInternatlized() {
		
		Locale locale = LocaleContextHolder.getLocale();
		//현재 스레드와 연관된 로케일을 반환 
		//사용자는 요청과 함께 Accept 헤더를 감지하는데, 그다음 해당 헤더에 대한 로케일은 LocaleContextHolder.getLocale();에 의해 반환.
		return msgSource.getMessage("good.morning.message", null, "Default Msg", locale);
		//return "Hello world V2";
		
		//1: 메세지 저장
		//2: 이 값을 가져올 수 있는 코드 작성  
	}

결과

  • 헤더에 Accept-Language를 추가해준다

profile
블로그 이사중 ☃︎

0개의 댓글

관련 채용 정보