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

文章目录


前言

【第19章】spring-i8n

在mvc的基础上,我们可以通过界面完成浏览器和服务端的交互,可以更好地展示国际化功能;

本章节依旧以北京车展为案例。


一、准备

1. resource

2. 数据

数据文件已上传附件

二、前端

界面只展示关键部分

1.界面

html 复制代码
<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;"><span
                       style="color: rgb(238, 238, 238); z-index: 1;"><strong
                       style="z-index: 1;"><span
                       style="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.脚本

javascript 复制代码
<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>

三、后端

java 复制代码
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.英文


总结

回到顶部
官方文档

大公司,有专门的前端同学,搞两套界面就可以了,扩展性更好,这种方式更适合前后端在一块的项目。

在一起,才是中国汽车,祝雷总大卖!

相关推荐
蜗牛^^O^28 分钟前
Docker和K8S
java·docker·kubernetes
从心归零1 小时前
sshj使用代理连接服务器
java·服务器·sshj
一个诺诺前行的后端程序员2 小时前
springcloud微服务实战<1>
spring·spring cloud·微服务
IT毕设梦工厂2 小时前
计算机毕业设计选题推荐-在线拍卖系统-Java/Python项目实战
java·spring boot·python·django·毕业设计·源码·课程设计
Ylucius3 小时前
动态语言? 静态语言? ------区别何在?java,js,c,c++,python分给是静态or动态语言?
java·c语言·javascript·c++·python·学习
七夜zippoe3 小时前
分布式系统实战经验
java·分布式
是梦终空3 小时前
JAVA毕业设计176—基于Java+Springboot+vue3的交通旅游订票管理系统(源代码+数据库)
java·spring boot·vue·毕业设计·课程设计·源代码·交通订票
落落落sss3 小时前
sharding-jdbc分库分表
android·java·开发语言·数据库·servlet·oracle
码爸3 小时前
flink doris批量sink
java·前端·flink
Monodye4 小时前
【Java】网络编程:TCP_IP协议详解(IP协议数据报文及如何解决IPv4不够的状况)
java·网络·数据结构·算法·系统架构