Table of Contents
1. Create resource files
3. Call internationalization
in the jsp file header Change the content at the jsp HTML display content, such as
Add produces ="application/json; charset=utf-8"
5 中英文切换
5.1 资源文件的中英文切换
5.2 easyui框架的中英文切换
Home Web Front-end HTML Tutorial Spring MVC and easyui internationalization_html/css_WEB-ITnose

Spring MVC and easyui internationalization_html/css_WEB-ITnose

Jun 24, 2016 pm 12:02 PM
easyui mvc spring globalization

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>
Copy after login

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" %>
Copy after login
Copy after login

or

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

changed to

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

3.2 Call internationalization in javascript

<input type="text" value="welcome" />
Copy after login
Add

<🎜 in the jsp file header >

Change

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

to

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

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

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

$.messager.alert('info');
Copy after login

3.3 Calling internationalization in java

Use

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

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");}
Copy after login
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}"/>
Copy after login

Then I found that most interfaces have added this sentence

<%@ page language="java" contentType="text /html; charset=UTF-8" pageEncoding="UTF-8"%>

<property name="url" value="${jdbc.url}?useUnicode=true&amp;characterEncoding=UTF-8"/>
Copy after login
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"

< in

🎜> 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)
Copy after login
Solution:

Format the return value of tree to

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

5 中英文切换

5.1 资源文件的中英文切换

这个比较简单,使用如下代码就行

<a href="?locale=en" onclick="changeLanguage('en')">English</a><a href="?locale=zh" onclick="changeLanguage('zh_CN')">简体中文</a>
Copy after login

5.2 easyui框架的中英文切换

easyui中有些框架(如datagrid)有些内容是框架自带的,需要在中加入

<script type="text/javascript" src="js/locale/easyui-lang-zh_CN.js"></script>
Copy after login

那如何切换中英文呢

首先不要在中添加上面这句,然后在主界面中加入如下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>
Copy after login

这是使用jquery加载js文件。

 

接下来将项目中需要替换的内容全部用步骤3中的方法替换就行了,这样国际化就完成了。

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

A new programming paradigm, when Spring Boot meets OpenAI A new programming paradigm, when Spring Boot meets OpenAI Feb 01, 2024 pm 09:18 PM

In 2023, AI technology has become a hot topic and has a huge impact on various industries, especially in the programming field. People are increasingly aware of the importance of AI technology, and the Spring community is no exception. With the continuous advancement of GenAI (General Artificial Intelligence) technology, it has become crucial and urgent to simplify the creation of applications with AI functions. Against this background, "SpringAI" emerged, aiming to simplify the process of developing AI functional applications, making it simple and intuitive and avoiding unnecessary complexity. Through "SpringAI", developers can more easily build applications with AI functions, making them easier to use and operate.

Use Spring Boot and Spring AI to build generative artificial intelligence applications Use Spring Boot and Spring AI to build generative artificial intelligence applications Apr 28, 2024 am 11:46 AM

As an industry leader, Spring+AI provides leading solutions for various industries through its powerful, flexible API and advanced functions. In this topic, we will delve into the application examples of Spring+AI in various fields. Each case will show how Spring+AI meets specific needs, achieves goals, and extends these LESSONSLEARNED to a wider range of applications. I hope this topic can inspire you to understand and utilize the infinite possibilities of Spring+AI more deeply. The Spring framework has a history of more than 20 years in the field of software development, and it has been 10 years since the Spring Boot 1.0 version was released. Now, no one can dispute that Spring

What are the implementation methods of spring programmatic transactions? What are the implementation methods of spring programmatic transactions? Jan 08, 2024 am 10:23 AM

How to implement spring programmatic transactions: 1. Use TransactionTemplate; 2. Use TransactionCallback and TransactionCallbackWithoutResult; 3. Use Transactional annotations; 4. Use TransactionTemplate in combination with @Transactional; 5. Customize the transaction manager.

PHP MVC Architecture: Building Web Applications for the Future PHP MVC Architecture: Building Web Applications for the Future Mar 03, 2024 am 09:01 AM

Introduction In today's rapidly evolving digital world, it is crucial to build robust, flexible and maintainable WEB applications. The PHPmvc architecture provides an ideal solution to achieve this goal. MVC (Model-View-Controller) is a widely used design pattern that separates various aspects of an application into independent components. The foundation of MVC architecture The core principle of MVC architecture is separation of concerns: Model: encapsulates the data and business logic of the application. View: Responsible for presenting data and handling user interaction. Controller: Coordinates the interaction between models and views, manages user requests and business logic. PHPMVC Architecture The phpMVC architecture follows the traditional MVC pattern, but also introduces language-specific features. The following is PHPMVC

How to set transaction isolation level in Spring How to set transaction isolation level in Spring Jan 26, 2024 pm 05:38 PM

How to set the transaction isolation level in Spring: 1. Use the @Transactional annotation; 2. Set it in the Spring configuration file; 3. Use PlatformTransactionManager; 4. Set it in the Java configuration class. Detailed introduction: 1. Use the @Transactional annotation, add the @Transactional annotation to the class or method that requires transaction management, and set the isolation level in the attribute; 2. In the Spring configuration file, etc.

Application of JUnit unit testing framework in Spring projects Application of JUnit unit testing framework in Spring projects Apr 18, 2024 pm 04:54 PM

JUnit is a widely used Java unit testing framework in Spring projects and can be applied by following steps: Add JUnit dependency: org.junit.jupiterjunit-jupiter5.8.1test Write test cases: Use @ExtendWith(SpringExtension.class) to enable extension, use @Autowired inject beans, use @BeforeEach and @AfterEach to prepare and clean, and mark test methods with @Test.

Building Multilingual Websites with PHP: Eliminating Language Barriers Building Multilingual Websites with PHP: Eliminating Language Barriers Feb 19, 2024 pm 07:10 PM

1. Prepare the database to create a new table for multilingual data, including the following fields: CREATETABLEtranslations(idINTNOTNULLAUTO_INCREMENT,localeVARCHAR(255)NOTNULL,keyVARCHAR(255)NOTNULL,valueTEXTNOTNULL,PRIMARYKEY(id)); 2. Set the language switching mechanism on the website Add a language switcher to the top or sidebar to allow users to select their preferred language. //Get the current language $current_locale=isset($_GET["locale"])?$_

Spring Security permission control framework usage guide Spring Security permission control framework usage guide Feb 18, 2024 pm 05:00 PM

In back-end management systems, access permission control is usually required to limit different users' ability to access interfaces. If a user lacks specific permissions, he or she cannot access certain interfaces. This article will use the waynboot-mall project as an example to introduce how common back-end management systems introduce the permission control framework SpringSecurity. The outline is as follows: waynboot-mall project address: https://github.com/wayn111/waynboot-mall 1. What is SpringSecurity? SpringSecurity is an open source project based on the Spring framework, aiming to provide powerful and flexible security for Java applications.

See all articles