15> 국제화(I18N: Internationalization)
- 로케일(locale) 에 따라 화면의 언어를 다르게 랜더링이 가능하다.
다국어 처리가 가능하다.
- Spring 이 특정언어를 선택하는 방법
==> 기본적으로 LocaleResolver 를 이용해서 Locale 를 인식하여
원하는 특정언어를 선택 가능.
가. AcceptHeaderLocaleResolver
- 요청 헤더값을 이용해서 locale 정보를 얻음.
- Accept-language 헤더값 이용.
- 기본적으로 적용되는 방법임.
나. CookieLocaleResolver
- 쿠키를 이용해서 locale 정보를 저장 및 얻음.
- setLocale("ko|en");
다. SessionLocaleResolve
- 세션을 이용해서 locale 정보를 저장 및 얻음.
- setLocale("ko|en");
- 구현1
(1) 리소스 번들 파일 작성(resource bundle 파일)
-> 문법:
src/main/resources 폴더
ㄴ파일명-언어코드.properties
-> 키=값
ex>
기본번들파일: message.properties
greeting=안녕하세요
한국어 번들파일: message_ko.properties
greeting=안녕하세요
영어 번들파일: message_en.properties
greeting=hello
일본어 번들파일: message_jp.properties
greeting=こんにちは
-> 번들파일의 인코딩은 모두 utf-8 로 변경
# message-en.properties
greerting=hello
# message-ko.properties
greerting=안녕하세요
# message.properties
greerting=안녕하세요. 기본값
(2) application.properties 에 리소스 번들 등록
# application.properties
# 리소스 번들 파일 등록
spring.messages.basename=bundle/message
spring.messages.encoding=utf-8
# message.properties
greeting=안녕하세요. 기본값
# message_ko.properties
greeting=안녕하세요
# message_en.properties
greeting=hello
(3) spring.messages.fallback-to-system-locale=false
기본: true. message.properties 가 아닌 system 의 locale(제어판의 국가별 설정) 적용함.
false. message.properties 적용.
///////////////////////////////////////////////////
#application.properties 에서 I18N 설정(종합)
# 리소스 번들 파일 등록
spring.messages.basename=bundle/message
spring.messages.encoding=utf-8
spring.messages.fallback-to-system-locale=false
///////////////////////////////////////////////////
# application.properties
logging.level.org.springframework=info
# tomcat port 번호 변경
server.port=8090
# context 명 변경
server.servlet.context-path=/app
# jsp의 경로와 확장자 지정
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
# 리소스 번들 파일 등록
spring.messages.basename=bundle/message
spring.messages.encoding=utf-8
spring.messages.fallback-to-system-locale=false
(4) SessionLocaleResolver 생성
@Configuration
public class WebMvc {
// 기본인 AcceptHeaderLocaleResolver 인데
// 기본을 SessionLocaleResolver로 변경하는 작업이다.
@Bean
public SessionLocaleResolver localeResolver() {
SessionLocaleResolver locale =
new SessionLocaleResolver();
locale.setDefaultLocale(new Locale("ko"));
return locale;
}
}
package com.exam.config;
import java.util.Locale;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@Configuration
public class WebMvc {
// 기본인 AcceptHeaderLocaleResolver 인데
// 기본을 SessionLocaleResolver로 변경하는 작업이다.
@Bean
public SessionLocaleResolver localeResolver() {
SessionLocaleResolver locale =
new SessionLocaleResolver();
locale.setDefaultLocale(new Locale("ko"));
return locale;
}
}
(5) Controller 에서 개발자가 직접 locale 변경(명시적)
@GetMapping("/bundle")
public String bundle( HttpServletRequest request,
HttpServletResponse response,
@RequestParam String lang) {
//SessionLocaleResolver의 로케일을 변경.
locale.setLocale(request, response,
new Locale(lang));
Locale xxx = LocaleContextHolder.getLocale();
System.out.println("현재 로케일2:" + xxx);
return "show";
}
package com.exam.controller;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@Controller
public class MainController {
Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
SessionLocaleResolver locale;
@GetMapping("/main")
public String main() {
Locale xxx = LocaleContextHolder.getLocale();
System.out.println("현재 로케일1:" + xxx);
return "main";
}
@GetMapping("/bundle")
public String bundle( HttpServletRequest request,
HttpServletResponse response,
@RequestParam String lang) {
//SessionLocaleResolver의 로케일을 변경.
locale.setLocale(request, response,
new Locale(lang));
Locale xxx = LocaleContextHolder.getLocale();
System.out.println("현재 로케일2:" + xxx);
return "show";
}
}
package com.exam;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
<%@ page
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>main.jsp</h2>
<p>안녕하세요</p>
<a href="bundle?lang=en">영어</a><br>
<a href="bundle?lang=ko">한국어</a><br>
<a href="bundle?lang=jp">일본어</a><br>
</body>
</html>
<%@ page
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>show.jsp</h2>
bundle message:<spring:message code="greerting"
text="default값"></spring:message>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.18</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.exam</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 구현2
(1) LocalechangeInterceptor 이용해서 로케일 변경해줌.
따라서 Controller 에서 명시적으로 setLocale(new Locale("값")) 할 필요가 없음.
@Configuration
public class WebMvc implements WebMvcConfigurer {
// 기본인 AcceptHeaderLocaleResolver 인데
// 기본을 SessionLocaleResolver로 변경하는 작업이다.
@Bean
public SessionLocaleResolver localeResolver() {
SessionLocaleResolver locale =
new SessionLocaleResolver();
locale.setDefaultLocale(new Locale("ko"));
return locale;
}
// LocalChangeInterceptor 생성
@Bean
public LocaleChangeInterceptor changeInterceptor() {
LocaleChangeInterceptor xxx = new LocaleChangeInterceptor();
// ?lang=en|ko|jp 의 값을 changeInterceptor 자동으로 세팅해줌.
xxx.setParamName("lang"); // lange 의 값을 changeInterceptor 세팅해줌.
return xxx;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(changeInterceptor());
}
}
package com.exam.config;
import java.util.Locale;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@Configuration
public class WebMvc implements WebMvcConfigurer {
// 기본인 AcceptHeaderLocaleResolver 인데
// 기본을 SessionLocaleResolver로 변경하는 작업이다.
@Bean
public SessionLocaleResolver localeResolver() {
SessionLocaleResolver locale =
new SessionLocaleResolver();
locale.setDefaultLocale(new Locale("ko"));
return locale;
}
// LocalChangeInterceptor 생성
@Bean
public LocaleChangeInterceptor changeInterceptor() {
LocaleChangeInterceptor xxx = new LocaleChangeInterceptor();
// ?lang=en|ko|jp 의 값을 changeInterceptor 자동으로 세팅해줌.
xxx.setParamName("lang"); // lange 의 값을 changeInterceptor 세팅해줌.
return xxx;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(changeInterceptor());
}
}
'[study]이론정리 > Spring Boot' 카테고리의 다른 글
Spring MVC - 폼유효성체크 (0) | 2024.07.03 |
---|---|
Spring MVC - webjar (0) | 2024.07.03 |
Spring MVC - 예외처리 (0) | 2024.07.03 |
Spring MVC - HandlerInterceptor (X 라고 가정) (0) | 2024.07.03 |
Spring MVC - FileUpload & download (0) | 2024.07.02 |