-
spring 다국어처리 2가지 session, Accept-LanguageIT/spring 2021. 2. 8. 16:02
session
@Configuration @EnableConfigurationProperties(ProjectProperties.class) public class CmsWebMvcConfigurerAdapter implements WebMvcConfigurer { ... @Bean public LocaleResolver localeResolver() { SessionLocaleResolver slr = new SessionLocaleResolver(); slr.setDefaultLocale(Locale.US); return slr; } public LocaleChangeInterceptor localeChangeInterceptor() { LocaleChangeInterceptor lci = new LocaleChangeInterceptor(); lci.setParamName(AnonsController.LANG_CHANGE_PARAM_NAME); return lci; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()).addPathPatterns(AnonsController.URI_PREFIX + AnonsController.LANG_CHANGE_URI); } ... }
@Slf4j @RestController @RequestMapping(AnonsController.URI_PREFIX) public class AnonsController { public static final String URI_PREFIX = CmsWebSecurityConfigurerAdapter.ANON_PATH; public static final String LANG_CHANGE_URI = "/langs"; public static final String LANG_CHANGE_PARAM_NAME = "lang"; ... // 인터셉터에서 lang으로 들어오면 자동으로 변경해준다. @GetMapping(value = LANG_CHANGE_URI) public Map<String, String> i18n(@RequestParam(LANG_CHANGE_PARAM_NAME) String lang) { return anonService.i18n(lang); } }
Accept-Language
@Configuration @EnableWebMvc @EnableConfigurationProperties(ProjectProperties.class) @EnableScheduling @EnableTransactionManagement public class ApiWebMvcConfigurerAdapter implements WebMvcConfigurer { ... @Bean public LocaleResolver localeResolver() { AcceptHeaderLocaleResolver slr = new AcceptHeaderLocaleResolver(); slr.setDefaultLocale(Locale.US); return slr; } }
'IT > spring' 카테고리의 다른 글
Intellij spring entity jpa Persistence 기능 Diagram (0) 2021.02.08 spring method argumentResolvers (controller 파라미터 맵핑) (0) 2021.02.08 logback 5분 마다 logfile 생성하기 (0) 2021.02.05 spring boot swagger setting (0) 2021.02.05 spring boot oaauth2.0 (링크 정리 공유) (0) 2021.02.04