基于SpringBoot的国际化网站处理

为了在不同得语言环境下使用不同的语言提示,所以一般会在网站引入网站国际化处理。下面讲解一下springboot对这国际化的支持

1.springboot进行国际化处理的类

public class MessageSourceAutoConfiguration {
    @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());
        }
        
        

上面的代码是springboot获取用户国际化配置的代码,springboot会读取默认配置文件,查看是否含有国际化路径配置,如果没有则使用默认的路径。默认路径为根路径下的message。

public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
            String basename = context.getEnvironment().getProperty("spring.messages.basename", "messages");
            ConditionOutcome outcome = (ConditionOutcome)cache.get(basename);
            if (outcome == null) {
                outcome = this.getMatchOutcomeForBasename(context, basename);
                cache.put(basename, outcome);
            }

            return outcome;
        }

springboot对国际化的配置文件存在默认的读取路径,则说明我们可以直接在根路径下创建国际化配置,这样springboot也可以读取。但是一般情况下不会这样做,为了更好的语义化,我们会提供不同界面不同的国际化配置路径。

2.根据浏览器的头信息改变界面语言环境

i18n===>代表的是internationalization,首字母+单词个数+尾字母。一般会将所有的国际化文件放置来这个文件夹中。下面演示登陆界面的国际化处理

在springboot的主配置文件里,配置spring.messages.basename=i18n.login(il8n是文件夹名字,login是国际化配置的文件名的前缀,对应的中文为:login_zh_CN.properties,英文为:login_en_US.properties

此时便可以在界面中引用对应的国际化配置了,当浏览器的语言环境切换的时候,界面的语言也会随之改变

5.2.2 主动改变界面语言环境

在通常的网页中,会提供一个国际化的切换标签,用来切换语言。

我们为切换语言的按钮提供一个区域参数,来决定要使用什么语言(使用了thymeleaf模板引擎),访问当前界面即可,此登陆界面使用了视图渲染,访问index.html在没有登陆信息的情况下相当于访问登陆界面

<a class="btn btn-sm" th:href="@{/index.html(lang='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(lang='en_US')}">English</a>

接下来,我们需要提供一个自定义的区域信息解析器,并将这个组件添加到容器中,让springboot来加载

public class MyLocaleResover implements LocaleResolver {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        String l = httpServletRequest.getParameter("lang");
        logger.info(l);
        //获取操作系统默认的区域信息
        Locale locale = Locale.getDefault();
        if(!StringUtils.isEmpty(l)){
            String[] s = l.split("_");
            locale = new Locale(s[0],s[1]);
        }
        return locale;
    }

具体的实现逻辑:获取传入的区域信息,判断是否为空,如果不为空,将 传入的区域信息封装为Locale对象进行返回,如果为空使用系统默认的区域信息或者获取浏览器默认的请求头。

httpServletRequest.getHeader("Accept-Language");获取浏览器默认请求头
zh-CN,zh;q=0.9,en;q=0.8  === 这是结果,需要进行切割,然后传入

最后将这个区域解析器添加到容器中

@Bean("localeResolver")
    public LocaleResolver getLocaleResolver(){
        return new MyLocaleResover();
    }

注:需要指定这个bean 的id为localResolver,不然springboot无法解析这个组件

猜你喜欢

转载自www.cnblogs.com/onlyzuo/p/12238367.html
今日推荐