diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/configs/WebConfig.kt b/src/main/kotlin/kr/co/vividnext/sodalive/configs/WebConfig.kt index 6c38c9c..6ef72f9 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/configs/WebConfig.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/configs/WebConfig.kt @@ -1,11 +1,19 @@ package kr.co.vividnext.sodalive.configs +import kr.co.vividnext.sodalive.i18n.LangInterceptor import org.springframework.context.annotation.Configuration import org.springframework.web.servlet.config.annotation.CorsRegistry +import org.springframework.web.servlet.config.annotation.InterceptorRegistry import org.springframework.web.servlet.config.annotation.WebMvcConfigurer @Configuration -class WebConfig : WebMvcConfigurer { +class WebConfig( + private val langInterceptor: LangInterceptor +) : WebMvcConfigurer { + override fun addInterceptors(registry: InterceptorRegistry) { + registry.addInterceptor(langInterceptor).addPathPatterns("/**") + } + override fun addCorsMappings(registry: CorsRegistry) { registry.addMapping("/**") .allowedOrigins( diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/i18n/Lang.kt b/src/main/kotlin/kr/co/vividnext/sodalive/i18n/Lang.kt new file mode 100644 index 0000000..d40abe3 --- /dev/null +++ b/src/main/kotlin/kr/co/vividnext/sodalive/i18n/Lang.kt @@ -0,0 +1,22 @@ +package kr.co.vividnext.sodalive.i18n + +import java.util.Locale + +enum class Lang(val code: String, val locale: Locale) { + KO("ko", Locale.KOREAN), + EN("en", Locale.ENGLISH), + JA("ja", Locale.JAPANESE); + + companion object { + fun fromAcceptLanguage(header: String?): Lang { + if (header.isNullOrBlank()) return KO + val two = header.trim().lowercase().take(2) // 앱은 2자리만 보내지만 안전하게 처리 + return when (two) { + "ko" -> KO + "en" -> EN + "ja" -> JA + else -> KO + } + } + } +} diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/i18n/LangContext.kt b/src/main/kotlin/kr/co/vividnext/sodalive/i18n/LangContext.kt new file mode 100644 index 0000000..61bd91b --- /dev/null +++ b/src/main/kotlin/kr/co/vividnext/sodalive/i18n/LangContext.kt @@ -0,0 +1,15 @@ +package kr.co.vividnext.sodalive.i18n + +import org.springframework.stereotype.Component +import org.springframework.web.context.annotation.RequestScope + +@Component +@RequestScope +class LangContext { + var lang: Lang = Lang.KO + internal set + + fun setLang(lang: Lang) { + this.lang = lang + } +} diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/i18n/LangInterceptor.kt b/src/main/kotlin/kr/co/vividnext/sodalive/i18n/LangInterceptor.kt new file mode 100644 index 0000000..8e06329 --- /dev/null +++ b/src/main/kotlin/kr/co/vividnext/sodalive/i18n/LangInterceptor.kt @@ -0,0 +1,21 @@ +package kr.co.vividnext.sodalive.i18n + +import org.springframework.stereotype.Component +import org.springframework.web.servlet.HandlerInterceptor +import javax.servlet.http.HttpServletRequest +import javax.servlet.http.HttpServletResponse + +@Component +class LangInterceptor( + private val langContext: LangContext +) : HandlerInterceptor { + override fun preHandle( + request: HttpServletRequest, + response: HttpServletResponse, + handler: Any + ): Boolean { + val acceptLanguage = request.getHeader("Accept-Language") + langContext.setLang(Lang.fromAcceptLanguage(acceptLanguage)) + return true + } +}