정리노트

[스프링]로그②, 다국어 처리

망고고래 2024. 1. 25. 17:58

1. 로그 형태

1)서블릿 컨테이너 이용

2)필터 혹은 리스너 이용

3)AOP 이용

 

2. log4.xml

1)라이브러리 확보

2)root 태그 작성, 로그 레벨 설정

<root>
    <level value="INFO"/>
</root>

 

 

3)appender 및 layout 작성(root 상단에 위치)

<appender name="어펜더명" class="org.apache.log4j.~~Appender">
    <layout class="org.apache.log4j.~~Layout">
        <param name="~~~Pattern" value="    "/>
    </layout>
</appender>

 

4)작성한 appender root에 등록

<root>
    <level value="INFO"/>
    <appender-ref ref="어펜더명"/>
</root>

 

5)SLF4J, Log4j 연동

 

 

 

 

 

 

Ch12 다국어 처리

12.1 개요

12.2 MessageSource를 이용한 다국어 처리

12.3 LocaleResolver와 LocaleChangeInterceptor를 이용한 다국어 변경

 

 

12.1 개요

MessageSource 적용 필요

화면에 출력할 메시지를 가져와 쉽게 화면에 다국어로 표현할 수 있음

 

LocaleResolver, LocaleChangeInterceptor(MessageSource 기반) 이용: 원하는 언어 선택 가능

 

12.2 MessageSource를 이용한 다국어 처리

12.2.1 메시지 리소스 파일(*.properties) 작성

properties: key=value 쌍으로 구성

src/main/resources 폴더에 생성

 

언어별 파일 유형

파일 형식 설명
파일명.properties 시스템의 언어 및 지역에 맞는 리소스 파일이 없을 때 사용
파일명_ko.properties 시스템 언어 코드가 한국어일 경우 사용
파일명_en.properties 시스템 언어 코드가 영어일 경우 사용
파일명_en_UK.properties 시스템 언어 코드가 영어고 영국일 때 사용
파일명_ja.proeprties 시스템 언어 코드가 일본일 경우 사용

 

12.2.2 MessageSource 환경 설정

애플리케이션 컨텍스트(application context): MessageSource 인터페이스 구현체 지원, 빈 객체 생성/관계 설정/사용/제거 등 기능 담당

servlet-context.xml에 MessageSource 인터페이스 구현체를 빈 객체로 등록하면 애플리케이션 컨텍스트에 접근하여 원하는 메시지를 가져올 수 있음

<bean id="messageSource" class="org.springframework.context.support.messageSource 구현체">
    <property name="basename" value="메시지 리소스 파일"/>
    <property name="defaultEncoding" value="인코딩"/>
</bean>


<beans:beans...>
    <beans:bean id="messageSource" class="org.springframework.context.support.resourceBundleMessageSource">
        <beans:property name="basename" value="messages"/>
    </beans:bean>
</beans:beans>

 

MessageSource 구현체의 유형 설명
ResourceBundleMessageSource ResourceBundle과 MessageFormat 클래스 기반으로 만들어짐. 특정 이름으로 메시지에 접근 가능
ReloadableResourceBundleMessageSource <property name="cacheSeconds" value="2"/> 프로퍼티 설정으로 다시 시작하지 않고 애플리케이션 실행 도중에 메시지 정의를 다시 로드 가능

 

 

12.2.3 뷰 페이지에 메시지 출력

(1)스프링 태그 라이브러리 선언

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

 

(2)<spring:message> 태그로 메시지 리소스 파일에서 메시지를 가져와 출력

<spring:message> 태그 속성

속성 설명
arguments 부가적인 인자를 넘겨줌. 콤마로 구분된 문자열, 객체 배열, 객체 하나를 넘김
argumentSeparator 넘겨줄 인자의 구분자 설정. 기본값은 콤마
code 추출할 메시지의 키 지정. 지정하지 않으면 text 속성에 입력한 값 출력
htmlEscape HTML의 기본 escape 속성 오버라이딩. 기본값 false
javaScriptEscape 기본값 false
message 스프링 MVC에서 유효성 검사를 거친 오류 메시지를 보여줌
scope 결과 값을 변수에 지정할 때 변수 범위를 정함
text 해당 code 속성에서 가져온 값이 없을 때 기본으로 보여주는 문자열
var 결과 값을 저장할 때 사용

 

 

예시

(1)servlet-context.xml 파일에 MessageSource 구현체의 빈 객체 등록

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

 

(2)src/main/resources 폴더에 messages_ko.properties와 messages_en.properties 생성, 메시지 작성

addBook.form.title.label = Book Addition
addBook.form.subtitle.label = New Book Registration
addBook.form.bookId.label = Book ID
addBook.form.name.label = Name
addBook.form.unitPrice.label = Unit Price
addBook.form.author.label = Author
addBook.form.description.label = Description
addBook.form.publisher.label = Publisher
addBook.form.category.label = Category
addBook.form.unitsInStock.label = Units in Stock
addBook.form.releaseDate.label = Release Date
addBook.form.condition.label = Condition
addBook.form.bookImage.label = Book Image
addBook.form.button.label = Addition

 

(3)JSP 페이지에 <spring:message>태그 라이브러리 선언, 태그 사용해서 작성

        <legend><spring:message code="addBook.form.subtitle.label"/></legend>
        <div class="form-group row">
            <!-- <label class="col-sm-2 control-label">도서 ID</label> -->
            <label class="col-sm-2 control-label"><spring:message code="addBook.form.bookId.label"/></label>
            <div class="col-sm-3">
                <form:input  path="bookId"  class="form-control"/>  
            </div>
        </div>

 

 

12.3 LocaleResolver와 LocaleChangeInterceptor를 이용한 다국어 변경

12.3.1 LocalResolver 환경 설정

12.3.2 LocaleChangeInterceptor를 이용한 로케일 변경

 

 

12.3.1 LocalResolver 환경 설정

LocaleResolver: 웹 브라우저의 로케일을 추출, 알맞은 언어를 선택해서 메시지 출력

 

 

12.3.2 LocaleChangeInterceptor를 이용한 로케일 변경