springboot--国际化

参考博客和自己犯的一些错或者要用到的知识点
参考博客
https://www.dalaoyang.cn/article/10
https://blog.csdn.net/caychen/article/details/80218898
https://blog.csdn.net/liujun03/article/details/82775634(超链接加入)
依靠浏览器的语言转化:
1 网页中文显示乱码(将配置文件的编码设置为utf-8)
https://blog.csdn.net/NOTEXCEPTION/article/details/96862409
超链接转化:
2@bean
产生这个Bean对象的方法Spring只会调用一次,随后这个Spring将会将这个Bean对象放在自己的IOC容器中。
未指定bean 的名称,默认采用的是 “方法名” + "首字母小写"的配置方式
https://www.cnblogs.com/cxuanBlog/p/11179439.html

3java.lang.IllegalArgumentException: Attribute name cannot be null or empty

中的th:后面不能有空格!!!

4 ViewControllerRegistry的用法
https://blog.csdn.net/lzm13462249457/article/details/80241239

一,先看需要自己创建的文件:在这里插入图片描述
二,步骤

1,先整合thymeleaf 整合thymeleaf
两点注意:
1注意前缀后面必须加/
spring.thymeleaf.prefix=classpath:/templates/
2 引用modelmap中的键值对时应该写成

而不是

${msg}

2,创建三个配置文件
messages.properties
messages_en_US.properties
messages_zh_CH.properties

文件内容如下:
messages.peroperties:

message = 你好

messages_en_US.properties:

message = hello

messages_zh_CH.properties:

message = 你好~

表示默认状态下为你好,英文状态下为hello,中文状态下为你好~

3,实现LocaleResolve接口

LocaleResolve.java

package cn.cxc.demo.component;

import org.springframework.web.servlet.LocaleResolver;

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

/**
 * @author chenxiaochong
 * @date 2019/10/23 11:27
 */
public class MyLocaleResolver implements LocaleResolver {
    /**
     * 获取前端传来的国际化信息(语言和国家)
     * @param httpServletRequest
     * @return locale
     */
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {

        String lang = httpServletRequest.getParameter("lang");
        System.out.println(lang);
        Locale locale = Locale.getDefault();
        if (lang != null) {
            try {
                String[] temp = lang.split("_");
                locale = new Locale(temp[0],temp[1]);
            } catch (Exception e) {
                System.out.println("错误信息:"+ e);
            }
        }
        return locale;
    }

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

    }
}

4,在配置文件MyConfig.java中将localeResolve注册到容器中

package cn.cxc.demo.config;

import cn.cxc.demo.component.MyLocaleResolver;
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;


/**
 * @author chenxiaochong
 * @date 2019/10/23 11:25
 */
@Configuration
public class MyConfig implements WebMvcConfigurer {

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

    /**
     * 覆盖掉默认的localeResolver
     * @return
     */
    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }

}

上面用到了ViewControllerRegistry
详细解释见:ViewControllerRegister

5,最后前端代码
lang2.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="UTF-8">
    <title>超链接国际化</title>
</head>

<body>
<h1 th:text = "#{message}"></h1>
<a class="btn btn-sm" th:href="@{/lang2.html(lang='zh_CH')}">中文</a>
<a class="btn btn-sm" th:href="@{/lang2.html(lang='en_US')}">English</a>
</body>

</html>

配置文件application.properties的代码也附上

#前缀
spring.thymeleaf.prefix=classpath:/templates/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值