国际化我们通常用缩写来简单,那就是i18n。它就是让我们的系统可以转换成不同的语言。为了转换成不同的言语,我们需要定义不同的文件,如:messages_en.properties,messages_en_US.properties,messages_fr.properties。我想聪明的你,可以看出他们之间的定义格式了。(如果,读者在定义这些文件后,系统没有实现国际化,那么可以将系统默认的语言文件直接用messages.peoperties.)

1. 添加配置文件

接下来,我们要做的事就是转换我们的信息,笔者这里对英语的语言就没有定义为message_en_US.properties文件,而是直接用messages.properties,因为英语就是作为系统的默认语言。messages.properties的配置信息如下:

Size.profileForm.twitterHandle=Please type in your twitter user name
Email.profileForm.email=Please specify a valid email address
NotEmpty.profileForm.email=Please specify your email address
PastLocalDate.profileForm.birthDate=Please specify a real birth date
NotNull.profileForm.birthDate=Please specify your birth datetypeMismatch.birthDate = Invalid birth date format.NotEmpty.profileForm.tastes=Please enter at least one thing
profile.title=Your profile
twitter.handle=Twitter handle
email=Email
birthdate=Birth Date
tastes.legend=What do you like?
remove=Remove
taste.placeholder=Enter a keyword
add.taste=Add taste
submit=Submit

对于法语,读者可以用翻译的软件来翻译。文件是messages_fr.properties

 Email.profileForm.email=Veuillez spécifier une adresse mail valide
NotEmpty.profileForm.email=Veuillez spécifier votre adresse mail
PastLocalDate.profileForm.birthDate=Veuillez donner votre vraie date de naissance
NotNull.profileForm.birthDate=Veuillez spécifier votre date de naissancetypeMismatch.birthDate = Date de naissance invalide.NotEmpty.profileForm.tastes=Veuillez saisir au moins une chose
profile.title=Votre profil
twitter.handle=Pseudo twitter
email=Email
birthdate=Date de naissance
tastes.legend=Quels sont vos go?ts ?
remove=Supprimer
taste.placeholder=Entrez un mot-clé
add.taste=Ajouter un centre d'intérêt
submit=Envoyer

2.改变本地语言

在我们的程序中,用户使用系统是跟自己本地相关系的。我们会Session中保存用户的个人信息。我们应该要允许用户去改变自己的本地语言。所以我们要使用SessionLocaleResolver.让我们修改WebConfiguration:

package masterSpringMVC.config;import masterSpringMVC.date.USLocalDateFormatter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;import java.time.LocalDate;
import java.util.Locale;/*** 定制SpringMVC的配置(如:日期、语言等)* 有点类似于Spring的xml的文件配置* 可以添加需要用的Bean,还有拦截器、事务控制等* Created by OwenWilliam on 2016/5/15.*/
@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {/***格式转换处理* @param registry*/@Overridepublic void addFormatters(FormatterRegistry registry) {registry.addFormatterForFieldType(LocalDate.class, new USLocalDateFormatter());}/*** 从HTTP的请求地址中找到所属于的国家* @return*/@Beanpublic LocaleResolver localeResolver(){return new SessionLocaleResolver();}/*** 拦截器:拦截请求的地址* @return*/@Beanpublic LocaleChangeInterceptor localeChangeInterceptor(){LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();//在拦截的地址后面添加?lang=xx。如: http://localhost:8080/profile?lang=frlocaleChangeInterceptor.setParamName("lang");return localeChangeInterceptor;}/*** 注册拦截器* @param registry*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(localeChangeInterceptor());}
}

在上面的代码中,细心的你已经清单到了newSessionLocaleResolver()。其实这个是LocaleResolver的接口。其实LocaleResolver的接口有如下几个:

1)        FixedLocaleResolver: 这修复了配置中定义的区域设置,一旦这个被定义了就不能修改。

2)        CookiesLocaleResolver:这个允许将相关的信息检索和保存到本地的cookie中。

3)        SessionLocaleResolver:这个作用域就是在HTTPsession中。

3.视图层修改

1)        我们需要在视图的profilePage的页面中添加可能更换语言的控件,这个不仅仅是用在这个页面上,同时也是公用部分,所以我们添加到layout的页面。修改之后的页面如下:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head><meta http-equiv="Content-Type" content="text/html" charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no"/><title>Default title</title><link href="/webjars/materializecss/0.96.0/css/materialize.css"type="text/css" rel="stylesheet" media="screen,projection"/>
</head>
<body><ul id="lang-dropdown" class="dropdown-content"><li><a href="?lang=en_US">English</a></li><li><a href="?lang=fr">French</a></li></ul><nav><div class="nav-wrapper indigo"><ul class="right"><li><a class="dropdown-button" href="#!" data-activates="lang-dropdown"><i class="mdi-action-language right"></i>Lang</a></li></ul></div></nav><section layout:fragment="content"><p>Page content goes here</p></section><script src="/webjars/jquery/2.1.4/jquery.js"></script><script src="/webjars/materializecss/0.96.0/js/materialize.js"></script><script type="text/javascript">$(".dropdown-button").dropdown();</script><script type="text/javascript" layout:fragment="script"></script>
</body>
</html>

2)        最后,我们要在视图的页面上用到国际化,我们用到的EL表达语言是#{}.

查看下面的代码,清单#{}的地方(profilePage.html)。

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="layout/default">
<head lang="en">
<title>Your profile</title>
</head>
<body>
<div class="row" layout:fragment="content">
<h2 class="indigo-text center" th:text="#{profile.title}">Personal
info</h2>
<form th:action="@{/profile}" th:object="${profileForm}"
method="post" class="col m8 s12 offset-m2">
<div class="row">
<div class="input-field col s6">
<input th:field="${profileForm.twitterHandle}"
id="twitterHandle" type="text" th:errorclass="invalid"/>
<label for="twitterHandle" th:text="#{twitter.
handle}">Twitter handle</label>
<div th:errors="*{twitterHandle}" class="redtext">Error</div>
</div>
<div class="input-field col s6">
<input th:field="${profileForm.email}" id="email"
type="text" th:errorclass="invalid"/>
<label for="email" th:text="#{email}">Email</label>
<div th:errors="*{email}" class="red-text">Error</div>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input th:field="${profileForm.birthDate}"
id="birthDate" type="text" th:errorclass="invalid"/>
<label for="birthDate" th:text="#{birthdate}" th:place
holder="${dateFormat}">Birth Date</label>
<div th:errors="*{birthDate}" class="red-text">Error</
div>
</div>
</div>
<div class="row s12 center">
<button class="btn indigo waves-effect waves-light"
type="submit" name="save" th:text="#{submit}">Submit
<i class="mdi-content-send right"></i>
</button>
</div>
</form>
</div>
</body>
</html>

4.总结

    在这一章节中,我们学习了SpringMVC的国际化是如何配置的。无非不是messages_xx.properties的文件配置。当然,为了要以通过控件来修改,所以我们在webConfiguration中也添加了代码。这个地方主要是浏览器请求时的地址解析是请求哪一种语言调用。如:http://localhost:8080/profile?lang=fr

。最后,我们也不要忘记在视图层中加入我们国际化文件信息。最后运行的结果如下:

源码路径:git@github.com:owenwilliam/masterSpringMVC.git

SpringMVC国际化(i18n)(五)相关推荐

  1. SpringMvc国际化i18n

    所谓国际化就是支持多种语言,web应用在不同的浏览环境中可以显示出不同的语言,比如说汉语.英语等.    下面简单说一下Springmvc下的i18n的国际化配置: 首先发下我的目录结构图: spri ...

  2. springMVC项目国际化(i18n)实现方法

    SpringMVC项目国际化(i18n)实现方法 按照作息规律,每周五晚必须是分享知识的时间\(^o^)/~,这周讲点儿啥呢,项目需要逼格,咱们国际化吧(* ̄rǒ ̄)~,项目中碰到这类需求的童鞋可能并 ...

  3. javaweb学习总结(三十一)——国际化(i18n)

    一.国际化开发概述 软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的.符合来访者阅读习惯的页面或数据. 国际化(internationaliz ...

  4. 深入分析JavaWeb Item22 -- 国际化(i18n)

    一.国际化开发概述 软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的.符合来访者阅读习惯的页面或数据. 国际化(internationaliz ...

  5. springMVC 国际化 多语言

    springMVC 国际化(多语言) 配置 系统有时需要考虑多国人员使用(比如中国人.美国人.日本人.韩国人),面向不同国家的使用者应该能方便地在不同语言之间进行切换,比如中文.英文.日文.韩文. 常 ...

  6. 关于SpringMVC国际化的问题--中文状态下运行正常,英文状态不正常

    注意:这是我自己的分析,如有什么不对的地方请评论区指正. 问题描述: 昨天学习了关于SpringMVC国际化的知识,在所有代码完成的情况下,我在配置文件中配置i18n文件之后,浏览器出现在中文环境下好 ...

  7. Python中国际化(i18n)完整指南

    这是一个完整的指南,展示了如何为一个Python应用程序进行国际化(i18n).当我在handroll项目中添加i18n时,我很难找到支持其他语言的明确建议.这是我个人的一点经验,解释了我是如何做到这 ...

  8. java i18n实例_Java国际化(i18n)格式化日期

    本篇文章帮大家学习java国际化(i18n)格式化日期,包含了Java国际化(i18n)格式化日期使用方法.操作技巧.实例演示和注意事项,有一定的学习价值,大家可以用来参考. DateFormat类提 ...

  9. Docker+Nginx部署Angular国际化i18n

    Docker+Nginx部署Angular国际化i18n 在Angular项目中添加default.conf文件 default.conf 为了支持局域网,增加一个域名,即本地的局域网ip地址. se ...

  10. spring 国际化-i18n

    i18n(其 来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是"国际化"的简称.在资讯领域,国际化(i18n)指让产品(出版 物,软 ...

最新文章

  1. 服务模拟-ServiceMock
  2. python调用qq互联_Django项目中实现使用qq第三方登录功能
  3. GIS影像数据集初步学习
  4. oracle 丁勇 从零开始学_8.3.1 多表查询分类
  5. 深入了解ibatis源码----简单ibatis示例代码
  6. Vue.js入门系列教程(二)
  7. 新乡学院2019计算机报名,新乡学院2019年招生章程
  8. mysql inet_aton 与 inet_ntoa 方法
  9. 几款不错的屏幕键盘软件~
  10. git push you are not allowed to upload merges
  11. win10删除工作组计算机,win10工作组怎么退出-退出win10工作组的教程 - 河东软件园...
  12. 全国计算机等级考试-三级信息安全考试知识点(无顺序)
  13. 微信小程序订阅信息之Java实现详解
  14. 秒懂 Git 与 Gitee(码云)
  15. python错误:IndentationError: expected an indented block,教你一招搞定
  16. larver php7.0,关于PHP7.0与PHP5.6下Laravel博客应用性能对比分析详解
  17. .net 6 在退出构造函数时,不可为 null 的 属性“xxx”必须包含非 null 值。
  18. Android类微信(二)
  19. ArcGIS 水文分析模型构建器工具
  20. python学习 第一模块习题总结

热门文章

  1. MVC仓储执行存储过程报错“未提供该参数”
  2. [多线程学习笔记] 一个线程安全的队列
  3. iOS基础 - UIScrollView
  4. Sqlite使用简单教程
  5. Windows 8实用窍门系列:1.使用Xaml+C#开发第一个Metro Style应用程序
  6. DIY协同办公平台(C/S)系列3之内部邮箱篇
  7. 怎么用javascript进行拖拽[zt]
  8. web安全day21:学习使用最基本的批处理程序
  9. Security+ 学习笔记43 无线网络
  10. 数据库与MySQL基本知识