messages.properties
typeMismatch.item.price=상품 가격은 숫자를 입력하세요.
typeMismatch.price=가격은 숫자를 입력하세요.
typeMismatch.java.lang.Integer=Failed to convert property value of type java.lang.String to required type java.lang.Integer for property {0}; nested exception is {1}: For input string: "{2}"
typeMismatch=Failed to convert property value {0}; nested exception is {1}: For input string: "{2}"
required.item.name=상품 이름은 필수입니다.
required.name=이름은 필수입니다.
required.java.lang.String=필수 문자입니다.
required=필수 값 입니다.
item=Item
item.id=Item ID
item.name=Item Name
item.price=Item Price
item.quantity=Item Quantity
item=상품
item.id=상품 ID
item.name=상품 이름
item.price=상품 가격
item.quantity=상품 수량
basenames
defaultEncoding
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("messages", "errors");
messageSource.setDefaultEncoding(StandardCharsets.UTF_8.name())
return messageSource
}
application.properties
spring.messages.basename=messages,errors
MessageSource
NoSuchMessageException
MessageSourceResolvable
public interface MessageSource {
@Nullable
String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale);
String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException;
String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException;
}
@FunctionalInterface
public interface MessageSourceResolvable {
@Nullable
String[] getCodes();
@Nullable
default Object[] getArguments() {
return null;
}
@Nullable
default String getDefaultMessage() {
return null;
}
}
NoSuchMessageException
발생Locale
을 파라미터로 넘겨주지 않은 경우Locale.getDefault()
을 호출해서 시스템의 기본 Locale을 사용message.properties
조회#{...}
#{a.b.c(${message.argument.a.b.c})
<div th:text="#{label.item}"></div>"
LocaleResolver
AcceptHeaderLocaleResolverr
클라이언트가 서버에 기대하는 언어 정보를 담아서 요청하는 HTTP 요청 헤더
public interface LocaleResolver {
Locale resolveLocale(HttpServletRequest request);
/**
* Set the current locale to the given one.
* @param request the request to be used for locale modification
* @param response the response to be used for locale modification
* @param locale the new locale, or {@code null} to clear the locale
* @throws UnsupportedOperationException if the LocaleResolver
* implementation does not support dynamic changing of the locale
*/
void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable Locale locale);
}
item=Item
item.id=Item ID
item.name=Item Name
item.price=Item Price
item.quantity=Item Quantity
item=상품
item.id=상품 ID
item.name=상품 이름
item.price=상품 가격
item.quantity=상품 수량
spring.messages.basename
spring.messages.encoding
#{...}
_