Spring Boot 메세지, 국제화

2024. 5. 16. 22:54·spring
웹 사이트를 개발하다보면 같은 문구가 여러 페이지에서 반복해서 들어가는 경우가 많이 존재합니다.
만약 모든곳에 직접 문구를 기입하는 와중에 특정 문구를 일괄로 바꿔야 하는 상황이 생긴다면 해당 문구가 사용되는 페이지를 모두 찾아가면서 직접 바꿔야하는 수고가 발생 합니다. 그렇기 때문에 그런 메세지들은 한곳에 보관하여 값을 불러와 화면에 뿌려준다면 매우 편리하게 관리가 가능해 질 것입니다.

 

application.properties

spring.messages.basename=messages

앞으로 메시지를 관리할 파일의 이름을 messages.properties로 할 것이며 application.properties에 위처럼 명시해줍니다.

 

messages.properties

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=취소

파일 위치는 /resources/messages.properties 이곳에 생성해주면 됩니다. 안에 내용을 보면 key와 value로 확인이 됩니다.

앞으로 사용된 페이지에서 key값만 입력하면 해당 태그에 value가 보이게 될 것같습니다.

 

messages.properties에 등록된 메세지를 사용하는 html파일을 확인해보겠습니다.

items.html

<!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">
</head>
<body>

<div class="container" style="max-width: 600px">
    <div class="py-5 text-center">
        <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" th:text="#{page.addItem}">상품 등록</button>
        </div>
    </div>

    <hr class="my-4">
    <div>
        <table class="table">
            <thead>
            <tr>
                <th th:text="#{label.item.id}">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>
            <tr th:each="item : ${items}">
                <td><a href="item.html" th:href="@{/message/items/{itemId}(itemId=${item.id})}" th:text="${item.id}">회원id</a></td>
                <td><a href="item.html" th:href="@{|/message/items/${item.id}|}" th:text="${item.itemName}">상품명</a></td>
                <td th:text="${item.price}">10000</td>
                <td th:text="${item.quantity}">10</td>
            </tr>
            </tbody>
        </table>
    </div>

</div> <!-- /container -->

</body>
</html>

 

태그 중간중간에 th:text의 문구가 보입니다. 이곳에 messages.properties에 등록한 key를 넣어주면 해당 태그에 value가 등록이 됩니다. 이미 태그사이에 문자가 들어가있지만 상관없이 messeages의 값이 노출이 됩니다.

 

 


국제화

메세지와 같이 소개하는 국제화는 웹사이트에 노출되는 메세지들을 사용자의 선호 언어에 따라 값이 언어가 바뀌면 해당 메세지들 역시 messages.properties처럼 일괄로 관리하는 방법입니다.

 

위의 작성된 messages.properties이 한글로 작성된 메세지들을 관리하는 파일이라면 이번에는 영어로 메세지를 저장해놓고 관리하는 messages_en.properties를 생성합니다.

 

messages_en.properties

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

처음에 messages.properties를 사용하기 위해 application.properties에 messages란 이름으로 등록을 했지만 messages_en.properties도 message로 인식을 하여 추가도 등록하지 않아도 사용이 가능합니다.

브라우저 언어 영어로 변경하는 방법
- 설정 > 언어 > 기본언어 > 영어를 가장 위로 올리기

 

본 글을 인프런 김영한님의 '스프링 활용'강의 내용을 기반으로 작성하였습니다.

개인적으로 자바 스프링 개발자라면 김영한님의 강의는 꼭 봐주셧으면 좋겠습니다.

'spring' 카테고리의 다른 글

Session Cookies  (0) 2024.05.31
Bean Validation  (0) 2024.05.18
Validation 분리  (0) 2024.05.15
Validation V5  (0) 2024.05.15
Validation V4  (1) 2024.05.14
'spring' 카테고리의 다른 글
  • Session Cookies
  • Bean Validation
  • Validation 분리
  • Validation V5
noAb
noAb
참개발
noAb
noAb
noAb
전체
오늘
어제
  • 분류 전체보기 (34)
    • JAVA (1)
    • spring (14)
    • sqld (4)
    • DB (1)
    • linux (6)
    • javascript (8)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

spring
validation
Spring Boot
Javascript

최근 댓글

최근 글

hELLO· Designed By정상우.v4.5.3
noAb
Spring Boot 메세지, 국제화
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.