首页 > 编程语言 > 详细

JavaWeb-国际化之fmt标签

时间:2019-07-23 17:10:17      阅读:87      评论:0      收藏:0      [点我收藏+]

JSTL的fmt标签完成的国际化,后面使用框架提供的标签完成

实现中文、英文切换:

  》提供两个超简洁,携带不同的变量值

  》根据变量值确定对应的Locale对象

  》把Locale对象放入到session中

  》绑定Locale对应的资源文件。

 

<%@ page import="java.util.Locale" %>
<%@ page import="java.util.Date" %><%--
  Created by IntelliJ IDEA.
  User: dell
  Date: 2019/7/23
  Time: 16:36
  To change this template use File | Settings | File Templates.
--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@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>c
</head>
<body>
    <%
        Date date = new Date();
        request.setAttribute("date",date);
        request.setAttribute("salary",12345.67);
    %>

    <%
        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>
    <fmt:setBundle basename="i18n"/>
        <fmt:message key="key"/>:
        <fmt:formatDate value="${date }" dateStyle="FULL" />
        <fmt:message key="salary"/>:
        <fmt:formatNumber value="${salary }" type="currency"/>
    <br><br>

    <a href="xxx.jsp?code=en">English</a>
    <a href="xxx.jsp?code=zh">中文</a>
    
    
</body>
</html>

 

  

 

JavaWeb-国际化之fmt标签

原文:https://www.cnblogs.com/yangHS/p/11232857.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!