用JSTL实现i18n国际化

<%--1 使用标签设置 Locale 信息 --%>
< fmt :setLocale value ="" />
<%--2 使用标签设置 baseName--%>
< fmt :setBundle basename ="" />
< %--3 输出指定 key 的国际化信息 --%>
< fmt :message key ="" />

 

 

 

 

<%@ page import="java.util.Locale" %>
<%@ page import="java.util.ResourceBundle" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--   <%--%>

<%--       Locale locale =null;--%>
<%--       String country = request.getParameter("country");--%>
<%--       if("china".equals(country)){--%>
<%--           locale=Locale.CHINA;--%>
<%--       }else if("usa".equals(country)){--%>
<%--           locale=Locale.US;--%>
<%--       }else{--%>
<%--           locale=request.getLocale();//从请求头中读取locale语言信息--%>
<%--       }--%>
<%--       //根据basename和locale对象获得resourceBundle类--%>
<%--       ResourceBundle resourceBundle=ResourceBundle.getBundle("i18n"+locale);--%>
<%--   %>--%>
<%--  使用标签设置locale信息--%>
  <fmt:setLocale value="${param.locale}"/>
<%--     使用标签设置basename--%>
  <fmt:setBundle basename="i18n"/>
<%--   <a href="test.jsp?country=china">中文</a>--%>
<%--   <a href="test.jsp?country=usa">English</a>--%>
    <a href="test.jsp?locale=zh_CN">中文</a>
   <a href="test.jsp?locale=en_US">English</a>
   <form>
       <table>
           <tr>
<%--               <td><%=resourceBundle.getString("username")%></td>--%>
<%--               输出指定key的国际化信息--%>
               <td><fmt:message key="username"/> </td>
               <td><input name="username" type="text"/></td>
           </tr>
           <tr>
<%--               <td><%=resourceBundle.getString("password")%></td>--%>
               <td><fmt:message key="password"/> </td>
               <td><input name="password" type="password"/></td>
           </tr>
           <tr>
<%--               <td><%=resourceBundle.getString("reset")%></td>--%>
               <td><fmt:message key="reset"/> </td>
               <td><input name="reset" type="reset"/></td>
           </tr>
           <tr>
<%--               <td><%=resourceBundle.getString("submit")%></td>--%>
               <td><fmt:message key="submit"/> </td>
               <td><input name="submit" type="submit"/> </td>
           </tr>
       </table>
   </form>
</body>
</html>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值