요청 스코프 언어 컨텍스트와 인터셉터 추가
- Interceptor에서 Accept-Language 헤더를 파싱 - 요청 단위 LangContext에 언어 정보 저장 - 서비스 및 예외 처리 계층에서 언어 컨텍스트 주입 - enum 및 when 기반 언어 정책을 한 곳으로 통합
This commit is contained in:
@@ -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(
|
||||
|
||||
22
src/main/kotlin/kr/co/vividnext/sodalive/i18n/Lang.kt
Normal file
22
src/main/kotlin/kr/co/vividnext/sodalive/i18n/Lang.kt
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
15
src/main/kotlin/kr/co/vividnext/sodalive/i18n/LangContext.kt
Normal file
15
src/main/kotlin/kr/co/vividnext/sodalive/i18n/LangContext.kt
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user