2025. 3. 2. 23:10ㆍSpring Microservice
🌍 Spring Boot 마이크로서비스 국제화(i18n) 적용하기
🎯 1. 국제화의 필요성
국제화(i18n, internationalization)는 다양한 언어와 지역 환경을 지원하는 애플리케이션을 개발하는 과정입니다.
🌎 사용자가 다양한 언어 환경에서 동일한 기능을 사용할 수 있도록 지원해야 합니다.
✅ 목표:
- 라이선스 서비스에서 언어별 메시지 번역 적용
Accept-Language
헤더를 통해 언어 변경 가능- 다국어 메시지를 Spring의 MessageSource를 활용하여 관리
🏗 2. Spring Boot 설정 추가
Spring Boot에서 국제화를 적용하기 위해 LocaleResolver와 MessageSource를 설정해야 합니다.
📝 📌 LocaleResolver & MessageSource 설정 (LicensingServiceApplication.java)
package com.optimagrowth.license;
import java.util.Locale;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@SpringBootApplication
public class LicensingServiceApplication {
public static void main(String[] args) {
SpringApplication.run(LicensingServiceApplication.class, args);
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
// 🇺🇸 기본 로케일을 미국(US)으로 설정
localeResolver.setDefaultLocale(Locale.US);
return localeResolver;
}
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
// 메시지를 찾을 수 없을 경우, 메시지 코드 자체를 반환
messageSource.setUseCodeAsDefaultMessage(true);
// 메시지 파일의 기본 경로 설정
messageSource.setBasenames("messages");
return messageSource;
}
}
🔹 설정한 주요 사항
✅ SessionLocaleResolver
→ 기본 로케일을 미국(US)으로 설정
✅ messageSource.setUseCodeAsDefaultMessage(true)
→ 해당 키가 없으면 오류 대신 키 값을 반환
✅ messageSource.setBasenames("messages")
→ 메시지 파일을 messages.properties
에서 찾음
📄 3. 다국어 메시지 파일 생성
메시지 파일을 /src/main/resources/
폴더 아래에 생성해야 합니다.
📝 📌 영어 메시지 (messages_en.properties)
license.create.message = License created %s
license.update.message = License %s updated
license.delete.message = Deleting license with id %s for the organization %s
📝 📌 스페인어 메시지 (messages_es.properties)
license.create.message = Licencia creada %s
license.update.message = Licencia %s creada
license.delete.message = Eliminando licencia con id %s para la organization %s
✅ 디폴트 메시지 파일 (messages.properties
)도 생성 가능 → 디폴트 로케일 메시지를 저장
🛠 4. 서비스 코드 수정 (MessageSource 적용)
📝 📌 LicenseService.java 수정
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import java.util.Locale;
import org.springframework.stereotype.Service;
@Service
public class LicenseService {
@Autowired
private MessageSource messages;
public String createLicense(License license, String organizationId, Locale locale){
if (license != null) {
license.setOrganizationId(organizationId);
return String.format(messages.getMessage(
"license.create.message", null, locale),
license.toString());
}
return null;
}
public String updateLicense(License license, String organizationId, Locale locale){
if (license != null) {
license.setOrganizationId(organizationId);
return String.format(messages.getMessage(
"license.update.message", null, locale),
license.toString());
}
return null;
}
public String deleteLicense(String licenseId, String organizationId, Locale locale){
return String.format(messages.getMessage(
"license.delete.message", null, locale),
licenseId, organizationId);
}
}
🔹 수정한 주요 사항
✅ @Autowired MessageSource messages;
→ 다국어 메시지를 가져오기 위해 MessageSource 주입
✅ messages.getMessage("license.create.message", null, locale)
→ 로케일에 따라 다국어 메시지 반환
🔗 5. 컨트롤러에서 Accept-Language
지원
이제 클라이언트에서 Accept-Language
헤더를 보내면 해당 언어에 맞는 메시지를 반환해야 합니다.
📝 📌 LicenseController.java 수정
import org.springframework.web.bind.annotation.RequestHeader;
import java.util.Locale;
@PostMapping
public ResponseEntity<String> createLicense(
@PathVariable("organizationId") String organizationId,
@RequestBody License request,
@RequestHeader(value = "Accept-Language", required = false) Locale locale){
return ResponseEntity.ok(licenseService.createLicense(request, organizationId, locale));
}
🔹 설정한 주요 사항
✅ @RequestHeader("Accept-Language") Locale locale
→ 요청 헤더에서 언어 설정을 받아옴
✅ 언어가 설정되지 않으면 기본 로케일(Locale.US)을 사용
🛠 6. 다국어 API 테스트
Postman을 활용해 다국어 기능을 테스트해보겠습니다.
🟢 1) Accept-Language: en
(영어 요청)
📌 POST 요청 (영어)
POST http://localhost:8080/v1/organization/optimaGrowth/license
Headers:
- Content-Type: application/json
- Accept-Language: en
🟡 2) Accept-Language: es
(스페인어 요청)
📌 POST 요청 (스페인어)
POST http://localhost:8080/v1/organization/optimaGrowth/license
Headers:
- Content-Type: application/json
- Accept-Language: es
📌 Postman에서 Accept-Language: es
를 설정하면 자동으로 스페인어 응답을 반환! 🏆
🎉 7. 결론
✅ 국제화(i18n) 기능을 Spring Boot 마이크로서비스에 성공적으로 적용!
✅ LocaleResolver
와 MessageSource
를 설정해 다국어 메시지 관리
✅ Accept-Language
헤더를 활용해 클라이언트 요청에 따라 언어 자동 변경
✅ Postman을 이용해 영어(🇺🇸)와 스페인어(🇪🇸) API 테스트 성공! 🚀
💡 다음 단계 → HATEOAS 적용하기! 🔥
'Spring Microservice' 카테고리의 다른 글
The DevOps story: Building for the rigors of runtime (2) | 2025.03.03 |
---|---|
Implementing Spring HATEOAS to display related links (0) | 2025.03.03 |
Building the doorway into the microservice: The Spring Boot controller (0) | 2025.03.02 |
The developer’s tale: Building a microservice with Spring Boot and Java (0) | 2025.03.02 |
3.Building microserviceswith Spring Boot (0) | 2025.03.02 |