国际化 中英文切换

本文详细介绍了如何在网页设计中实现多语言支持,通过使用Java和JSP技术,结合JSTL标签库,实现了日期和货币的本地化显示。同时,通过设置不同的URL参数来切换语言环境,提供了英语和中文两种语言版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

index.jsp
<%@page import="java.util.Locale"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib   prefix="fmt"  uri="http://java.sun.com/jsp/jstl/fmt" %>
    <%@taglib   uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
    
<!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>
         <%
         Date  date=new Date();
         request.setAttribute("date", date);
         request.setAttribute("salary", 12345.12);
         
         %>
         <fmt:bundle basename="i18n">
            <fmt:message key="date"></fmt:message>
             <fmt:formatDate value="${date }"/>
            <fmt:message key="salary"></fmt:message>
			<fmt:formatNumber value="${salary }"></fmt:formatNumber>
   </fmt:bundle>
   <br><br>
   <%
     String code=request.getParameter("code");
     if(code!=null){
    	 if("en".equals(code)){
    		            session.setAttribute("locale", Locale.US);
    	 }else if("zh".equals(code)){
    		 session.setAttribute("locale", Locale.CHINA);
    	 }
     }
   
   
   %>
   <c:if test="${sessionScope.locale !=null }">
       <fmt:setLocale value="${sessionScope.locale }"/>
   </c:if>
   <br><br>
              <fmt:setBundle basename="i18n"/>
              <fmt:message key="date"></fmt:message>
            <fmt:formatDate value="${date }" dateStyle="FULL"/>
            <fmt:message key="salary"></fmt:message>
            <fmt:formatNumber value="${salary }" type="currency"></fmt:formatNumber>
   <br><br>
       <a href="index.jsp?code=en">English</a> 
       <a href="index.jsp?code=zh">中文</a> 
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值