码迷,mamicode.com
首页 > 编程语言 > 详细

springboot实现自定义国际化

时间:2020-03-14 15:05:04      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:basename   解析   nts   配置   创建   resource   message   文件   always   

1.编写国际化配置文件,抽取页面需要显示的国际化消息,以登录页面为例,在resources下创建目录i18n/login,创建目录login.properties,login_zh_CN.properties,login_en_US.properties

2.SpringBoot自动配置好了管理国际化资源文件的组件;配置前缀spring.messages.basename=i18n.login

@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(name = AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME, search = SearchStrategy.CURRENT)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Conditional(ResourceBundleCondition.class)
@EnableConfigurationProperties
public class MessageSourceAutoConfiguration {

    private static final Resource[] NO_RESOURCES = {};

    @Bean
    @ConfigurationProperties(prefix = "spring.messages")
    public MessageSourceProperties messageSourceProperties() {
        return new MessageSourceProperties();
    }

    @Bean
    public MessageSource messageSource(MessageSourceProperties properties) {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        if (StringUtils.hasText(properties.getBasename())) {
            messageSource.setBasenames(StringUtils
                    .commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(properties.getBasename())));
        }
        if (properties.getEncoding() != null) {
            messageSource.setDefaultEncoding(properties.getEncoding().name());
        }
        messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
        Duration cacheDuration = properties.getCacheDuration();
        if (cacheDuration != null) {
            messageSource.setCacheMillis(cacheDuration.toMillis());
        }
        messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
        messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
        return messageSource;
    }
}

webMvc自动配置类中注入了,默认的就是根据请求头带来的区域信息获取Locale进行国际化

@Bean
        @ConditionalOnMissingBean
        @ConditionalOnProperty(prefix = "spring.mvc", name = "locale")
        public LocaleResolver localeResolver() {
            if (this.mvcProperties.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {
                return new FixedLocaleResolver(this.mvcProperties.getLocale());
            }
            AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
            localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
            return localeResolver;
        }

其实到这个地方已经能够根据浏览器的默认语言自动切换国际化了

下面我们自定义区域解析器,这里是根据你的定制规则实现的,我的定制规则是将国际化信息放在了请求中

切换国际化的两个按钮
<
a class="btn btn-sm" th:href="@{/index.html(l=‘zh_CN‘)}">中文</a> <a class="btn btn-sm" th:href="@{/index.html(l=‘en_US‘)}">English</a>
public class MyLocaleResolver implements LocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String l = request.getParameter("l");
        Locale locale = Locale.getDefault();
        if(!StringUtils.isEmpty(l)){
            String[] split = l.split("_");
            locale = new Locale(split[0],split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}

最后将自定义的解析器注入spring中

@Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }

 

springboot实现自定义国际化

标签:basename   解析   nts   配置   创建   resource   message   文件   always   

原文地址:https://www.cnblogs.com/vegeta-xiao/p/12491865.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!