이번에 실습해볼내용은
나라의 Locale 값이용해 다국어메세지를 보낼려고하니다.
좀 신기한 기능이었던거같습니다.
공통으로 사용한 properties 는 아래와 같습니다.
message_ko_KR.properties
error.404=\uC874\uC7AC\uD558\uC9C0\uC54A\uB294\uD398\uC774\uC9C0\uC785\uB2C8\uB2E4!
error.500=\uC5D0\uB7EC\uAC00 \uBC1C\uC0DD\uD588\uC2B5\uB2C8\uB2E4.
message_en_US.properties
error.404=It's a page that doesn't exist!
error.500=There's an error!
애너테이션 방법
ContextConfiguration.java
package com.memory2.javaconfig.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
@Configuration
public class ContextConfiguration {
@Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("message");
messageSource.setCacheSeconds(10);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}
Application.java
package com.memory2.javaconfig;
import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.memory2.javaconfig.config.ContextConfiguration;
public class Application {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(ContextConfiguration.class);
String error404 = context.getMessage("error.404", null, Locale.KOREA);
String error500 = context.getMessage("error.500", null, Locale.KOREA);
System.out.println("========== Locale이 KOREA 일시 ==========");
System.out.println(error404);
System.out.println(error500);
error404 = context.getMessage("error.404", null, Locale.US);
error500 = context.getMessage("error.500", null, Locale.US);
System.out.println("========== Locale이 US 일시 ==========");
System.out.println(error404);
System.out.println(error500);
}
}
XML 방식
spring-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="message"/>
<property name="cacheSeconds" value="10"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
</beans>
Application.java
package com.memory2.xmlconfig;
import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class Application {
public static void main(String[] args) {
ApplicationContext context = new GenericXmlApplicationContext("com/memory2/xmlconfig/config/spring-context.xml");
String error404 = context.getMessage("error.404", null, Locale.KOREA);
String error500 = context.getMessage("error.500", null, Locale.KOREA);
System.out.println("========== Locale KOREA ==========");
System.out.println(error404);
System.out.println(error500);
error404 = context.getMessage("error.404", null, Locale.US);
error500 = context.getMessage("error.500", null, Locale.US);
System.out.println("========== Locale US ==========");
System.out.println(error404);
System.out.println(error500);
}
}
공통 출력
'프로그래밍 > Spring' 카테고리의 다른 글
Spring - 9. mapperscan (0) | 2021.10.25 |
---|---|
Spring - 6_2. Spring JDBC (XML) (0) | 2021.10.25 |
Spring - 7. Spring properties (0) | 2021.10.22 |
Spring - 6_1. Spring JDBC (Annotation) (0) | 2021.10.22 |
Spring - 5. Spring IOC (0) | 2021.10.18 |