国际化

1)创建文件夹i18n

在i18n文件下直接创建properties文件

格式:前面语言代码,后面国家代码

点中间下面Resource Bundle

spring boot国际化

 

中间左上角加号添加属性

第一个默认,第二个英文,第三个中文

Springboot自动配置好了国际化资源文件组件(MessageSourceAutoConfiguration)

    if (StringUtils.hasText(this.basename)) {
        //设置国际化资源文件的基础名字(去掉语言国家代码的)
            messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(
                    StringUtils.trimAllWhitespace(this.basename)));
        }

2)主配置文件

spring.messages.basename=i18n.login

3)前台:

    <body class="text-center">
        <form class="form-signin" action="dashboard.html" th:action="@{/user/login}" method="post">
            <img class="mb-4" th:src="@{/asserts/img/bootstrap-solid.svg}" src="asserts/img/bootstrap-solid.svg" alt="" width="72" height="72">
<!--            <img class="mb-4" th:src="@{/asserts/img/logo.png}" src="asserts/img/logo.png" alt="" width="72" height="72">-->
            <h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
            <!--判断-->
            <p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
            <label class="sr-only" th:text="#{login.username}">Username</label>
            <input type="text"  name="username" class="form-control" placeholder="Username" th:placeholder="#{login.username}" required="" autofocus="">
            <label class="sr-only" th:text="#{login.password}">Password</label>
            <input type="password" name="password" class="form-control" placeholder="Password" th:placeholder="#{login.password}" required="">
            <div class="checkbox mb-3">
                <label>
                    <input type="checkbox" value="remember-me"/> [[#{login.remember}]]
                </label>
            </div>
            <button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button>
            <p class="mt-5 mb-3 text-muted">© 2017-2018</p>
            <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>
        </form>
    </body>

行内表达式:

Input不是标签体,th:text只能在标签体内使用,所以用行内表达式[[#{login.remember}]]

 

4)创建类:

对前台数据分析并写自己的解析器:

package com.atguigu.springboot.component;
​
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
​
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
​
/**
 * 可以在连接上携带区域信息
 */
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) {
​
    }
}
​

 

5)让系统用我们写的组件

在myMVCConfig里面加上@Bean

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

myMVCConfig:

配置类;继承WebMvcConfigurerAdapter(过时:更换:implements WebMvcConfigurer)

@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
​
​

相关文章: