搜尋
首頁Javajava教程SpringMVC學習系列(8) 之 國際化程式碼詳細介紹

在系列(7)中我們講了數據的格式化顯示,Spring在做格式化展示的時候已經做了國際化處理,那麼如何將我們網站的其它內容(如菜單、標題等)做國際化處理呢?這就是本篇要將的內容—>國際化。

一.基於瀏覽器請求的國際化實作:

首先配置我們專案的springservlet-config.xml檔案新增的內容如下:

<bean>
    <!-- 国际化信息所在的文件名 -->                     
    <property></property>   
    <!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称  -->               
    <property></property>           </bean>


在com.demo.web.controllers套件中加入GlobalController.java內容如下:

package com.demo.web.controllers;import java.util.Date;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.support.RequestContext;import com.demo.web.models.FormatModel;

@Controller
@RequestMapping(value = "/global")public class GlobalController {
    
    @RequestMapping(value="/test", method = {RequestMethod.GET})    public String test(HttpServletRequest request,Model model){        if(!model.containsAttribute("contentModel")){            
            //从后台代码获取国际化信息
            RequestContext requestContext = new RequestContext(request);
            model.addAttribute("money", requestContext.getMessage("money"));
            model.addAttribute("date", requestContext.getMessage("date"));

            
            FormatModel formatModel=new FormatModel();

            formatModel.setMoney(SpringMVC學習系列(8) 之 國際化程式碼詳細介紹SpringMVC學習系列(8) 之 國際化程式碼詳細介紹SpringMVC學習系列(8) 之 國際化程式碼詳細介紹SpringMVC學習系列(8) 之 國際化程式碼詳細介紹SpringMVC學習系列(8) 之 國際化程式碼詳細介紹.SpringMVC學習系列(8) 之 國際化程式碼詳細介紹78);
            formatModel.setDate(new Date());
            
            model.addAttribute("contentModel", formatModel);
        }        return "globaltest";
    }
    
}


這裡展示模型也用系列(7)中的作為演示。

在專案中的來源資料夾resources中新增messages.properties、messages_zh_CN.properties、messages_en_US.properties三個文件,其中messages.properties、messages_zh_CN.properties裡面的"money", "date",", "date",為中文,messages_en_US.properties裡面的為英文。

在views資料夾中新增globaltest.jsp視圖,內容如下:

nbsp;html PUBLIC "-//WSpringMVC學習系列(8) 之 國際化程式碼詳細介紹C//DTD HTML SpringMVC學習系列(8) 之 國際化程式碼詳細介紹.0SpringMVC學習系列(8) 之 國際化程式碼詳細介紹 Transitional//EN" "http://www.wSpringMVC學習系列(8) 之 國際化程式碼詳細介紹.org/TR/htmlSpringMVC學習系列(8) 之 國際化程式碼詳細介紹/loose.dtd"><meta><title>Insert title here</title>

    下面展示的是后台获取的国际化信息:<br>
    ${money}<br>
    ${date}<br>

    下面展示的是视图中直接绑定的国际化信息:<br>
    <message></message>:<br>
    <eval></eval><br>
    <message></message>:<br>
    <eval></eval><br>
    


執行測試:

SpringMVC學習系列(8) 之 國際化程式碼詳細介紹

更改瀏覽器語言順序,刷新頁面:

SpringMVC學習系列(8) 之 國際化程式碼詳細介紹

 

二.基於Session的國際化實現:

##在專案的springservlet-config.xml檔案新增的內容如下(第一種時新增的內容要保留):

<interceptors>  
    <!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 --> 
    <bean></bean>  </interceptors>  <bean></bean>


更改globaltest.jsp視圖為如下內容:

nbsp;html PUBLIC "-//WSpringMVC學習系列(8) 之 國際化程式碼詳細介紹C//DTD HTML SpringMVC學習系列(8) 之 國際化程式碼詳細介紹.0SpringMVC學習系列(8) 之 國際化程式碼詳細介紹 Transitional//EN" "http://www.wSpringMVC學習系列(8) 之 國際化程式碼詳細介紹.org/TR/htmlSpringMVC學習系列(8) 之 國際化程式碼詳細介紹/loose.dtd"><meta><title>Insert title here</title>
    <a>中文</a> | <a>英文</a><br>

    下面展示的是后台获取的国际化信息:<br>
    ${money}<br>
    ${date}<br>

    下面展示的是视图中直接绑定的国际化信息:<br>
    <message></message>:<br>
    <eval></eval><br>
    <message></message>:<br>
    <eval></eval><br>
    


更改GlobalController.java為下列內容:

package com.demo.web.controllers;import java.util.Date;import java.util.Locale;import javax.servlet.http.HttpServletRequest;import org.springframework.context.iSpringMVC學習系列(8) 之 國際化程式碼詳細介紹8n.LocaleContextHolder;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.servlet.iSpringMVC學習系列(8) 之 國際化程式碼詳細介紹8n.SessionLocaleResolver;import org.springframework.web.servlet.support.RequestContext;import com.demo.web.models.FormatModel;

@Controller
@RequestMapping(value = "/global")public class GlobalController {
    
    @RequestMapping(value="/test", method = {RequestMethod.GET})    public String test(HttpServletRequest request,Model model, @RequestParam(value="langType", defaultValue="zh") String langType){        if(!model.containsAttribute("contentModel")){            
            if(langType.equals("zh")){
                Locale locale = new Locale("zh", "CN"); 
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale); 
            }            else if(langType.equals("en")){
                Locale locale = new Locale("en", "US"); 
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
            }            else 
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());            
            //从后台代码获取国际化信息
            RequestContext requestContext = new RequestContext(request);
            model.addAttribute("money", requestContext.getMessage("money"));
            model.addAttribute("date", requestContext.getMessage("date"));

            
            FormatModel formatModel=new FormatModel();

            formatModel.setMoney(SpringMVC學習系列(8) 之 國際化程式碼詳細介紹SpringMVC學習系列(8) 之 國際化程式碼詳細介紹SpringMVC學習系列(8) 之 國際化程式碼詳細介紹SpringMVC學習系列(8) 之 國際化程式碼詳細介紹SpringMVC學習系列(8) 之 國際化程式碼詳細介紹.SpringMVC學習系列(8) 之 國際化程式碼詳細介紹78);
            formatModel.setDate(new Date());
            
            model.addAttribute("contentModel", formatModel);
        }        return "globaltest";
    }
    
}


執行測試:

SpringMVC學習系列(8) 之 國際化程式碼詳細介紹

SpringMVC學習系列(8) 之 國際化程式碼詳細介紹




############## #######三.基於Cookie的國際化實作:######把實作第二種方法時在專案的springservlet-config .xml檔案中新增的###
<bean></bean>
############註解掉,並新增以下內容:###
<bean></bean>
############更改GlobalController.java為如下內容:###
package com.demo.web.controllers;import java.util.Date;import java.util.Locale;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.context.iSpringMVC學習系列(8) 之 國際化程式碼詳細介紹8n.LocaleContextHolder;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.servlet.iSpringMVC學習系列(8) 之 國際化程式碼詳細介紹8n.CookieLocaleResolver;//import org.springframework.web.servlet.iSpringMVC學習系列(8) 之 國際化程式碼詳細介紹8n.SessionLocaleResolver;import org.springframework.web.servlet.support.RequestContext;import com.demo.web.models.FormatModel;

@Controller
@RequestMapping(value = "/global")public class GlobalController {
    
    @RequestMapping(value="/test", method = {RequestMethod.GET})    public String test(HttpServletRequest request, HttpServletResponse response, Model model, @RequestParam(value="langType", defaultValue="zh") String langType){        if(!model.containsAttribute("contentModel")){            
            /*if(langType.equals("zh")){
                Locale locale = new Locale("zh", "CN"); 
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale); 
            }
            else if(langType.equals("en")){
                Locale locale = new Locale("en", "US"); 
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
            }
            else 
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());*/
            
            if(langType.equals("zh")){
                Locale locale = new Locale("zh", "CN"); 
                //request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
                (new CookieLocaleResolver()).setLocale (request, response, locale);
            }            else if(langType.equals("en")){
                Locale locale = new Locale("en", "US"); 
                //request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
                (new CookieLocaleResolver()).setLocale (request, response, locale);
            }            else 
                //request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());
                (new CookieLocaleResolver()).setLocale (request, response, LocaleContextHolder.getLocale());            
            //从后台代码获取国际化信息
            RequestContext requestContext = new RequestContext(request);
            model.addAttribute("money", requestContext.getMessage("money"));
            model.addAttribute("date", requestContext.getMessage("date"));

            
            FormatModel formatModel=new FormatModel();

            formatModel.setMoney(SpringMVC學習系列(8) 之 國際化程式碼詳細介紹SpringMVC學習系列(8) 之 國際化程式碼詳細介紹SpringMVC學習系列(8) 之 國際化程式碼詳細介紹SpringMVC學習系列(8) 之 國際化程式碼詳細介紹SpringMVC學習系列(8) 之 國際化程式碼詳細介紹.SpringMVC學習系列(8) 之 國際化程式碼詳細介紹78);
            formatModel.setDate(new Date());
            
            model.addAttribute("contentModel", formatModel);
        }        return "globaltest";
    }
    
}
#########

运行测试:

SpringMVC學習系列(8) 之 國際化程式碼詳細介紹

SpringMVC學習系列(8) 之 國際化程式碼詳細介紹

关于bean id="localeResolver" class="org.springframework.web.servlet.iSpringMVC學習系列(8) 之 國際化程式碼詳細介紹8n.CookieLocaleResolver" />SpringMVC學習系列(8) 之 國際化程式碼詳細介紹个属性的说明(可以都不设置而用其默认值):

<bean>
    <!-- 设置cookieName名称,可以根据名称通过js来修改设置,也可以像上面演示的那样修改设置,默认的名称为 类名+LOCALE(即:org.springframework.web.servlet.iSpringMVC學習系列(8) 之 國際化程式碼詳細介紹8n.CookieLocaleResolver.LOCALE-->
    <property></property>
    <!-- 设置最大有效时间,如果是-SpringMVC學習系列(8) 之 國際化程式碼詳細介紹,则不存储,浏览器关闭后即失效,默认为Integer.MAX_INT-->
    <property>
    <!-- 设置cookie可见的地址,默认是“/”即对网站所有地址都是可见的,如果设为其它地址,则只有该地址或其后的地址才可见-->
    <property></property></property></bean>


四.基于URL请求的国际化的实现:

首先添加一个类,内容如下:

import java.util.Locale;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.DispatcherServlet;import org.springframework.web.servlet.LocaleResolver;public class MyAcceptHeaderLocaleResolver extends AcceptHeaderLocaleResolver {    private Locale myLocal;    public Locale resolveLocale(HttpServletRequest request) {        return myLocal;
    } 

    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
        myLocal = locale;
    }
  
}


然后把实现第二种方法时在项目的springservlet-config.xml文件中添加的

<bean></bean>


注释掉,并添加以下内容:

<bean></bean>


“xx.xxx.xxx”是刚才添加的MyAcceptHeaderLocaleResolver 类所在的包名。

保存之后就可以在请求的URL后附上 locale=zh_CN 或 locale=en_US 如 http://www.php.cn/ 来改变语言了,具体这里不再做演示了。


国际化部分的内容到此结束。

以上就是SpringMVC学习系列(8) 之 国际化代码详细介绍的内容,更多相关内容请关注PHP中文网(www.php.cn)!


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
使用Gin框架实现国际化和多语言支持功能使用Gin框架实现国际化和多语言支持功能Jun 23, 2023 am 11:07 AM

随着全球化的发展以及互联网的普及,越来越多的网站和应用开始致力于实现国际化和多语言支持功能,以满足不同人群的需求。为了实现这些功能,开发者需要使用一些先进的技术及框架。在本文中,我们将介绍如何使用Gin框架来实现国际化和多语言支持功能。Gin框架是一个轻量级的Web框架,由Go语言编写。它具有高效、易用和灵活等特点,已经成为了许多开发者的首选框架。除此之外,

SpringBoot与SpringMVC的比较及差别分析SpringBoot与SpringMVC的比较及差别分析Dec 29, 2023 am 11:02 AM

SpringBoot和SpringMVC都是Java开发中常用的框架,但它们之间有一些明显的差异。本文将探究这两个框架的特点和用途,并对它们的差异进行比较。首先,我们来了解一下SpringBoot。SpringBoot是由Pivotal团队开发的,它旨在简化基于Spring框架的应用程序的创建和部署。它提供了一种快速、轻量级的方式来构建独立的、可执行

使用FastAPI框架构建国际化的Web应用使用FastAPI框架构建国际化的Web应用Sep 29, 2023 pm 03:53 PM

使用FastAPI框架构建国际化的Web应用FastAPI是一个高性能的PythonWeb框架,它结合了Python类型注解和性能较好的异步支持,使得开发Web应用变得更加简单、快速和可靠。在构建一个国际化的Web应用时,FastAPI提供了方便的工具和理念,可以使得应用能够轻松支持多种语言。下面我将给出一个具体的代码示例,介绍如何使用FastAPI框架构

PHP8.0中的国际化库PHP8.0中的国际化库May 14, 2023 pm 05:51 PM

PHP8.0中的国际化库:UnicodeCLDR和Intl扩展随着全球化的进程,开发跨语言、跨地域的应用程序变得越来越普遍。国际化是实现这一目标的重要组成部分。在PHP8.0中,引入了UnicodeCLDR和Intl扩展,这两个组件都为开发者提供了更好的国际化支持。UnicodeCLDRUnicodeCLDR(CommonLocaleDat

如何使用Webman框架实现国际化和多语言支持?如何使用Webman框架实现国际化和多语言支持?Jul 09, 2023 pm 03:51 PM

如今,随着互联网技术的不断发展,越来越多的网站和应用程序需要支持多语言和国际化。在Web开发中,使用框架可以极大地简化开发过程。本文将介绍如何使用Webman框架实现国际化和多语言支持,同时提供了一些代码示例。一、什么是Webman框架?Webman是一个基于PHP的轻量级框架,提供了丰富的功能和易于使用的工具,用于开发Web应用程序。其中之一就是国际化和多

比较SpringBoot与SpringMVC的差异是什么?比较SpringBoot与SpringMVC的差异是什么?Dec 29, 2023 am 10:46 AM

SpringBoot与SpringMVC的不同之处在哪里?SpringBoot和SpringMVC是两个非常流行的Java开发框架,用于构建Web应用程序。尽管它们经常分别被使用,但它们之间的不同之处也是很明显的。首先,SpringBoot可以被看作是一个Spring框架的扩展或者增强版。它旨在简化Spring应用程序的初始化和配置过程,以帮助开发人

Vue开发中如何解决国际化语言切换问题Vue开发中如何解决国际化语言切换问题Jul 01, 2023 pm 01:33 PM

Vue开发中如何解决国际化语言切换问题引言:在如今的全球化时代,应用程序的国际化变得越来越重要。为了让不同地区的用户能够更好地使用应用程序,我们需要对内容进行本地化,以适应不同语言和文化环境。对于使用Vue进行开发的应用程序来说,国际化是一个重要的考虑因素。本文将介绍如何在Vue开发中解决国际化语言切换问题,以实现应用程序的多语言支持。一、国际化与本地化在开

Vue中如何使用路由实现国际化的多语言切换?Vue中如何使用路由实现国际化的多语言切换?Jul 22, 2023 pm 12:17 PM

Vue中如何使用路由实现国际化的多语言切换?在开发多语言网站时,我们的一个重要需求是能够根据用户选择的语言,实现网站内容的切换。Vue.js是一款流行的JavaScript框架,通过使用VueRouter插件,我们可以很方便地实现路由功能。在本文中,我将介绍如何使用路由实现Vue中的国际化多语言切换。首先,我们需要安装VueRouter插件。可以通过np

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前By尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具