1.建立资源文件
在webapp下建立文件夹language,在其中再添加file,命名分别为language.properties,language_en.properties,language_zh_CN.properties。其中language.properties为默认资源文件。
在其中添加内容,格式如下:
language.properties
welcome=Welcome
language_en.properties
welcome=Welcome
language_zh_CN.properties
welcome=\u6b22\u8fce
其中welcome为key,在jsp中调用使用的就是这个,汉语的需要使用unicode编码(在eclipse的properties文件中输入汉语会自动转换成unicode。\u6b22\u8fce-欢迎)
2.配置springMVC
在demo-servlet.xml中添加如下
<mvc:interceptors> <!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de --> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /></mvc:interceptors><!-- Saves a locale change using a session--><bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" /><!-- 国际化文件 --><bean id="messageSource"class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="/language/language" /> <property name="defaultEncoding" value="UTF-8"/></bean>
其中
3.调用国际化
3.1 在web中调用国际化
在jsp文件头添加
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
在jsp HTML显示内容处将内容更改,如
<a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true"> Welcome</a>
更改为
<a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true"> <spring:message code="welcome"/></a>
或
<input type="text" value="welcome" />
更改为
<input type="text" value='<spring:message code="welcome"/>' />
3.2 在javascript中调用国际化
在jsp文件头中添加
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
将
$.messager.alert('info');
更改为
$.messager.alert('<spring:message code="message.info"/>');
注意:1.
2.在_en文件中key的值不可以使用\或'符号。
3.3 在java中调用国际化
在controller中使用
if(!model.containsAttribute("contentModel")){ RequestContext requestContext = new RequestContext(request); String welcome = requestContext.getMessage("welcome");}
其中request为HttpServletRequest,model为Model。
4 解决乱码
4.1 中文输入乱码
完成了上面的步骤之后,会发现XCenter界面中输入汉语,点击确定,往数据库中写入汉语时,写入的是乱码。
解决办法如下:
在applicationContext.xml中的
<property name="url" value="${jdbc.url}"/>
更改为
<property name="url" value="${jdbc.url}?useUnicode=true&characterEncoding=UTF-8"/>
注意:不要在jdbc.properties中的jdbc.url=jdbc:mysql://localhost:3306/demo之后添加?useUnicode=true&characterEncoding=UTF-8,这样是不正确的。
4.2 部分界面显示中文乱码
接下来发现在大部分界面因为添加了这句
可以正常显示中文了,但是XCenter部分界面中文显示为???,经过检查发现是因为在controller中返回值为String,而且标记为@ResponseBody,查资料发现是因为Spring MVC中@ResponseBody默认编码是ISO-8859-1(其他的都是UTF-8,谁知道为什么同一个框架中这里要用另外一种编码)。
解决办法:
在
@RequestMapping(value = "welcome", method = RequestMethod.POST)
中添加produces = "application/json; charset=utf-8"
变成
@RequestMapping(value = "welcome", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
然后会发现在部分的tree中中文仍然显示为乱码,发现是因为返回值转换成JSON格式的原因。
解决办法:
tree的返回值格式化将
JSON.toJSONStringWithDateFormat(rs.getDefaultModel(), "yyyy-MM-dd HH:mm:ss");
更改为
JSON.toJSONString(rs.getDefaultModel());
5 中英文切换
5.1 资源文件的中英文切换
这个比较简单,使用如下代码就行
<a href="?locale=en" onclick="changeLanguage('en')">English</a><a href="?locale=zh" onclick="changeLanguage('zh_CN')">简体中文</a>
5.2 easyui框架的中英文切换
easyui中有些框架(如datagrid)有些内容是框架自带的,需要在
中加入
<script type="text/javascript" src="js/locale/easyui-lang-zh_CN.js"></script>
那如何切换中英文呢
首先不要在
中添加上面这句,然后在主界面中加入如下js语句
<script type="text/javascript"> var language=window.navigator.language; var userLanguage="${sessionScope['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE']}"; if(null != userLanguage&&userLanguage!=""){//not login language = userLanguage; } $(function(){ var src = 'js/locale' + '/easyui-lang-'+language.replace("-","_")+'.js';// when login in China the language=zh-CN $.getScript(src); });</script>
这是使用jquery加载js文件。
接下来将项目中需要替换的内容全部用步骤3中的方法替换就行了,这样国际化就完成了。

HTML、CSS和JavaScript在網頁開發中的作用分別是:1.HTML用於構建網頁結構;2.CSS用於美化網頁外觀;3.JavaScript用於實現動態交互。通過標籤、樣式和腳本,這三者共同構築了現代網頁的核心功能。

設置標籤的lang屬性是優化網頁可訪問性和SEO的關鍵步驟。 1)在標籤中設置lang屬性,如。 2)在多語言內容中,為不同語言部分設置lang屬性,如。 3)使用符合ISO639-1標準的語言代碼,如"en"、"fr"、"zh"等。正確設置lang屬性可以提高網頁的可訪問性和搜索引擎排名。

htmlattributeseresene forenhancingwebelements'functionalityandAppearance.TheyAdDinformationTodeFineBehavior,外觀和互動,使網站互動,響應式,visalalyAppealing.AttributesLikutesLikeSlikEslikesrc,href,href,href,類,類型,類型,和dissabledtransfransformformformformformformformformformformformformformformforment

toCreateAlistInHtml,useforforunordedlistsandfororderedlists:1)forunorderedlists,wrapitemsinanduseforeachItem,RenderingeringAsabulletedList.2)fororderedlists,useandfornumberedlists,useandfornumberedlists,casundfornumberedlists,casundfornthetthetthetthetthetthetthetttributefordforderfordforderforderentnumberingsnumberingsnumberingStys。

HTML用於構建結構清晰的網站。 1)使用標籤如、、定義網站結構。 2)示例展示了博客和電商網站的結構。 3)避免常見錯誤如標籤嵌套不正確。 4)優化性能通過減少HTTP請求和使用語義化標籤。

toinsertanimageIntoanhtmlpage,usethetagwithsrcandaltattributes.1)usealttextforAcccessibilityandseo.2)instementRcsetForresponSiveImages.3)applylazyloadingWithLoadingWithLoading =“ lazy” tooptimizeperformance.4)tooptimizeperformance.4)

HTML的核心目的在於讓瀏覽器理解並展示網頁內容。 1.HTML通過標籤定義網頁結構和內容,如、到、等。 2.HTML5增強了多媒體支持,引入了和標籤。 3.HTML提供了表單元素,支持用戶交互。 4.優化HTML代碼可提升網頁性能,如減少HTTP請求和壓縮HTML。

htmltagsareessentialforwebdevelopmentastheyandendenhancewebpages.1)semantictagsimproveaccessibilityandseo.2)semanteLayOut,語義和互動性。 3)poseriblesibilityandseoandseo.3)poseriblesoftagscanoftagscanoftagscanoptagscanoptimizeperefeneandimizeanDenSuroceRecRoscRoss-BrowserCrowserCercerComercompatibility。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

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

Atom編輯器mac版下載
最受歡迎的的開源編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Dreamweaver Mac版
視覺化網頁開發工具

禪工作室 13.0.1
強大的PHP整合開發環境