随着现代社会的进步,国际化语言越来越重要,当然软件行业的国际化也要与时俱进。
国际化:简称“i8N”(internationalization 由于是由18位字母组成,所以简称为 I18N,关于i18n的简介大家感兴趣的可以搜索下百科)。
Java语言的国际化有很多方式可以实现,通过xml、Struts2等等途径,都可以达到我们想要的效果,下面我们使用配置文件的方式来实现国际化语言的切换
- 建立Web项目,(jdk的版本随项目要求去定义,这里不做要求)
- 在src下编写配置文件,注意配置文件的命名(尽量统一并有明显区分的命名,ps:message_zh_CN.properties)
- 编写jsp页面,页面上我们要用到两个jstl标签一个是“c”标签,另一个是“fmt”标签,(注意包不要导错哦)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
- 第一页面我们写个简单的登录页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <% String code = request.getParameter("code"); if (code != null) { if ("US".equals(code)) { session.setAttribute("locale", Locale.US); } else if ("CN".equals(code)) { session.setAttribute("locale", Locale.CHINA); } else if ("TW".equals(code)) { session.setAttribute("locale", Locale.TAIWAN); } else if ("JP".equals(code)) { session.setAttribute("locale", Locale.JAPAN); } else if ("KR".equals(code)) { session.setAttribute("locale", Locale.KOREA); } } %> <body> <c:if test="${sessionScope.locale !=null }"> <!--sessionScope 即session.getAttribute(key)获取 --> <fmt:setLocale value="${sessionScope.locale }" /> </c:if> <fmt:bundle basename="mess"> <form action="MyJsp.jsp" method="post" ><br><br><br><br><br> <div align="center" > <h2><fmt:message key='lan1' ></fmt:message>: <input type="text" id = "username" name = "uname" ><br><br> <fmt:message key='lan2' ></fmt:message>: <input type="password" id = " password" name = "pwd" ><br><br> <input type="submit" value = "<fmt:message key='lan3' ></fmt:message>" ></h2> <br><br><br><br><br> <a href="index.jsp?code=CN"><img src="images/china.png">简体中文</a> <a href="index.jsp?code=TW"><img src="images/taiwan.png"> 繁體中文</a> <a href="index.jsp?code=US"><img src="images/american.png"> American English</a> <a href="index.jsp?code=JP"><img src="images/japan.png"> 日本の言叶</a> </div> </form> </fmt:bundle> <br> </body> </html>
- 再写个登陆后的页面
从上面两个页面我们可以看出最主要的就是两个jstl标签了<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'MyJsp.jsp' starting page</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <% request.setCharacterEncoding("UTF-8"); %> </head> <body> <c:if test="${sessionScope.locale !=null }"> <!--sessionScope 即session.getAttribute(key)获取 --> <fmt:setLocale value="${sessionScope.locale }" /> </c:if> <fmt:bundle basename="mess"> <form action="" method="post"> <div align="center"> <h1><% String name = request.getParameter("uname"); %> <fmt:message key='lan1' ></fmt:message>: <% out.print(name); %> </h1> </div> </form> </fmt:bundle> </body> </html>
这两个标签的具体含义可自行查看,这里我就不多说,<c:if test="${sessionScope.locale !=null }"> <!--sessionScope 即session.getAttribute(key)获取 --> <fmt:setLocale value="${sessionScope.locale }" /> </c:if> <fmt:bundle basename="mess"> </fmt:bundle>
- 点击项目下载