简单的国际化i18n

就是简单的中英文转换

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
    <%@ taglib prefix="fmt"  uri="http://java.sun.com/jsp/jstl/fmt"%>
<!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>

<form action="">
<!--为空相当于当前页面刷新-->
    <%
        String code=request.getParameter("code");
        if(code==null || "".equals(code) ){
            code=request.getLocale().toString();
        }
    %>
    <fmt:setLocale value="<%=code%>"/>//通过这里来设置不同类型的语言
    <fmt:bundle basename="i18n">
    <!-- 在bundle里的地址(message... )加前缀i18n -->
    <table>
        <tr>
            <td><fmt:message key="usernamelable"></fmt:message></td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td><fmt:message key="passwordlable"></fmt:message></td>
            <td><input type="text" name="password"></td>
        </tr>
        <tr>
            <td><input type="submit" value="<fmt:message key="submitlable"></fmt:message>
"></td>
            <td><input type="reset" value="<fmt:message key="resetlable"></fmt:message>"></td>
        </tr>
        <tr>
            <td><a href="index.jsp?code=zh">中文</a></td>
            <td><a href="index.jsp?code=en">英文</a></td>
        </tr>
    </table>
    </fmt:bundle>
</form>    
</body>
</html>

2种语言

中文(默认)

i18n_cn.properties

i18n.properties

两者名字不同但是内容一致

usernamelable=u7528u6237u540d
passwordlable=u5bc6u7801
submitlable=u767bu5f55
resetlable=u91cdu7f6e

英文

i18n_en.properties

usernamelable=UserName
passwordlable=Password
submitlable=Submit
resetlable=Reset
原文地址:https://www.cnblogs.com/ydymz/p/6374720.html