java web项目中资源国际化

有一些网站会有语言栏选项:

选择英文,内容就显示为英文;

选择中文,内容就显示文中文。

这里就用到了国际化资源。

先看效果图:

步骤:

1.建立资源包:

mess_en_US.properties (英文)

mess_ko_KR.properties (韩文)

mess_zh_CN.properties (中文)

...以及其他国家语言的资源文件。

文件内容如下:

mess_en_US.properties:

mess_ko_KR.properties:

mess_zh_CN.properties:

这三个文件放置在src目录下,页面和后台可以直接引用使用:

2.页面代码:

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@page import="java.util.Locale"%>
<%@page import="java.util.Date"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%
        String ctxPath = request.getContextPath();
        request.setAttribute("ctxpath", ctxPath);//项目根路径
    %>
    <!-- 动态获取,显示中英文切换 -->
    <%
        String code = request.getParameter("code");
        //HttpSession session = request.getSession();
        if(code!=null){
            if("en".equals(code)){ //英文
                session.setAttribute("locale", new Locale("en", "US"));
            }
            else if("zh".equals(code)){ //中文
                session.setAttribute("locale", new Locale("zh", "CN"));
            }
            else if("ko".equals(code)){ //韩文
                session.setAttribute("locale", new Locale("ko", "KR"));
            }
        }else{ //默认为中文
            session.setAttribute("locale", new Locale("zh", "CN"));
        }
    %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="${ctxpath}/js/jquery-1.11.3.min.js"></script>
   <title>国际化资源</title>
<style type="text/css">
/* 调整语言栏选项位置 */
.language{
    position: absolute;
    top: 4%;
    right:0;
}

/* 主页 */
.main{
    width: 50%;
    position: absolute;
    left: 25%;
    height: 100%;
}

/* 内容 */
.content{
    margin-left: 25%;
}

/* 标题 */
.title{
    display:block;
    width: 250px;
    margin: 0 auto;
    text-align: center;
}
</style>
<script type="text/javascript"> 
    $(function(){
        $("#language").bind("change",function(){
            window.location.href = "i18n_1.jsp?code="+this.value;
        });
        set_select_checked();
    });
    
    function set_select_checked(){ 
        var language = "<%=session.getAttribute("locale")%>";   
        language = language.substring(0, 2);
        var select = $("#language option");  
        
        
        for (var i = 0; i < select.length; i++){  
            if (select[i].value == language){  
                select[i].selected = true;
            }  
        }  
    }
</script>
</head>
<body>
    <div class="main">
        <div class="language">
            <span>语言:</span>
            <select id="language">
                <option value="zh">中文</option>
                <option value="en">英文</option>
                <option value="ko">韩文</option>
            </select> 
        </div>
        <br>
        <!-- 在session范围内设置locale,便于在jsp中获取locale -->
        <c:if test="${sessionScope.locale!=null }">
            <fmt:setLocale value="${sessionScope.locale }"/>
        </c:if>
        <fmt:setBundle basename="mess"/>
        <span class="title"><fmt:message key="hello"></fmt:message></span>
        <div class="content"> 
            <%-- <fmt:message key="salaryUnit"></fmt:message>
            <fmt:formatNumber value="12"></fmt:formatNumber> --%>
            <br><br>
            <span><fmt:message key="menu"></fmt:message></span>
            <ul>
                <li><fmt:message key="profTeam"></fmt:message></li>
                <li><fmt:message key="responsibility"></fmt:message></li>
                <li><fmt:message key="contactUs"></fmt:message></li>
            </ul>
        </div>
    </div>
</body>
</html>

页面使用JSTL标签显示国际化的内容。这个页面就实现了动态图所显示的效果(资源国际化)。


========================================

后台测试效果图:

后台测试类:

package test.locale;

import java.util.Locale;
import java.util.ResourceBundle;

public class GuojihuaTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        //取得系统默认的国家/语言环境
        
        Locale myLocale = Locale.ENGLISH;
        //Locale localeUS = new Locale("en", "US"); //ko KR
        Locale localeUS = new Locale("zh", "CN");
        //根据指定国家/语言环境加载资源文件

        ResourceBundle bundle = ResourceBundle.getBundle("mess" , localeUS);

        //打印从资源文件中取得的消息

        System.out.println(bundle.getString("hello"));


    }

}


 

原文地址:https://www.cnblogs.com/super-chao/p/8610109.html