ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • spring 다국어처리 2가지 session, Accept-Language
    IT/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;
        }
        
    }

    댓글

Designed by Tistory.