JavaWeb-国际化之fmt标签

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>

  

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