# 세팅
resources/application.properties
#국제화 경로설정시 아래 폼
#spring.messages.basename=messages,config.i18n.messages
#국제화 default
spring.messages.basename=messages
messages.properties
hello=안녕
hello.name=안녕 {0}
label.item=상품
label.item.id=상품 ID
label.item.itemName=상품명
label.item.price=가격
label.item.quantity=수량
page.items=상품 목록
page.item=상품 상세
page.addItem=상품 등록
page.updateItem=상품 수정
page.saveComplete=저장 완료
button.save=저장
button.cancel=취소
messages_en.properties
hello=hello
hello.name=hello {0}
label.item=Item
label.item.id=Item ID
label.item.itemName=Item Name
label.item.price=price
label.item.quantity=quantity
page.items=Item List
page.item=Item Detail
page.addItem=Item Add
page.updateItem=Item Update
page.saveComplete=Save Complete
button.save=Save
button.cancel=Cancel
#사용예시 (th:text="#{properties에 있는 정보}
ex)<h2 th:text="#{page.addItem}">message.properties_{로케일}의 page.addItem 값</h2>
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<link th:href="@{/css/bootstrap.min.css}"
href="../css/bootstrap.min.css" rel="stylesheet">
<style>
.container {
max-width: 560px;
}
</style>
</head>
<body>
<div class="container">
<div class="py-5 text-center">
<h2 th:text="#{page.addItem}">상품 등록 폼</h2>
</div>
<form action="item.html" th:action th:object="${item}" method="post">
<div>
<label for="itemName" th:text="#{label.item.itemName}">상품명</label>
<input type="text" id="itemName" th:field="*{itemName}" class="form-control" placeholder="이름을 입력하세요">
</div>
<div>
<label for="price" th:text="#{label.item.price}">가격</label>
<input type="text" id="price" th:field="*{price}" class="form-control" placeholder="가격을 입력하세요">
</div>
<div>
<label for="quantity" th:text="#{label.item.quantity}">수량</label>
<input type="text" id="quantity" th:field="*{quantity}" class="form-control" placeholder="수량을 입력하세요">
</div>
<hr class="my-4">
<div class="row">
<div class="col">
<button class="w-100 btn btn-primary btn-lg" type="submit" th:text="#{button.save}">저장</button>
</div>
<div class="col">
<button class="w-100 btn btn-secondary btn-lg"
onclick="location.href='items.html'"
th:onclick="|location.href='@{/message/items}'|"
type="button" th:text="#{button.cancel}">취소</button>
</div>
</div>
</form>
</div> <!-- /container -->
</body>
</html>
#상세기능들
package hello.itemservice.message;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import java.util.Locale;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
@SpringBootTest
public class MessageSourceTest {
@Autowired
MessageSource ms;
@Test
void helloMessage(){
String result = ms.getMessage("hello",null,null);
Assertions.assertThat(result).isEqualTo("안녕");
}
@Test
void notFoundMessageCode(){
assertThatThrownBy(()->ms.getMessage("no code",null,null))
.isInstanceOf(NoSuchMessageException.class);
}
@Test
void notFoundMessageCodeDefaultMessage(){
String result = ms.getMessage("no_code",null,"기본 메세지",null);
assertThat(result).isEqualTo("기본 메세지");
}
@Test
void argumentMessage(){
String message = ms.getMessage("hello.name", new Object[]{"Spring"},null);
assertThat(message).isEqualTo("안녕 Spring");
}
@Test
void defaultLang(){
assertThat(ms.getMessage("hello",null,null)).isEqualTo("안녕");
assertThat(ms.getMessage("hello",null, Locale.KOREA)).isEqualTo("안녕");
}
@Test
void enLang(){
assertThat(ms.getMessage("hello",null,Locale.ENGLISH)).isEqualTo("hello");
}
}
'web > Spring' 카테고리의 다른 글
@RequestParam @RequestBody @ModelAttribute 초간단 정리 (0) | 2022.07.08 |
---|---|
form -> server ->form 할 때 form 데이터 유지하고 싶다면 (0) | 2022.06.30 |
intelliJ Spring properties의 국제화 작업 중 한글 인코딩 안됨 문제 (0) | 2022.06.29 |
form의 elements를 객체에 바로 바인딩 (0) | 2022.06.29 |
redirect시 attribute 전송 (0) | 2022.06.29 |