Skip to content

Commit

Permalink
spring boot mvc2 - 국제화 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
finnbell committed Jul 29, 2023
1 parent d8e869e commit c1b78f1
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 29 deletions.
Binary file added mvc2/message/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion mvc2/message/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
spring.messages.basename=messages
#spring.messages.basename=messages 생략도 가능함
15 changes: 14 additions & 1 deletion mvc2/message/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
hello=안녕
hello.name=안녕 {0}
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=상품 수정
button.save=저장
button.cancel=취소
14 changes: 13 additions & 1 deletion mvc2/message/src/main/resources/messages_en.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
hello=hello
hello.name=hello {0}
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
button.save=Save
button.cancel=Cancel
13 changes: 7 additions & 6 deletions mvc2/message/src/main/resources/templates/message/addForm.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,35 @@
<div class="container">

<div class="py-5 text-center">
<h2>상품 등록 폼</h2>
<h2 th:text="#{page.addItem}"></h2>
</div>

<form action="item.html" th:action th:object="${item}" method="post">
<div>
<label for="itemName">상품명</label>
<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">가격</label>
<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">수량</label>
<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">상품 등록</button>
<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">취소</button>
type="button"
th:text="#{button.cancel}"></button>
</div>
</div>

Expand Down
15 changes: 8 additions & 7 deletions mvc2/message/src/main/resources/templates/message/editForm.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,39 @@
<div class="container">

<div class="py-5 text-center">
<h2>상품 수정 폼</h2>
<h2 th:text="#{page.updateItem}"></h2>
</div>

<form action="item.html" th:action th:object="${item}" method="post">
<div>
<label for="id">상품 ID</label>
<label for="id" th:text="#{label.item.id}"></label>
<input type="text" id="id" th:field="*{id}" class="form-control" readonly>
</div>
<div>
<label for="itemName">상품명</label>
<label for="itemName" th:text="#{label.item.itemName}"></label>
<input type="text" id="itemName" th:field="*{itemName}" class="form-control">
</div>
<div>
<label for="price">가격</label>
<label for="price" th:text="#{label.item.price}"></label>
<input type="text" id="price" th:field="*{price}" class="form-control">
</div>
<div>
<label for="quantity">수량</label>
<label for="quantity" th:text="#{label.item.quantity}"></label>
<input type="text" id="quantity" th:field="*{quantity}" class="form-control">
</div>

<hr class="my-4">

<div class="row">
<div class="col">
<button class="w-100 btn btn-primary btn-lg" type="submit">저장</button>
<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='item.html'"
th:onclick="|location.href='@{/message/items/{itemId}(itemId=${item.id})}'|"
type="button">취소</button>
type="button"
th:text="#{button.cancel}"></button>
</div>
</div>

Expand Down
17 changes: 10 additions & 7 deletions mvc2/message/src/main/resources/templates/message/item.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@
<div class="container">

<div class="py-5 text-center">
<h2>상품 상세</h2>
<h2 th:text="#{page.item}"></h2>
</div>

<!-- 추가 -->
<h2 th:if="${param.status}" th:text="'저장 완료'"></h2>

<div>
<label for="itemId">상품 ID</label>
<label for="itemId" th:text="#{label.item.id}"></label>
<input type="text" id="itemId" name="itemId" class="form-control" value="1" th:value="${item.id}" readonly>
</div>
<div>
<label for="itemName">상품명</label>
<label for="itemName" th:text="#{label.item.itemName}"></label>
<input type="text" id="itemName" name="itemName" class="form-control" value="상품A" th:value="${item.itemName}" readonly>
</div>
<div>
<label for="price">가격</label>
<label for="price" th:text="#{label.item.price}"></label>
<input type="text" id="price" name="price" class="form-control" value="10000" th:value="${item.price}" readonly>
</div>
<div>
<label for="quantity">수량</label>
<label for="quantity" th:text="#{label.item.quantity}"></label>
<input type="text" id="quantity" name="quantity" class="form-control" value="10" th:value="${item.quantity}" readonly>
</div>

Expand All @@ -45,13 +45,16 @@ <h2 th:if="${param.status}" th:text="'저장 완료'"></h2>
<button class="w-100 btn btn-primary btn-lg"
onclick="location.href='editForm.html'"
th:onclick="|location.href='@{/message/items/{itemId}/edit(itemId=${item.id})}'|"
type="button">상품 수정</button>
type="button"
th:text="#{page.updateItem}"></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">목록으로</button>
type="button"
th:text="#{page.items}"
></button>
</div>
</div>

Expand Down
13 changes: 7 additions & 6 deletions mvc2/message/src/main/resources/templates/message/items.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@

<div class="container" style="max-width: 600px">
<div class="py-5 text-center">
<h2>상품 목록</h2>
<h2 th:text="#{page.items}"></h2>
</div>

<div class="row">
<div class="col">
<button class="btn btn-primary float-end"
onclick="location.href='addForm.html'"
th:onclick="|location.href='@{/message/items/add}'|"
type="button">상품 등록</button>
type="button"
th:text="#{page.addItem}"></button>
</div>
</div>

Expand All @@ -26,10 +27,10 @@ <h2>상품 목록</h2>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>상품명</th>
<th>가격</th>
<th>수량</th>
<th th:text="#{label.item.id}"></th>
<th th:text="#{label.item.itemName}">상품명</th>
<th th:text="#{label.item.price}">가격</th>
<th th:text="#{label.item.quantity}">수량</th>
</tr>
</thead>
<tbody>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package hello.itemservice.message;

import org.assertj.core.api.AbstractStringAssert;
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.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

@SpringBootTest
public class MessageSourceTest {

@Autowired
MessageSource ms;


@Test
void helloMessage() {
String result = ms.getMessage("hello", null, null);
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");
}

}

0 comments on commit c1b78f1

Please sign in to comment.