다국어처리

This commit is contained in:
haerong22
2020-11-27 17:05:51 +09:00
parent 86887dcc32
commit 74a0143945
6 changed files with 30 additions and 1 deletions

View File

@@ -2,6 +2,11 @@ package com.example.restfulwebservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import java.util.Locale;
@SpringBootApplication
public class RestfulWebServiceApplication {
@@ -10,4 +15,10 @@ public class RestfulWebServiceApplication {
SpringApplication.run(RestfulWebServiceApplication.class, args);
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(Locale.KOREA);
return localeResolver;
}
}

View File

@@ -1,5 +1,7 @@
package com.example.restfulwebservice.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
@@ -7,10 +9,13 @@ import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import javax.validation.Valid;
import java.net.URI;
import java.util.List;
import java.util.Locale;
@RestController
public class UserController {
private final UserDaoService userDaoService;
@Autowired
private MessageSource messageSource;
public UserController(UserDaoService userDaoService) {
this.userDaoService = userDaoService;
@@ -51,4 +56,10 @@ public class UserController {
throw new UserNotFoundException(String.format("ID[%s] not found", id));
}
}
@GetMapping(path = "/hello-world-internationalized")
public String helloWorldInternationalized(
@RequestHeader(name = "Accept-Language", required = false) Locale locale) {
return messageSource.getMessage("greeting.message", null, locale);
}
}

View File

@@ -3,4 +3,8 @@ server:
logging:
level:
org.springframework: DEBUG
org.springframework: DEBUG
spring:
messages:
basename: messages

View File

@@ -0,0 +1 @@
greeting.message=<EFBFBD>ȳ<EFBFBD><EFBFBD>ϼ<EFBFBD><EFBFBD><EFBFBD>

View File

@@ -0,0 +1 @@
greeting.message=Hello

View File

@@ -0,0 +1 @@
greeting.message=Bonjour