spring專題---第三部分MVC---國際化

在這裏插入圖片描述

本篇總結內容如下:

    前言
    操作步驟
      •native2ascii編碼
    總結
    分享與交流

前言

    spring MVC提供的功能衆多,其中有一個國際化功能,所謂國際化,就是同一個程序在不同語言設置的瀏覽器中,顯示不同的語言,但內容一樣。這樣的功能在當前網站中應用廣泛,特別是政府性網站,面向全球性的網站,不同語言設置的網站讓信息連接國內外。拿QQ註冊網站來說👇
在這裏插入圖片描述
在這裏插入圖片描述

操作步驟

    下面我們通過一個小案例一起學習一下如何實現spring MVC下的國際化👇
首先spring MVC環境要搭建好,這裏就不再說明了😄
(1)配置springmvc.xml

    <context:component-scan base-package="cn.lxy.controller"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>


    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:language"/>
        <property name="useCodeAsDefaultMessage" value="true"/>
    </bean>

    <mvc:interceptors>
        <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="lang"/>
        </bean>
    </mvc:interceptors>

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>

(2)在resources根目錄下創建language_zh_CN.properties和language_en_US.properties,分別代表兩種不同語言

language.cn= \u4e2d\u6587
language.en= English
info=\u767b\u5f55
username=\u7528\u6237\u540d
password=\u5bc6\u7801
tel=\u7535\u8bdd
email=\u90ae\u7bb1
submit=\u63d0\u4ea4
reset=\u91cd\u7f6e
#language.cn= 中文
#language.en= English
#info=登錄
#username=用戶名
#password=密碼
#tel=電話
#email=郵箱
#submit=提交
#reset=重置
language.cn= \u6d93\ue15f\u6783
language.en= English
info=login
username=username
password=password
tel=tel
email=email
submit=submit
reset=reset

(3)創建Handler類

@Controller
@RequestMapping("/zn_en")
public class CNtoEN {

    @RequestMapping("/test")
    public String test(){
        System.out.println("aa");
        return "zn_en";
    }
}

(4)創建zn_en.jsp

<form action="/zn_en/test" method="post">
    <spring:message  code="username"/>:
    <input type="text"/><br/>
    <spring:message code="password"/>:
    <input type="password"><br/>
    <spring:message code="tel"/>:
    <input type="text"><br/>
    <spring:message code="email"/>
    <input type="text"/><br/>
    <input type="submit" value="<spring:message code="submit"/>"/>
    <input type="reset" value="<spring:message code="reset"/>"/>
</form>

(5)測試
在這裏插入圖片描述
在這裏插入圖片描述

native2ascii編碼

    我們在寫language_zh_CN.properties的時候,不是直接在配置文件裏邊寫中文漢字的,需要經過native2ascii編碼,native2ascii編碼在jdk安裝目錄bin目錄下
在這裏插入圖片描述
    我們可以創建一個txt文件,在txt文件中輸入想要轉碼的內容,用dos命令輸入👇
在這裏插入圖片描述
    test.txt表示要轉碼的文件,aftertest.txt表示轉碼後的文件
在這裏插入圖片描述
    注意命令中定要加上-encoding UTF-8,否則轉碼後頁面展示中文亂碼

總結

    國際化操作,使得同一個軟件系統以不同的語言展示給不同國家的人。通過使用spring MVC的國際化操作,我們只需快速配置文件即可完成相關業務。

分享與總結

    由於能力有限,博客總結難免有不足,還請大佬們不吝賜教😄

發佈了31 篇原創文章 · 獲贊 30 · 訪問量 6502
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章