International multilingual configuration

The static multilingual configuration file is placed in the Resource Bundle under the resources directory and configured in the form of key=value

For example, in Chinese: messages ABCD cn.properties

Property1 = property1
Property2 = property2

English:

property1=property one
property2=property two

Configuration Internationalization:

@Configuration
public class I18Interceptor extends WebMvcConfigurationSupport {
    //Set the default directory i18n if the property file is not read
    @Value(value = "${spring.messages.baseFolder:i18n}")
    private String basename;

    @Bean
    public ResourceBundleMessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename(basename);
        return messageSource;
    }

    /**
     * Inheriting the WebMvcConfigurationSupport will cause the automatic configuration to fail and the static resources under static cannot be accessed. Here, you need to manually configure the
     *
     * @param
     * @return
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }

    /**
     * Add interceptor
     *
     * @param
     * @return
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }

    /**
     * Construct an international interceptor instance
     *
     * @param
     * @return
     */
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        localeChangeInterceptor.setParamName("lang");//Set the request parameter field representing local, default to local
        return localeChangeInterceptor;
    }

    /**
     * Constructing a session region using local parsing strategy
     *
     * @param
     * @return
     */
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        localeResolver.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
        return localeResolver;
    }
}

Get the content of the corresponding language version:

@Autowired
private MessageSource messageSource;
public String getMessage(String attrName) {
    return messageSource.getMessage(attrName, null, "", LocaleContextHolder.getLocale());
}

Note: after springboot2.0, the configuration class inherits the WebMvcConfigurerAdapter class, which is out of date. Then it inherits the WebMvcConfigurationSupport class instead. However, after inheriting this class, the automatic configuration fails to access the resources under the static directory. You need to manually configure the static resource path again

Keywords: Programming Spring Session

Added by ron814 on Tue, 05 May 2020 22:44:36 +0300