【第18章】spring-mvc之国际化(i18n)

2024-05-11 11:28
文章标签 java 国际化 spring 18 mvc i18n

本文主要是介绍【第18章】spring-mvc之国际化(i18n),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 前言
  • 一、准备
    • 1. resource
    • 2. 数据
  • 二、前端
    • 1.界面
    • 2.脚本
  • 三、后端
  • 四、效果
    • 1. 中文(默认)
    • 2.英文
  • 总结


前言

【第19章】spring-i8n

在mvc的基础上,我们可以通过界面完成浏览器和服务端的交互,可以更好地展示国际化功能;
本章节依旧以北京车展为案例。


一、准备

1. resource

在这里插入图片描述

2. 数据

在这里插入图片描述

数据文件已上传附件

二、前端

界面只展示关键部分

1.界面

<div id="smv_con_382_54" ctype="text" class="esmartMargin smartAbs "cpid="30536" cstyle="Style1" ccolor="Item2" areaid="Area0"iscontainer="False" pvid="con_1_35" tareaid="" re-direction="all"daxis="All" isdeletable="True"style="height: 85px; width: 761px; left: 11px; top: 385px; z-index: 1000012;"><div class="yibuFrameContent con_382_54  text_Style1  "style="overflow: hidden; z-index: 1;"><div id="txt_con_382_54" style="height: 100%; z-index: 1;"><div class="editableContent" id="txtc_con_382_54"style="height: 100%; overflow-wrap: break-word; z-index: 1;"><p style="z-index: 1;"><spanstyle="color: rgb(238, 238, 238); z-index: 1;"><strongstyle="z-index: 1;"><spanstyle="font-size: 40px; z-index: 1;"><span id="message_001"style="font-family: &quot;Source Han Sans&quot;, Geneva, sans-serif; z-index: 1;">2024(第十八届)北京国际汽车展览会</span></span></strong></span></p></div></div><script style="z-index: 1;">var tables = $(' #smv_con_382_54').find('table')for (var i = 0; i < tables.length; i++) {var tab = tables[i]var borderWidth = $(tab).attr('border')if (borderWidth <= 0 || !borderWidth) {console.log(tab)$(tab).addClass('hidden-border')$(tab).children("tbody").children("tr").children("td").addClass('hidden-border')$(tab).children("tbody").children("tr").children("th").addClass('hidden-border')$(tab).children("thead").children("tr").children("td").addClass('hidden-border')$(tab).children("thead").children("tr").children("th").addClass('hidden-border')$(tab).children("tfoot").children("tr").children("td").addClass('hidden-border')$(tab).children("tfoot").children("tr").children("th").addClass('hidden-border')}}</script></div>
</div>

2.脚本

<script type="text/javascript" style="z-index: 1;">$(function () {i18n("zh-CN");$('#smv_con_392_54').on("click", "li.w-language-item a", function () {var locale = $(this).attr("languageculture");i18n(locale);})$("#div_con_391_2").remove();});function i18n(locale){console.log(locale)$.ajax({url: '${pageContext.request.contextPath}/i18n',type: 'POST', // 或者 'POST', 'PUT' 等beforeSend: function(xhr) {xhr.setRequestHeader('accept-language', locale);},success: function(data) {console.log(data); // 打印返回的数据$("#message_001").text(data.message_001)// 创建新的子元素var newChild = $('<div id="txt_con_383_29" style="height: 100%; z-index: 1;">'+'    <div class="editableContent" id="txtc_con_383_29" style="height: 100%; overflow-wrap: break-word; z-index: 1;">'+'        <p style="text-align: justify; z-index: 1;"><span style="color: rgb(238, 238, 238); z-index: 1;"><span style="line-height: 1.75; z-index: 1;"><span style="font-size: 18px; z-index: 1;"><strong style="z-index: 1;"><span style="font-family: &quot;Source Han Sans&quot;, Geneva, sans-serif; z-index: 1;">'+data.message_002+'</span></strong></span></span></span></p>'+'<p style="text-align: justify; z-index: 1;"><span style="color: rgb(238, 238, 238); z-index: 1;"><span style="line-height: 1.75; z-index: 1;"><span style="font-size: 18px; z-index: 1;">'+data.message_003+'</span></span></span></p>'+'<p style="text-align: justify; z-index: 1;"><span style="color: rgb(238, 238, 238); z-index: 1;"><span style="line-height: 1.75; z-index: 1;"><span style="font-size: 18px; z-index: 1;">'+data.message_004+'</span></span></span></p>'+'<p style="text-align: justify; z-index: 1;"><span style="color: rgb(238, 238, 238); z-index: 1;"><span style="line-height: 1.75; z-index: 1;"><span style="font-size: 18px; z-index: 1;">'+data.message_005+'</span></span></span></p>'+'<p style="text-align: justify; z-index: 1;"><span style="color: rgb(238, 238, 238); z-index: 1;"><span style="line-height: 1.75; z-index: 1;"><span style="font-size: 18px; z-index: 1;">'+data.message_006+'</span></span></span></p>'+'<p style="text-align: justify; z-index: 1;">&nbsp;</p>'+'<p style="text-align: justify; z-index: 1;"><span style="color: rgb(238, 238, 238); z-index: 1;"><span style="line-height: 1.75; z-index: 1;"><span style="font-size: 18px; z-index: 1;"><strong style="z-index: 1;"><span style="font-family: &quot;Source Han Sans&quot;, Geneva, sans-serif; z-index: 1;">'+data.message_007+'</span></strong></span></span></span></p>'+'<p style="text-align: justify; z-index: 1;"><span style="color: rgb(238, 238, 238); z-index: 1;"><span style="line-height: 1.75; z-index: 1;"><span style="font-size: 18px; z-index: 1;">'+data.message_008+'</span></span></span></p>'+'<p style="text-align: justify; z-index: 1;"><span style="color: rgb(238, 238, 238); z-index: 1;"><span style="line-height: 1.75; z-index: 1;"><span style="font-size: 18px; z-index: 1;">'+data.message_009+'</span></span></span></p>'+'    </div>'+'</div>');$('#txt_con_383_29').remove();$('#smv_con_391_2 >.yibuFrameContent').append(newChild);},error: function(jqXHR, textStatus, errorThrown) {console.error('Error: ' + textStatus, errorThrown);}});}
</script>

三、后端

package org.example.springmvc.i18n;import com.alibaba.fastjson2.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;/*** Create by zjg on 2024/5/4*/
@RestController
public class I18nController {@RequestMapping("/i18n")public JSONObject i18n(HttpServletRequest request, AcceptHeaderLocaleResolver localeResolver){Locale locale = localeResolver.resolveLocale(request);ResourceBundle bundle = ResourceBundle.getBundle("autoshow", locale);Set<String> strings = bundle.keySet();JSONObject jsonObject=new JSONObject();strings.forEach((k)->{jsonObject.put(k,bundle.getString(k));});return jsonObject;}
}

四、效果

1. 中文(默认)

在这里插入图片描述

2.英文

在这里插入图片描述


总结

回到顶部
官方文档

大公司,有专门的前端同学,搞两套界面就可以了,扩展性更好,这种方式更适合前后端在一块的项目。
在一起,才是中国汽车,祝雷总大卖!

这篇关于【第18章】spring-mvc之国际化(i18n)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Java.lang.InterruptedException被中止异常的原因及解决方案

《Java.lang.InterruptedException被中止异常的原因及解决方案》Java.lang.InterruptedException是线程被中断时抛出的异常,用于协作停止执行,常见于... 目录报错问题报错原因解决方法Java.lang.InterruptedException 是 Jav

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We

java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)

《java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)》:本文主要介绍java中pdf模版填充表单踩坑的相关资料,OpenPDF、iText、PDFBox是三... 目录准备Pdf模版方法1:itextpdf7填充表单(1)加入依赖(2)代码(3)遇到的问题方法2:pd