1. κ°λ π
1. λ©μμ§ (Message)
νλ©΄μ 보μ΄λ 'μνλͺ 'μ΄λΌλ λ¨μ΄λ₯Ό 'μνμ΄λ¦'μΌλ‘ λ°κΎΈκ³ μΆμ κ²½μ° μ΄λ»κ² ν΄μΌ ν κΉ? κ°λ¨νκ²λ item.htmlμμ ν΄λΉ λ¨μ΄λ₯Ό λ°κΎΈλ©΄ λλ€.
νμ§λ§ μνλͺ μ΄λΌλ λ¨μ΄κ° item.html, addItem.html, updateItem.html, deleteItem.html, ... μμ μ¬μ©λκ³ μλ€λ©΄ λͺ¨λ νμΌμ κ³ μ³μΌ νλ€λ λ¬Έμ κ° μλ€.
μ΄λ κ² HTML νμΌμ λ©μμ§λ₯Ό νλμ½λ©λμ΄ μλλ‘ νλ κ² μλλΌ, λ©μμ§λ₯Ό ν κ³³μμ κ΄λ¦¬νλλ‘ ν μ μλλ° μ΄κ²μ΄ λ°λ‘ λ©μμ§ κΈ°λ₯μ΄λ€.
2. κ΅μ ν (Internationalization)
νκ΅μ΄λ₯Ό μ¬μ©νλ μ¬λμκ²λ 'μνλͺ ', μμ΄λ₯Ό μ¬μ©νλ μ¬λμκ²λ 'Item Name'μΌλ‘ 보μ¬μ£Όκ³ μ νλ€. μ΄λ₯Ό κ°λ₯νκ² νλ κ²μ΄ κ΅μ ν κΈ°λ₯μ΄λ€.
2. λ©μμ§ μμ€ μ€μ π
λ©μμ§μ κ΅μ νλ /resources/messages.propertiesμ μλ λ΄μ©μ μ½μμΌλ‘μ¨ λμνλ€. (λ¬Όλ‘ messsages.propertiesκ° μλ λ€λ₯Έ νμΌμ μ½μ μλ μλ€.) messages.propertiesλ₯Ό μ½μ μ μλλ‘ μ€μ νλ λ°©λ²μΌλ‘λ μ§μ λ±λ‘(μ€νλ§ λΉ)κ³Ό μλ λ±λ‘(μ€νλ§ λΆνΈ)μ΄ μλ€.
1. μ§μ λ±λ‘ (μ€νλ§ λΉ)
μ€νλ§μ΄ μ 곡νλ MessageSourceλ₯Ό μ€νλ§ λΉμΌλ‘ λ±λ‘νμ¬ λ©μμ§ κΈ°λ₯μ μ¬μ©ν μ μλ€.
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
messageSource.setDefaultEncoding("utf-8");
return messageSource;
}
MessageSourcλ μΈν°νμ΄μ€μ΄λ―λ‘ ResourceBundleMessageSourceλΌλ ꡬν체λ₯Ό μ΄μ©ν΄μΌ νλ€.
- basename
κ°μΌλ‘ messagesλ‘ μ§μ νλ©΄ messages.properties, messages_ko.properties, messages_en.properties λ±μ μ½μ μ μλ€.
μ΄λ μ£Όμν μ μ μ΄λ¦μ '.'μ λ£μ§ μλλ‘ νμ. '.'μ pathꡬλΆμλ‘ λ³ννκΈ° λλ¬Έμ΄λ€. (ex. error.message) - defaultEncoding
μΈμ½λ© μ 보λ₯Ό μ μ₯νλ€.
νμΌμ μμΉλ /resources/messages.propertiesμ΄λ€.
2. μλ λ±λ‘ (μ€νλ§ λΆνΈ)
μ€νλ§ λΆνΈλ₯Ό μ¬μ©νλ©΄ μ€νλ§ λΆνΈκ° MessageSourceλ₯Ό μλμΌλ‘ μ€νλ§ λΉμΌλ‘ λ±λ‘νλ€. λ©μμ§ μμ€λ₯Ό application.propertiesμ μλμ κ°μ΄ μ€μ ν μ μλ€.
spring.messages.basename=messages
κΈ°λ³Έ κ°μ΄ spring.messages.basename=messages λΌμ λ³λλ‘ μ€μ μ νμ§ μμλ λκΈ΄ νλ€. κ·Έλ¬λ μΆκ°λ‘ λ©μμ§ κ°μ κ΄λ¦¬νκ³ μΆμ κ²½μ°μλ λ€μκ³Ό κ°μ΄ μ€μ μ ν μ μλ€.
#μμ
spring.messages.basename=messages, config.i18n.messages
3. λ©μμ§ νμΌ μ€μ π₯
μλμ κ°μ΄ /resourcesμ 'messages.properties', 'messages_en.properties'μ μ€μ ν μ μλ€.
# /resources/messages.properties
label.item=μν
label.item.id=μν ID
label.item.itemName=μνλͺ
page.addItem=μν λ±λ‘
page.updateItem=μν μμ
hello.name=μλ {0}
# /resources/messages_en.properties
label.item=Item
label.item.id=Item ID
label.item.itemName=Item Name
page.addItem=Item Registration
page.updateItem=Item Modification
hello.name=hello {0}
4. μΉ μ ν리μΌμ΄μ μμ μ¬μ©νκΈ° (νμ리ν) π
κ·Έλ¦¬κ³ 'xxx.html'νμΌμ λ€μκ³Ό κ°μ΄ μ€μ ν μ μλ€.
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>Title</title>
</head>
<body>
<div th:text="#{label.item}"></div>
<div th:text="#{label.item.id}"></div>
<div th:text="#{label.item.itemName}"></div>
<br>
<div th:text="#{page.addItem}"></div>
<div th:text="#{page.updateItem}"></div>
<br>
<div th:text="#{hello.name(${userName})}"></div> // νλΌλ―Έν° μ¬μ©
</body>
</html>
1. λ©μμ§
μμ HTMLμ μ΄μ©νλ©΄ νλ©΄μλ λ©μμ§ νμΌ(messages.properties)μ μλ κ°λ€μ΄ μΆλ ₯λλ€. (νκΈμ΄ λ¬Όμνλ‘ λμ¬ λμλ '[Settings] - [File Encodings] - Default encoding for properties files : UTF-8'λ‘ μ€μ νλλ‘ νμ. μ΄λ κ² μ€μ νλ©΄ messages.propertiesκ° λ€μ λ¬Όμνλ‘ λ°κΎΈμ΄ μμ μ μλλ° κ·Έκ²λ νμΈνμ.)
맨 μ²μμ κ³ λ €νλ κ²μ²λΌ 'μνλͺ 'μ 'μνμ΄λ¦'μ΄λΌκ³ λ©μμ§ νμΌμμ μμ ν΄ λ³΄μ.
# /resources/messages.properties
# 'μνλͺ 'μ 'μνμ΄λ¦'μΌλ‘ μμ
label.item=μν
label.item.id=μν ID
label.item.itemName=μνμ΄λ¦ #μ¬κΈ°λ₯Ό μμ
page.addItem=μν λ±λ‘
page.updateItem=μν μμ
hello.name=μλ {0}
κ·Έλ¬λ©΄ λ€μκ³Ό κ°μ΄ 'μνλͺ 'μ΄ 'μνμ΄λ¦'μ΄λΌκ³ 보μ΄κ² λλ€. νλμ© λ°κΏμ€ νμκ° μλ€!
2. κ΅μ ν
κ΅μ νλ Locale μ 보λ₯Ό κΈ°λ°μΌλ‘ μ΄λ£¨μ΄μ§λ€. Localeμ λ§μΆμ΄ ꡬ체μ μΈ κ²μ΄ μμΌλ©΄ ꡬ체μ μΈ κ²μ μ°Ύκ³ , μμΌλ©΄ λν΄νΈ(messages.properties)λ₯Ό μ°Ύλλ€.
νμ¬μλ Localeμ΄ koμΈλ° messages_ko.propertiesκ° μμΌλ―λ‘ messages.propertiesκ° μ¬μ©λκ³ μλ€. κ·Έλμ λ€μκ³Ό κ°μ΄ νκΈ νλ©΄μ΄ μΆλ ₯λλ κ²μ΄λ€. (Localeμ κ°λ°μλꡬ(F12) → Network → Request Headers → Accept-Languageμμ νμΈμ΄ κ°λ₯νλ€.)
λ§μ½ Localeμ΄ μμ΄λ‘ λ€μ΄μ¨λ€λ©΄ messages_en.propertiesκ° μ¬μ©λμ΄ μλμ κ°μ μμ΄ νλ©΄μ΄ λμ¬ κ²μ΄λ€.
μμ΄λ‘ μ€μ νλ λ°©λ²μ 'Chrome λ§μΆ€μ€μ λ° μ μ΄ → μΈμ΄ → μμ΄ → κ°μ₯ μλ‘ μ΄λ'μΌλ‘ μ€μ νλ©΄ λλ€. κ·Έλ¬λ©΄ Accept-Languageκ° enμ΄ λλ€.
5. μ€νλ§μμ μ¬μ©νκΈ° π
@SpringBootTest
public class MessageSourceTest {
@Autowired
MessageSource ms;
@Test
void itemMessage() {
String result = ms.getMessage("label.item", null, null);
Assertions.assertThat(result).isEqualTo("μν");
}
// νλΌλ―Έν° μ¬μ©
@Test
void argumentMessage() {
String result = ms.getMessage("hello.name", new Object[]{"Amenable"}, null);
Assertions.assertThat(result).isEqualTo("μλ
Amenable");
}
}
ν΄λΉ κΈμ
κΉμν λμ 'μ€νλ§ MVC 2νΈ - λ°±μλ μΉ κ°λ° νμ© κΈ°μ ',
H.Kwon λμ 'Spring Boot(Spring) i18n μ€μ μ μ£Όμμ¬ν',
hanker λμ 'Spring boot - λ€κ΅μ΄ μ²λ¦¬ μ νκΈ λ¬Όμν(?)λ‘ λμ¬λ'
μ μ°Έκ³ νμμ΅λλ€.
'π μ€νλ§ > κΈ°λ³Έ κ°λ ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
μ€νλ§ μΈν°μ ν°(Spring Interceptor) (0) | 2023.04.30 |
---|---|
μλΈλ¦Ώ νν°(Servlet Filter) (0) | 2023.04.29 |
HTTP μλ΅(μ μ 리μμ€, λ·° ν νλ¦Ώ, HTTP λ©μμ§) μ²λ¦¬ (0) | 2023.04.21 |
HTTP μμ²(ν€λ, νλΌλ―Έν°, λ©μμ§) μ²λ¦¬ (0) | 2023.04.20 |
μ€νλ§ MVC (0) | 2023.04.19 |