diff --git a/app/kac-app/src/main/java/kr/co/palnet/kac/app/config/WebConfig.java b/app/kac-app/src/main/java/kr/co/palnet/kac/app/config/WebConfig.java new file mode 100644 index 0000000..9065dba --- /dev/null +++ b/app/kac-app/src/main/java/kr/co/palnet/kac/app/config/WebConfig.java @@ -0,0 +1,19 @@ +package kr.co.palnet.kac.app.config; + +import kr.co.palnet.kac.app.core.web.BaseAcceptHeaderLocaleResolver; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.LocaleResolver; +import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver; + +import java.util.Locale; + +@Configuration +public class WebConfig { + @Bean + public LocaleResolver localeResolver() { + AcceptHeaderLocaleResolver localeResolver = new BaseAcceptHeaderLocaleResolver(); + localeResolver.setDefaultLocale(Locale.KOREA); + return localeResolver; + } +} diff --git a/app/kac-app/src/main/java/kr/co/palnet/kac/app/core/web/BaseAcceptHeaderLocaleResolver.java b/app/kac-app/src/main/java/kr/co/palnet/kac/app/core/web/BaseAcceptHeaderLocaleResolver.java new file mode 100644 index 0000000..98d05cb --- /dev/null +++ b/app/kac-app/src/main/java/kr/co/palnet/kac/app/core/web/BaseAcceptHeaderLocaleResolver.java @@ -0,0 +1,68 @@ +package kr.co.palnet.kac.app.core.web; + +import jakarta.servlet.http.HttpServletRequest; +import lombok.extern.slf4j.Slf4j; +import org.springframework.util.StringUtils; +import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver; + +import java.util.Arrays; +import java.util.List; +import java.util.Locale; + +@Slf4j +public class BaseAcceptHeaderLocaleResolver extends AcceptHeaderLocaleResolver { + private final List LOCALES = Arrays.asList( + new Locale("ko"), + new Locale("ko", "KR"), + new Locale("en"), + new Locale("en", "US") + ); + private final String LOCALE_HEADER_KEY = "Accept-Language"; + + /** + * resolve locale 구성 + * + * @param request the request to resolve the locale for + * @return + */ + @Override + public Locale resolveLocale(HttpServletRequest request) { + if (!StringUtils.hasText(request.getHeader(LOCALE_HEADER_KEY))) { + return this.getDefaultLocale(); + } + try { + + Locale locale = LOCALES.stream().filter(_locale -> { + String defaultLocale = _locale.toString() + .replaceAll("-", "") + .replaceAll("_", "") + .toLowerCase(); + String headerLocale = request.getHeader(LOCALE_HEADER_KEY).toString() + .replaceAll("-", "") + .replaceAll("_", "") + .toLowerCase(); + return defaultLocale.equals(headerLocale); + }).findFirst().orElse(null); + + if (locale == null) { + locale = LOCALES.stream() + .filter(_locale -> { + String language = _locale.getLanguage(); + return request.getHeader(LOCALE_HEADER_KEY).contains(language); + }) + .map(_locale -> { + String language = _locale.getLanguage(); + return new Locale(language); + }) + .distinct() + .findFirst().orElse(Locale.KOREAN); + } + + log.info("{}, {}", request.getHeader(LOCALE_HEADER_KEY), locale); + return locale; + } catch (IllegalArgumentException e) { + log.warn("{}", e.getMessage()); + return this.getDefaultLocale(); + } + } +}