springboot-国际化(框架提供版本)

2024-06-18 19:48

本文主要是介绍springboot-国际化(框架提供版本),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

需要成员:

mesage.properties,message_zh_CN.properties_en_US.properties三个文件放在resource下。

三个文件内放的是显示的内容(key,value)

还需要 国际化解析器,国际化拦截器

解析器:

决定将解析器的信息设置在哪(国际化设置操作),有session,cookie,header(读取Accept-Language属性),fixed(固定区域,不常用)

如需要使用header,fix可直接配置applicationContext,session和cookie需要写配置类配置。

locale可改变获取的头部信息的属性,-resolver获取使用哪种解析器。以上说的四种解析器实际都实现了LocalResolver接口。所以要实现自定义的解析器也可以实现这个接口。

解析器有了现在还需要一个拦截器,拦截器的作用是设定属性设置区域和语言时传的参数是哪个参数。拦截参数,同时将参数放进session。

个人感觉,拦截器和解析器功能其实可以放在一起。拦截器在设置国际化信息时用到了解析器对象。

properties文件信息

msg=Spring MVC\u56FD\u9645\u5316
mess.user.name=\u7528\u6237\u540D  
mess.user.password=\u5BC6\u7801  
mess.user.btn=\u767B\u5F55  

 配置类:

package com.springboot.chapter10.main;import java.util.Locale;import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;@SpringBootApplication(scanBasePackages = "com.springboot.chapter10")
@MapperScan(basePackages = "com.springboot.chapter10", annotationClass = Mapper.class)
public class Chapter10Application implements WebMvcConfigurer {public static void main(String[] args) {SpringApplication.run(Chapter10Application.class, args);}// 国际化拦截器private LocaleChangeInterceptor lci = null;// 国际化解析器,请注意这个Bean Name要为localeResolver@Bean(name = "localeResolver")public LocaleResolver initLocaleResolver() {SessionLocaleResolver slr = new SessionLocaleResolver();// 默认国际化区域slr.setDefaultLocale(Locale.ENGLISH);return slr;}// 创建国际化拦截器@Beanpublic LocaleChangeInterceptor localeChangeInterceptor() {if (lci != null) {return lci;}lci = new LocaleChangeInterceptor();// 设置参数名lci.setParamName("language");return lci;}// 给处理器增加国际化拦截器@Overridepublic void addInterceptors(InterceptorRegistry registry) {// 拦截器在执行处理器前方法(preHandle)将请求的国际区域根据参数修改为对应的区域registry.addInterceptor(localeChangeInterceptor());}// @Override// public void addInterceptors(InterceptorRegistry registry) {// // // 注册拦截器到Spring MVC机制,然后它会返回一个拦截器注册// // InterceptorRegistration ir = registry.addInterceptor(new Interceptor1());// // // 指定拦截匹配模式,限制拦截器拦截请求// // ir.addPathPatterns("/interceptor/*");//// // 注册拦截器到Spring MVC机制中// InterceptorRegistration ir = registry.addInterceptor(new// MulitiInterceptor1());// // 指定拦截匹配模式// ir.addPathPatterns("/interceptor/*");// // 注册拦截器到Spring MVC机制中// InterceptorRegistration ir2 = registry.addInterceptor(new// MulitiInterceptor2());// // 指定拦截匹配模式// ir2.addPathPatterns("/interceptor/*");// // 注册拦截器到Spring MVC机制中// InterceptorRegistration ir3 = registry.addInterceptor(new// MulitiInterceptor3());// // 指定拦截匹配模式// ir3.addPathPatterns("/interceptor/*");// }
}

jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form"%>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html xmlns:th="http://www.thymeleaf.org">  
<head>
<title>Spring MVC国际化</title>
</head>
<body><!-- 通过HTTP请求参数变化国际化 --><a href="./page?language=zh_CN">简体中文</a><a href="./page?language=en_US">美国英文</a><h2><!-- 找到属性文件变量名为welcome的配置 --><spring:message code="msg" /><div><div><spring:message code="mess.user.name" />:<input th:placeholder="<spring:message code="mess.user.name" />" /></div></div><div><div><spring:message code="mess.user.password" />:<input th:placeholder="<spring:message code="mess.user.password" />" /></div></div><div><div><button><spring:message code="mess.user.btn" /></button></div></div></h2><!-- 当前国际化区域 -->Locale: ${pageContext.response.locale }
</body>
</html>

 

这篇关于springboot-国际化(框架提供版本)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1072946

相关文章

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

Spring Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为