springboot-项目-国际化

 

首页控制

当index.html文件放到templates目录下面时,需要通过controller访问。

增加一个配置类,继承接口WebMvcConfigurer ,实现addViewController()方法:

满足http://localhost:8080/index.html 可以访问。

http://localhost:8080/  这个其实不用,默认首页就是这样访问的。

package com.wang.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcWebConfig implements WebMvcConfigurer {


    /**
     * 首页控制
     * 满足"/"和indext.html 都能访问到index.html去
     * @param registry
     */

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
    }
}

 

 

国际化:

thymeleaf

#{} 取国际化的值

+可以连接字符串

<h4 class="font-weight-bold mb-0 d-none d-md-block mt-1" th:text="#{index.welcome}+'Brandon Haynes'"> </h4>

步骤:

1.i18n配置文件

2.国际化引用

  thymeleaf中使用#{}引用国际化配置文件中的变量

3.国际化传参

前台选择语言,需要传递给后台。


                <div class="text-center mt-4 font-weight-light">
                  <a th:href="@{/index.html(lang='zh_CN')}" class="auth-link text-black margin">中文</a>
                  <a th:href="@{/index.html(lang='en_US')}" class="auth-link text-black margin">English</a>
                </div>

4.自定义locale bean

locale  bean,实现LocaleResolver接口,重写resolveLocale()

package com.wang.config;

import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;


public class MyLocaleResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        String lang = httpServletRequest.getParameter("lang");
        System.out.println(lang);
        Locale locale = Locale.getDefault();
        if(!StringUtils.isEmpty(lang)){
            String[] strings = lang.split("_");
//            new Locale(strings[1],strings[0])
            locale = new Locale(strings[0],strings[1]);

        }
        System.out.println(locale);
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}

 

调试的时候,我这里遇到一个问题,就是国际化不生效,一直没有进入我自己定义的Locale解析。也没有任何报错。这里在使用@Configuration注解注入对象时,方法名很有讲究:应该是接口名首字母小写,不能自己随意定义:

package com.wang.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcWebConfig implements WebMvcConfigurer {


    /**
     * 首页控制
     * 满足"/"和indext.html 都能访问到index.html去
     * @param registry
     */

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
    }

//    这里的方法名称不能随意写:必须是localeResolver
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }
}

效果:

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值