Java语言对国际化的支持

47 篇文章 0 订阅
45 篇文章 0 订阅
最近在做项目的时候,由于用到Java基础的东西比较少,都是一些框架的封装的应用,考虑到以后如果接触到其他框架了,会不会对这些底层的东西遗忘啊,所以下定决心,决定每天复习一点之前的知识,作为自己的一个备忘录吧。

随心翻到的一篇,Java对国际化的支持,做过web开发的同学都应该知道I18N这个东西,我就不去讲他的概念了,来张图片说说他的好处吧。

[img]http://dl2.iteye.com/upload/attachment/0093/0471/8883bdf3-df96-3345-8464-a91e05eb31ea.jpg[/img]

好处有那么多,我就不针对各个点进行详解了,我们直接来看Java语言的国际化的实现机制吧,当然下图是针对B/S框架而言的。


[img]http://dl2.iteye.com/upload/attachment/0093/0477/6c0c03b4-56c2-31ee-9e6d-21219c85e264.jpg[/img]


服务器根据客户端的传过来的语言进行解析,这些语言的值一般都保存在浏览器中,举例:

在IE浏览器中的语言选择如下图:
点击工具:


[img]http://dl2.iteye.com/upload/attachment/0093/0489/09dbbb92-2016-3c2c-a00d-b9a2ea22ef9a.jpg[/img]

点击语言:


[img]http://dl2.iteye.com/upload/attachment/0093/0491/78c773cf-94e0-3f44-a2c4-435ed7dcfc89.jpg[/img]

到时候浏览器向服务器发送请求的时候,就会把这个选项带着,然后交给web容器,web容器经过映射,拿到你想要的语言项。

[b]所以:Web容器中的Locale对象来源于Request对象。[/b]


好了,现在我们来说说如何开发吧。

你想实现多语言,首先你得告诉服务器你支持哪几种语言啊,就是你有哪些语言的资源文件。
[b]
资源文件位置:在类路径下,以.properties为后缀。

资源文件命名:基本的命名_场所标志.properties。

场所标志:Locale对象中保存的一个属性值,例如:中国:zh_CN,美国en_US

那么上述资源文件的命名举例:myres_zh_CN.properties。[/b]

资源文件内容:Key=Value,例如:jsp.index.user=User Login,由于多个模块都可能用到Key值,因此为了区分,它支持"."来命名,接触过资源文件的同学应该都知道这些。

举例:

英文的资源文件:
jsp.index.userId=UserId:
jsp.index.userPwd=Password:


中文的资源文件:
jsp.index.userId=用户名:
jsp.index.userPwd=密码:


上述保存的中文就是这样的么?当然不是的,Java中保存汉字的时候是以Unicode编码保存的,因此需要把这些汉字进行转码。

JDK考虑到了这一点,它提供了一个转码的工具,供我们使用,当然了,现在IDE的集成开发工具都集成了很多插件,这样的转码工作,完全交给集成开发工具就可以搞定的,在这里我也就提下我们最原始的转码办法吧,当然我相信,这些转码插件底层应该也是调用了这个工具吧。


转码工具:native2ascii。JDK自带的。

用法:native2ascii 原始文件的全路径 目标文件的全路径(保证你的JAVA_HOME配置的是对的,否则这样执行会提示找不到)。


[img]http://dl2.iteye.com/upload/attachment/0093/0518/9e3b58c0-e898-3026-9086-0d0486387174.jpg[/img]

那么在我的本地D盘下会生成一个文件,打开如下:

jsp.index.title=\u7528\u6237\u767b\u5f55
jsp.index.userId=\u7528\u6237\u540d\uff1a
jsp.index.userPwd=\u5bc6\u7801\uff1a
jsp.index.submit=\u767b\u5f55
err.login.failure=\u767b\u5f55\u5931\u8d25\uff01


好了,预备知识差不多了,我们就开始项目吧。

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<f:setBundle basename="res.myres" />
<c:if test="${errKey!=null}">
<f:message key="${errKey }" />
</c:if>
<f:message key="jsp.index.title" />
<form action="login.do" method="post">
<p>
<f:message key="jsp.index.userId" />
<input type="text" name="userId" />
</p>
<p>
<f:message key="jsp.index.userPwd" />
<input type="text" name="userPwd" />
</p>
<p>
<input type="submit" value="<f:message key="jsp.index.submit"/>" />
</p>
</form>
</body>
</html>


项目部署到Tomcat中,跑起来,按照之前浏览器的截图,把IE的浏览器语言设置为如下


[img]http://dl2.iteye.com/upload/attachment/0093/0520/9c393678-20f9-3ed0-8501-1228b14f8a74.jpg[/img]

访问http://localhost:8080/i18n/index.jsp,查看到的是英文的界面。


再找一个非IE内核的浏览器(我这里用的谷歌),直接访问,看到的应该是中文的界面了。

好了,页面弄好了,但是后台Java代码呢,如何访问资源文件呢?

package i18n;

import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("userId===" + request.getParameter("userId"));
Locale loc = request.getLocale();
ResourceBundle bundle = ResourceBundle.getBundle("res.myres", loc);
String errInfo = bundle.getString("err.login.failure");
request.setAttribute("errInfo", errInfo);

//request.setAttribute("errKey", "err.login.failure");
request.getRequestDispatcher("/index.jsp").forward(request, response);

}

}



上述有两种写法,一种是在后台把消息值根据本地语言环境拿到,然后再放进去,还有一种,是直接把key给放进去,让JSP自己去获取。
那么代码如下:
package i18n;

import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("userId===" + request.getParameter("userId"));
// Locale loc = request.getLocale();
// ResourceBundle bundle = ResourceBundle.getBundle("res.myres", loc);
// String errInfo = bundle.getString("err.login.failure");
// request.setAttribute("errInfo", errInfo);

request.setAttribute("errKey", "err.login.failure");
request.getRequestDispatcher("/index.jsp").forward(request, response);

}

}



好了,上述就是Java对国际化的支持,写的比较浅,大家觉得有什么不对的地方,可以指出来。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值