JavaWeb.国际化
来源:互联网 发布:刘易斯汉密尔顿 知乎 编辑:程序博客网 时间:2024/05/18 03:42
1 什么是国际化
国际化就是可以把页面中的中文变成英文。例如在页面中的登录表单:
2 理解国际化
想把页面中的文字修改,那么就不能再使用硬编码,例如下面的页面中都是硬编码:
上图中的中文想转换成英文,那么就需要把它们都变成活编码:
3 Locale类
创建Locale类对象:
*new Locale(“zh”, “CN”);
*new Locale(“en”, “US”);
你一定对zh、CN或是en、US并不陌生,zh、en表示语言,而CN、US表示国家。一个Locale对象表示的就是语言和国家。
4 ResourceBundle类
ReourceBundle类用来获取配置文件中的内容。
下面是两个配置文件内容:
资源文件名称的格式:基本名称+Locale部份+.properties
例如:res_zh_CN.properties,其中基本名称为res,而zh_CN是Locale部份
必须所有的资源文件基石名称要相同!不同之处就是Locale部件
res_zh_CN.properties
res_en_US.properties
配置文件要放到src目录下
public class Demo1 { @Test public void fun1() { ResourceBundle rb = ResourceBundle.getBundle("res", new Locale("zh", "CN" )); String username = rb.getString("msg.username"); String password = rb.getString("msg.password"); System.out.println(username); System.out.println(password); } @Test public void fun2() { ResourceBundle rb = ResourceBundle.getBundle("res", new Locale("en", "US" )); String username = rb.getString("msg.username"); String password = rb.getString("msg.password"); System.out.println(username); System.out.println(password); }}
ResourceBundle的getBundle()方法需要两个参数:
* 第一个参数:配置文件的基本名称* 第二个参数:Locale
getBundle()方法会通过两个参数来锁定配置文件!
5 页面国际化
其实页面国际化也是同一道理,只需要通过切换Locale来切换配置文件。我们写一个MessageUtils类,内部需要ResourceBundle的实例。
login.jsp:
<%--把与语言相关的所有字符串都写成变量!!! --%> <% /* 1. 获取Locale,这是由客户端的浏览器提供的Locale 2. 创建ResourceBundle 3. 把所有的语言信息使用rb.getString("xxx")来替换! */ Locale locale = request.getLocale(); ResourceBundle rb = ResourceBundle.getBundle("res", locale); %><h1><%=rb.getString("login") %></h1><form action="" method="post"><%=rb.getString("username") %>:<input type="text" name="username"/><br/><%=rb.getString("password") %>:<input type="password" name="password"/><br/><input type="submit" value="<%=rb.getString("login") %>"/></form>
Locale locale = request.getLocale();
获取Locale,这是由客户端的浏览器提供的Locale(浏览器工具–》Internet属性–》语言)
根据排序默认设置为排在最上面的语言(也可以通过上移来改默认值)
也可以通过头信息来查看:
Accept-Language: zh-CN,en-US;q=0.5
我们再写一个过滤器MessageFilter,它会通过参数创建Locale对象,传递给MessageUtils,然后在页面中使用MessageUtils来获取文本信息。
MessageUtils.java
public class MessageUtils { private static String baseName = "res"; private static Locale locale; public static String getText(String key) { return ResourceBundle.getBundle(baseName, locale).getString(key); } public static Locale getLocale() { return locale; } public static void setLocale(Locale locale) { MessageUtils.locale = locale; }}
MessageFilter.java
public class MessageFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; String l = req.getParameter("request_locale"); Locale locale = null; if(l != null && !l.isEmpty()) { String[] strs = l.split("_"); locale = new Locale(strs[0], strs[1]); req.getSession().setAttribute("WW_TRANS_I18N_LOCALE", locale); } else { locale = (Locale)req.getSession().getAttribute("WW_TRANS_I18N_LOCALE"); } if(locale == null) { locale = req.getLocale(); } MessageUtils.setLocale(locale); chain.doFilter(request, response); } public void init(FilterConfig fConfig) throws ServletException { }}
login.jsp
<body> <h1><%=MessageUtils.getText("msg.login") %></h1> <form action="<c:url value='/index.jsp'/>" method="post"> <%=MessageUtils.getText("msg.username")%>:<input type="text" name="username"/><br/> <%=MessageUtils.getText("msg.password")%>:<input type="password" name="password"/><br/> <input type="submit" value='<%=MessageUtils.getText("msg.login")%>'/> </form> </body>
index.jsp
<body> <p><%=MessageUtils.getText("hello") + ":" +request.getParameter("username") %>:</p> </body>
6 NumberFormat
NumberFormat类用来对数字进行格式化,我们只需要使用String format(double)一个方法即可。下面是获取NumberFormat实例的方法:
* NumberFormat format = NumberFormat.getNumberFormat()* NumberFormat format = NumberFormat.getNumberFormat(Locale)* NumberFormat format = NumberFormat.getCurrentcyFormat()* NumberFormat format = NumberFormat.getCurrentcyFormat(Locale)* NumberFormat format = NumberFormat.getPercentFormat()* NumberFormat format = NumberFormat.getPercentFormat(Locale)
7 DateFormat
DateFormat类用来对日期进行格式化,我们只需要使用String format(Date)一个方法即可。下面是获取DateFormat实例的方法:
* DateFormat format = DateFormat.getDateFormat();* DateFormat format = DateFormat.getTimeFormat();* DateFormat format = DateFormat.getDateTimeFormat();* DateFormat format = DateFormat.getDateFormat(int style, Locale locale);* DateFormat format = DateFormat.getTimeFormat(int style, Locale locale);* DateFormat format = DateFormat.getDateTimeFormat(int style, Locale locale);
其中style是对日期的长、中、短,以及完整样式。
* SHORT; * MEDIUM; * LONG;* FULL
8 MessageFormat
MessageFormat可以把模式中的{N}使用参数来替换。我们把{N}称之为点位符。其中点位符中的N是从0开始的整数。
MessageFormat.format(String pattern, Object… params),其中pattern中可以包含0~n个点位符,而params表示对点位符的替换文本。注意,点位符需要从0开始。
String p = “{0}或{1}错误”;
String text = MessageFormat.format(p, “用户名”, “密码”);
System.out.println(text);//用户名或密码错误
- javaweb-国际化
- javaweb国际化
- JavaWeb国际化
- Javaweb国际化
- JavaWeb 国际化
- javaweb国际化
- JavaWeb.国际化
- JavaWEB开发-国际化
- JavaWEB开发-国际化:
- javaweb国际化/格式化标签
- JavaWEB开发-国际化:
- JavaWEB开发--国际化
- javaweb国际化入门
- JavaWEB开发的国际化
- JavaWEB开发-国际化
- JavaWEB开发-国际化1
- javaweb-国际化2
- JavaWEB开发-国际化
- 漏洞分析挖掘基础知识
- mysql
- VUE(五)
- JAVA中JDK和JRE的区别
- 【Android Studio】platform-tools is too old to check APIs compiled with API 27
- JavaWeb.国际化
- HTML5获取当前地理位置并在百度地图上展示的实例
- Java常量池理解与总结
- Hadoop 安装与环境搭建——遇到的问题及解决方法总结
- python3 flask框架
- Hive的三种存储格式
- android开源框架
- bzoj 3217: ALOEXT 替罪羊树套trie
- Ubuntu16.04 安装Navicat11.2.16