search
HomeWeb Front-endHTML TutorialSpring MVC and easyui internationalization_html/css_WEB-ITnose

1. Create resource files

Create the folder language under the webapp, add file in it, and name it language.properties, language_en.properties, language_zh_CN.properties. Among them, language.properties is the default resource file.

Add content in it in the following format:

language.properties

welcome=Welcome

language_en.properties

welcome=Welcome

Language_zh_cn.properties

Welcome = U6b22u8fce

Among them, Welcome is key, and this is used in JSP. Properties file Entering Chinese into unicode will be automatically converted into unicode.

Among them, & lt; proprity name = "basenename" value = "/language/language"/& gt; value is the resource file, no suffix name.properties.

3. Call internationalization

3.1 Call internationalization in web

Add

<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>

in the jsp file header Change the content at the jsp HTML display content, such as

to

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>

or

<a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true">    Welcome</a>

changed to

<a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true">    <spring:message code="welcome"/></a>

3.2 Call internationalization in javascript

<input type="text" value="welcome" />
Add

Change

<input type="text" value='<spring:message code="welcome"/>' />

to

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>

Note: 1." as this will cause an error.

2. The key value in the _en file cannot use the or ' symbol.

$.messager.alert('info');

3.3 Calling internationalization in java

Use

$.messager.alert('<spring:message code="message.info"/>');

in the controller where request is HttpServletRequest and model is Model.

4 Solving Garbled Codes

4.1 Chinese Input Garbled Codes

After completing the above steps, you will find that you enter Chinese in the XCenter interface, click OK, and write Chinese into the database , what is written is garbled code.

if(!model.containsAttribute("contentModel")){    RequestContext requestContext = new RequestContext(request);    String welcome = requestContext.getMessage("welcome");}
The solution is as follows:

In applicationContext.xml, change

to

Note: Do not add ?useUnicode=true&characterEncoding=UTF-8 after jdbc.url=jdbc:mysql://localhost:3306/demo in jdbc.properties. This is incorrect.

4.2 Some interfaces display Chinese garbled characters

<property name="url" value="${jdbc.url}"/>

Then I found that most interfaces have added this sentence

<property name="url" value="${jdbc.url}?useUnicode=true&characterEncoding=UTF-8"/>
can display Chinese normally, but part of the XCenter interface displays Chinese as???. After inspection, it was found that it is because in the controller The return value is String and marked as @ResponseBody. After checking the information, I found that the default encoding of @ResponseBody in Spring MVC is ISO-8859-1 (others are UTF-8. Does anyone know why another one is used here in the same framework? coding).

Solution:

Add produces ="application/json; charset=utf-8"

🎜> becomes

Then you will find that the Chinese characters in some trees are still displayed as garbled characters. It is found that the return value is converted into JSON format.

@RequestMapping(value = "welcome", method = RequestMethod.POST)
Solution:

Format the return value of tree to

@RequestMapping(value = "welcome", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
to

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中的方法替换就行了,这样国际化就完成了。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

HTML's Purpose: Enabling Web Browsers to Display ContentHTML's Purpose: Enabling Web Browsers to Display ContentMay 03, 2025 am 12:03 AM

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

Why are HTML tags important for web development?Why are HTML tags important for web development?May 02, 2025 am 12:03 AM

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

Explain the importance of using consistent coding style for HTML tags and attributes.Explain the importance of using consistent coding style for HTML tags and attributes.May 01, 2025 am 12:01 AM

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

How to implement multi-project carousel in Bootstrap 4?How to implement multi-project carousel in Bootstrap 4?Apr 30, 2025 pm 03:24 PM

Solution to implement multi-project carousel in Bootstrap4 Implementing multi-project carousel in Bootstrap4 is not an easy task. Although Bootstrap...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.