Thymeleaf及国际化使用

1.导入依赖

        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

2、看一下配置 xxxProperties

Thymeleaf及国际化使用_第1张图片
3.页面与controller




    
    Title


    

测试页面


@Controller
public class TestController {
    @GetMapping("/test")
    public String test(Model model){
        model.addAttribute("msg","Hello World");
        model.addAttribute("msg2","

hello,Thymeleaf

"); model.addAttribute("users",Arrays.asList("coding","qinjiang")); return "test"; } }

Thymeleaf及国际化使用_第2张图片
Thymeleaf语法
Thymeleaf及国际化使用_第3张图片我们还可以编写哪些表达式呢?
Variable Expressions: ${…}
获取一些基本的变量值! OGNL;

  1. 对象的属性,调用方法
  2. 使用内置的基本对象
${#ctx.locale} 
${param.foo} 
${session.foo} 
${application.foo} 
${#request.getAttribute('foo')} 
${#servletContext.contextPath}
  1. 工具对象
${#messages.msg('msgKey')} 
${#uris.escapePath(uri)} 
${#conversions.convert(object, 'java.util.TimeZone')}
${#dates.format(date, 'dd/MMM/yyyy HH:mm')} 
${#calendars.format(cal)} 
${#numbers.formatInteger(num,3)}
${#strings.toString(obj)} ${#arrays.toArray(object)}

Selection Variable Expressions: *{…} 选择表达式,和 ${} 是一样的;
Message Expressions: #{…} 国际化内容获取!
Link URL Expressions: @{…} URL表达式; th:href=“@{/login}”
Fragment Expressions: ~{…} 组件化表达式;
Literals (字面量)
Text literals: ‘one text’ , ‘Another one!’ ,… (字符串)
Text operations: (文本操作)
Arithmetic operations: (数学运算)
Boolean operations: (布尔运算)
Comparisons and equality: (比较运算)
Conditional operators: (条件运算符)
官网查询
在这里插入图片描述官网:https://www.thymeleaf.org/documentation.html
thymeleaf官网
github:https://github.com/thymeleaf/thymeleaf/blob/3.0-master

国际化

准备
Thymeleaf及国际化使用_第4张图片i18n : 国际化表示的文件夹
Thymeleaf及国际化使用_第5张图片Thymeleaf及国际化使用_第6张图片Thymeleaf及国际化使用_第7张图片Thymeleaf及国际化使用_第8张图片Thymeleaf及国际化使用_第9张图片Thymeleaf及国际化使用_第10张图片如果是一整个完整的页面(文档页)文章量十分大的时候,没有必要做这些细节化的国际化操作!
Thymeleaf及国际化使用_第11张图片配置文件
#国际化 spring.messages.basename=i18n.login
前端页面

Thymeleaf及国际化使用_第12张图片Thymeleaf及国际化使用_第13张图片Thymeleaf及国际化使用_第14张图片以Duubo官网为例:
中文:http://dubbo.apache.org/zh-cn/
英文:http://dubbo.apache.org/en-us/
可以发现是通过不同链接来实现国际化映射的!

@Bean
		@ConditionalOnMissingBean
		@ConditionalOnProperty(prefix = "spring.mvc", name = "locale")
		public LocaleResolver 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;
		}

在我们的接收请求解析中发现一个重写的方法:
我们自定义
1、修改前端页面的链接

 
中文 
English

2、处理这个请求! 我们是以参数的请求来进行处理的!我们去解析请求构造国际化对象即可

package com.shying.handler;


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 parameter = request.getParameter("l");
        // 如果我们没有配置就使用默认的! 
        Locale locale = Locale.getDefault();
        if (!StringUtils.isEmpty(parameter)){
            String[] split = parameter.split("_");
            //分析国家和地区
            locale=new Locale(split[0],split[1]);

        }
        return locale;
    }

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

    }
}

3、注册到IOC中

// id localeResolver 只能为这个,应为SpringBoot回去扫描并是识别 
   @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }

4、重启项目,点击测试即可

你可能感兴趣的:(Thymeleaf及国际化使用)