spring的国际化1
国际化的实现i18n--错误码国际化以及在springboot项目中使用

国际化的实现i18n--错误码国际化以及在springboot项⽬中使⽤国际化,英⽂叫 internationalization 单词太长,⼜被简称为 i18n(取头取尾中间有18个字母);主要涉及3个类: Locale⽤来设置定制的语⾔和国家代码 ResourceBundle ⽤来加载国际化资源⽂件 MessageFormat ⽤来格式化占位符//先看结构:在创建国际化资源⽂件时,如我在resources⽂件下建了⼀个i18n⽂件夹,下放了3个资源⽂件,idea⾃动会⽣成⼀个Resource Bundle 'messages'⽂件夹,这个⽂件夹不是真实存在的,打开项⽬所在⽂件夹后,找不到它的;如图:资源⽂件的命名格式:前缀+"_"+"语⾔代码"+地区代码+".properites";(Locale[] locales = Locale.getAvailableLocales();//获取世界可⽤的语⾔地区表⽰)如上图所⽰,我定义了3个资源⽂件夹,其中⼀个没有语⾔代码和地区代码的那个是默认资源,也就是要找的资源⽂件都不存在时,就会去那⾥找;查找顺序,如果你指定Locale locale=new Locale("en_US"),那它就会找到messages_en_US.properties,如果你写错了⼀个单词Locale locale=new Locale("en_USAA");找不到的话,它就会找本地的语⾔(Locale locale2 = Locale.getDefault(); //获取默认语⾔地区),如我们在中国就会找本地的中国语⾔包messages_zh_CN.properties,如果没有本地的语⾔包messages_zh_CN.properties,就会去messages.properties这⾥找资源⽂件的内容格式为:key=value,其中value值可以含有占位符,格式{数字},例如" 你的订单号{0}的订单,⾦额{1}元";key和value值在properties⽂件中都不⽤加双引号注意:资源⽂件中的所有字符都必须是 ascll 码,不能保存为中⽂的,Java 中提供了 native2ascll ⼯具⽤于将中⽂转化为 ascll 码。
一个简单的示例在springboot中实现国际化

⼀个简单的⽰例在springboot中实现国际化最近在⽹上找了⼀个有关账单管理的spring boot项⽬,其中有⼀部分是涉及显⽰国际化信息的,即将页⾯上的中英⽂进⾏转换。
因为在这之前这部分内容没有接触过,所以在这记录下过程。
中⽂效果图如下所⽰:英⽂效果图如下所⽰:从上⾯两幅图可以看出在切换中英⽂时有五个部分的内容发送改变。
分别是:⽤户名(Username)、密码(Password)、记住我(Remember Me)、登录(Sign)、重置(Rest)。
第⼀部分、先在resources⽬录下新建⼀个i18n⽂件夹,并在该⽂件夹下新建⼀个Resource Bundle.如下图所⽰:并在跳出的弹框内写⼊以下信息:点击“OK”后就会在i18n⽬录下⽣成3个后缀名为“.properties”的⽂件。
如下所⽰:第⼆部分、分别在这三个⽂件中填⼊相应信息。
login.properties表⽰默认显⽰的信息。
login.password、login.remember、login.reset、login.submit、ername是⾃⼰设置的key值,⽤于在HTML中显⽰。
后⾯的是将要显⽰的内容。
1 login.password=密码2 login.remember=记住我3 login.reset=重置4 login.submit=登录5 ername=⽤户名login_en_US.properties1 login.password=Password2 login.remember=Remember Me3 login.reset=Rest4 login.submit=Sign5 ername=Usernamelogin_zh_CN.properties1 login.password=密码2 login.remember=记住我3 login.reset=重置4 login.submit=登录5 ername=⽤户名第三部分、在HTML相应位置填⼊key值,并在点击“中⽂”或“English”发出不同请求信息。
Spring中的国际化Message的简单例子(ApplicationContext)

Spring中的国际化Message的简单例子(ApplicationContext)首先,建立一个描述message的XML文件,名为messages.xml<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "/dtd/spring-beans.dtd"><beans><!-- 资源国际化测试 --><beanid="messageSource" class="org.springframework.context.s upport.ReloadableResourceBundleMessageSource"> <prop erty name="basenames"><list><value>org/rjstudio/spring/properties/messages</value> </list></property></bean></beans>这个Bean的id是定死的,只能为“messageSource”。
这里的Class需要填入MessageSource接口的实现。
其中,在我看的书中只提及了两个类,一个是:ResourceBundleMessageSource,另一个则是ReloadableResourceBundleMessageSource。
其中,后者提供了无需重启就可重新加载新配置的特性。
list节点的value子节点中的body值“org/rjstudio/spring/properties/messages”,是指org.rjstudio.spring.proerties包下的以messages为主要名称的properties文件。
Java框架的国际化支持

Java框架的国际化支持简介:随着全球化的发展,软件产品越来越面临多语言、多文化环境的挑战。
Java作为一种主流的编程语言,提供了丰富的框架和工具来支持国际化。
本文将介绍Java框架中国际化(Internationalization,简称i18n)的概念和常用的国际化支持方式。
一、国际化与本地化的概念在介绍Java框架中的国际化支持之前,我们先来了解一下国际化(i18n)和本地化(L10n)的概念。
1. 国际化国际化是指使软件在不同语言、不同文化环境下具备适应能力的过程。
通过国际化,软件能够根据用户的语言、地区等偏好展现相应的内容,以提供更好的用户体验。
2. 本地化本地化是指将软件调整到特定语言和文化环境的过程。
通过本地化,软件能够根据目标用户所在的特定地区,使用该地区的语言、符号、时间格式等,以更好地适应该地区的习惯和文化。
二、Java框架中的国际化支持1. ResourceBundleJava提供了ResourceBundle类,它是一种用于加载本地化资源的机制。
通过使用ResourceBundle,我们可以根据不同语言环境加载不同的资源文件,从而实现多语言支持。
2. MessageFormat在实现国际化时,经常需要根据不同的语言环境动态地替换文本中的占位符。
Java中的MessageFormat类可以帮助我们实现这一功能,它通过解析模式字符串,并使用传入的参数来生成最终的文本。
3. LocaleLocale是Java中用于表示不同国家和语言环境的类。
通过使用Locale,我们可以根据用户的偏好来选择合适的资源文件和本地化设置。
4. Spring框架的国际化支持Spring是Java中非常流行的开发框架之一,它提供了便捷的国际化支持。
在Spring中,我们可以使用MessageSource接口来加载和管理各国语言的资源文件。
通过配置不同的资源文件,我们可以实现Spring应用程序的国际化。
SpringBoot如何实现国际化

SpringBoot如何实现国际化在以前的 SpringMVC中,国际化需要以下3步骤1)、编写国际化配置⽂件;2)、使⽤ResourceBundleMessageSource管理国际化资源⽂件3)、在页⾯使⽤fmt:message取出国际化内容SpringBoot中实现国际化的步骤:1)编写国际化配置⽂件,抽取页⾯需要显⽰的国际化消息2)spring boot⾃动配置好了管理国际化资源⽂件的组件我们只需要在配置⽂件中指定国际化资源⽂件的位置spring.messages.basename=i18n.login//这是springboot帮我们配置的管理国际化资源⽂件的组件@ConfigurationProperties(prefix = "spring.messages")public class MessageSourceAutoConfiguration {/*** Comma-separated list of basenames (essentially a fully-qualified classpath* location), each following the ResourceBundle convention with relaxed support for* slash based locations. If it doesn't contain a package qualifier (such as* "org.mypackage"), it will be resolved from the classpath root.*/private String basename = "messages";//我们的配置⽂件可以直接放在类路径下叫messages.properties;@Beanpublic MessageSource messageSource() {ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); if (StringUtils.hasText(this.basename)) {//设置国际化资源⽂件的基础名(去掉语⾔国家代码的)messageSource.setBasenames(maDelimitedListToStringArray(StringUtils.trimAllWhitespace(this.basename)));}if (this.encoding != null) {messageSource.setDefaultEncoding(());}messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale);messageSource.setCacheSeconds(this.cacheSeconds);messageSource.setAlwaysUseMessageFormat(this.alwaysUseMessageFormat);return messageSource;}3)在页⾯获取国际化的值通过thymeleaf的 #{message}效果:根据浏览器语⾔设置的信息切换了国际化;原理:国际化Locale(区域信息对象);LocaleResolver(获取区域信息对象);@Bean@ConditionalOnMissingBean@ConditionalOnProperty(prefix = "spring.mvc", name = "locale")public LocaleResolver localeResolver() {if (this.mvcProperties.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {return new FixedLocaleResolver(this.mvcProperties.getLocale());}AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();localeResolver.setDefaultLocale(this.mvcProperties.getLocale());return localeResolver;}默认的就是根据请求头带来的区域信息获取Locale进⾏国际化4)、点击链接切换国际化前台:thymeleaf语法,localhost:8080/index<a class="btn btn-sm" th:href="@{/index(l='zh_CN')}" >中⽂</a><a class="btn btn-sm" th:href="@{/index(l='en_US')}">English</a>⾃定义 LocaleResolver/*** @author houChen* @date 2020/6/19 12:41* @Description: 定义⾃⼰的国际化信息解析器*/public class MyLocaleResolver implements LocaleResolver {@Overridepublic Locale resolveLocale(HttpServletRequest httpServletRequest) {String l= httpServletRequest.getParameter("l");Locale locale =Locale.getDefault();if(!StringUtils.isEmpty(l)){String [] split= l.split("_");locale=new Locale(split[0],split[1]);}return locale;}@Overridepublic void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) { }}在配置⽂件中:向ioc容器中注⼊LocaleResolver 组件/*** @Description: 将国际化信息解析器注册到容器* @param* @author houChen* @date 2020/6/19 13:16*/@Beanpublic LocaleResolver localeResolver(){return new MyLocaleResolver();}效果:。
SpringBoot实现国际化过程详解

SpringBoot实现国际化过程详解这篇⽂章主要介绍了SpringBoot实现国际化过程详解,⽂中通过⽰例代码介绍的⾮常详细,对⼤家的学习或者⼯作具有⼀定的参考学习价值,需要的朋友可以参考下实现⽅法:thymeleaf模板引擎加上BootStrap准备⼯作:1.将准备好的Bootstrap模板放在templates下让SpringBoot进⾏⾃动配置SpringBoot⾃动配置会⾃动到(idea的shif键连按两下进⼊全局搜索)2.Bootstrp的引⼊(这⾥是maven以depency的⽅式引⼊)<!--引⼊bootstrap--><dependency><groupId>org.webjars</groupId><artifactId>bootstrap</artifactId><version>4.0.0</version></dependency>3.thymeleaf的引⼊<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>4.编写国际化配置⽂件使⽤ResourceBundleMessageSource管理国际化资源⽂件springBoot默认配置⾃⼰配置的国际化的代码:package ponent;import org.springframework.web.servlet.LocaleResolver;import org.thymeleaf.util.StringUtils;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.util.Locale;/*** 在链接上携带区域信息*/public class MyLocaleResolver implements LocaleResolver {@Overridepublic Locale resolveLocale(HttpServletRequest request) {String l = request.getParameter("l");Locale locale = Locale.getDefault();if(!StringUtils.isEmpty(l)){String[] split = l.split("_");locale = new Locale(split[0],split[1]);}return locale;}@Overridepublic void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { }}上⾯附带链接结果乱码的解决⽅案;效果图:以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
中国春节国际化的原因英语作文

中国春节国际化的原因英语作文The Internationalization of Chinese Spring Festival。
Chinese Spring Festival, also known as the Lunar New Year, is the most important traditional festival in China. It is not only celebrated by Chinese people all around the world, but also attracts attention from people of different cultural backgrounds. The internationalization of Chinese Spring Festival can be attributed to several reasons.Firstly, the Chinese diaspora plays a significant role in spreading the celebration of Spring Festival to different parts of the world. With the globalization and economic development, many Chinese people have migrated to other countries for various reasons, such as education, work, or business opportunities. These overseas Chinese communities have brought their cultural traditions, including the Spring Festival, to their new homes. Through their efforts, the festival has gained recognition and popularity among local communities.Secondly, the increasing interest in Chinese culture and the rise of China's global influence have contributed to the internationalization of Spring Festival. As China becomes more prominent on the world stage, there is a growing curiosity and fascination with its rich cultural heritage. The Spring Festival, with its vibrant traditions, colorful customs, and delicious food, has become a window for people to explore and experience Chinese culture. Many countries now organize Spring Festival celebrations, featuring traditional performances, exhibitions, and cultural exchanges, to promote mutual understanding and appreciation.Furthermore, the development of transportation and communication has made it easier for people to travel and connect with others across borders. This has facilitated the exchange of cultural practices and traditions, including the celebration of Spring Festival. People from different countries and backgrounds now have the opportunity to participate in Spring Festival activities, such as lion dances, dragon parades, and temple fairs. The sharing of these experiences not only enhances cultural diversity but also promotes cultural dialogue and mutual respect.In addition, the commercialization of Spring Festival has played a role in its internationalization. As the festival has gained popularity, businesses have seized the opportunity to market Spring Festival-related products and services to a global audience. From traditional decorations and gifts to special holiday promotions, Spring Festival has become a lucrative market for businesses worldwide. This commercial aspect has further increased the visibility and recognition of the festival on an international scale.In conclusion, the internationalization of Chinese Spring Festival can be attributed to the Chinese diaspora, the increasing interest in Chinese culture, improved transportation and communication, as well as the commercialization of the festival. As more people from different cultural backgrounds embrace and celebrate Spring Festival, it serves as a bridge for cultural exchange and understanding. The internationalization of Spring Festival not only showcases the richness of Chinese culture but also promotes harmony and friendship among people from all over the world.。
春节越来越国际化的英语作文

春节越来越国际化的英语作文英文回答:The Spring Festival, also known as Chinese New Year, is the most important traditional festival in China. It is a time for family reunions, feasts, and celebrations. In recent years, the Spring Festival has become increasingly internationalized, as more and more people from around the world celebrate it.There are a number of reasons for the internationalization of the Spring Festival. One reason is the growing popularity of Chinese culture around the world. In recent years, there has been a surge of interest in Chinese language, food, and customs. This interest has been fueled by the rise of China as a global economic power, as well as by the increasing number of Chinese tourists traveling abroad.Another reason for the internationalization of theSpring Festival is the growing number of Chinese immigrants living in other countries. These immigrants have brought their traditions with them, and they have introduced the Spring Festival to their new communities. For example, there are now large Spring Festival celebrations in major cities all over the world, including New York City, London, and Sydney.The internationalization of the Spring Festival has had a number of positive effects. It has helped to spread Chinese culture around the world, and it has also helped to build bridges between China and other countries. Additionally, the Spring Festival has become a way for Chinese people living abroad to stay connected to their culture and traditions.中文回答:春节,又称中国新年,是中国最重要的传统节日,是千家万户团圆、欢庆的日子。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1:在MyEclipse下面创建一个test的Web Project,然后添加Spring相关的文件,在src根目录下创建applicationContext.xml文件。
applicationContext.xml
Xml代码
1.<?xml version="1.0"encoding="UTF-8"?>
2.<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.sp
/dtd/spring-beans.dtd">
3.
4.<beans>
5.
6.<bean id="messageSource"class="org.springframework.con
text.support.ResourceBundleMessageSource">
7.
8.<property name="basename"value="messages"/>
9.
10. </bean>
11.
12. <bean id="localeResolver"class="org.springframework.web
.servlet.i18n.CookieLocaleResolver"/>
13.
14.</beans>
2:在src根目录下面创建4个资源文件:分别是
引用
messages_zh.properties
main.title=你好
messages_en.properties
main.title=Hello World!
messages_ja.properties
main.title=こんにちは
messages_ko.properties
main.title=안녕하십니까
3:在WebRoot根目录下面创建test.jsp
test.jsp
Html代码
1.<%@ page language="java"pageEncoding="UTF-8"%>
2.<%@ taglib prefix="spring"uri="WEB-INF/lib/spring.tld"%>
3.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML
4.01 Transitional//EN">
4.<html>
5.<head>
6.<title>Spring国际化</title>
7.</head>
8.<body>
9.
10. <spring:message code="main.title"/><br>
11.
12. <input type="button"value="<spring:message code="main.titl
e" />"/><br>
13.
14. </body>
15.
16.</html>
4:修改WEB-INF下面的web.xml
在web.xml加入
Xml代码
1.<context-param>
2.<param-name>contextConfigLocation</param-name>
3.<param-value>
4. classpath*:/applicationContext*,classpath*:META-INF/applicat
ionContext*.xml
5.</param-value>
6.</context-param>
7.<listener>
8.<listener-class>org.springframework.web.context.ContextLoader
Listener</listener-class>
9.</listener>
这样用Spring国际化的Test.jsp页面就做好了:),此种方法是自动默认当前用户的语言,比如客户端是日语系统,就自动寻找messages_ja.properties资源文件,是英语系统,就自动寻找messages_en.properties资源文件。
注意事项:
1:用hibernate3.0,连接Mysql5.0数据库。
如果用hibernate.properties配置文件
hibernate.connection.url
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UT F-8
如果用hibernate.cfg.xml配置文件
jdbc:mysql://localhost:3306/test?useUnicode=true&useUnicode=true& amp;characterEncoding=utf-8&mysqlEncoding=utf8
2:页面的编码方式,应该选用utf-8
<%@ page language="java" pageEncoding="UTF-8"%>
3:创建的数据库的编码方式也应该选用utf-8,以及表,字段的编码方式都应选用utf-8
注意以上3点就可以解决国际化时,所出现的页面显示乱码问题,以及插入韩语时,出现的data too long for column问题.。