关于java国际化语言

        随着现代社会的进步,国际化语言越来越重要,当然软件行业的国际化也要与时俱进。

        国际化:简称“i8N”(internationalization 由于是由18位字母组成,所以简称为 I18N,关于i18n的简介大家感兴趣的可以搜索下百科)。  

        Java语言的国际化有很多方式可以实现,通过xml、Struts2等等途径,都可以达到我们想要的效果,下面我们使用配置文件的方式来实现国际化语言的切换


         

  1.     建立Web项目,(jdk的版本随项目要求去定义,这里不做要求)
  2.     在src下编写配置文件,注意配置文件的命名(尽量统一并有明显区分的命名,ps:message_zh_CN.properties)
  3.     编写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"%>

  4. 第一页面我们写个简单的登录页面
    <%@ 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>
    

  5. 再写个登陆后的页面
    <%@ 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>
    
    从上面两个页面我们可以看出最主要的就是两个jstl标签了
            <c:if test="${sessionScope.locale !=null }">
    		<!--sessionScope 即session.getAttribute(key)获取 -->
    		<fmt:setLocale value="${sessionScope.locale }" />
    	</c:if>
    	<fmt:bundle basename="mess">
    		
    	</fmt:bundle>
    这两个标签的具体含义可自行查看,这里我就不多说,
  6. 点击项目下载



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值