Struts 2 i18n没有处理动态内容

时间:2014-11-05 16:18:57

标签: jquery jsp struts2 internationalization

我想翻译一个页面,根据您点击的位置更改其内容。即。

我有这个JSP代码,main.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Main</title>

<script type="text/javascript" src="js/jquery-1.11.1.js"></script>

</head>
<body>
    <div id="header">
        <!-- When I click on a link, #content load the page [cars.jsp | plains.jsp | ships.jsp] -->
        <a id="cars" href="#"><s:text name="global.cars" /></a>
        <a id="plains" href="#"><s:text name="global.plains" /></a>
        <a id="ships" href="#"><s:text name="global.ships" /></a>
    </div>

    <div id="content">
        <!-- HERE GOES THE PAGE LOADED WHEN I CLICK [cars.jsp | plains.jsp | ships.jsp] -->
        <!-- I USE JQUERY .load() FUNCTION -->
        <!-- Of course I use struts-tags within cars.jsp, plains.jsp and ships.jsp -->
    </div>

    <div id="footer">
        <s:url id="indexES" namespace="/" action="locale" >
           <s:param name="request_locale" >es</s:param>
        </s:url>
        <s:url id="indexEN" namespace="/" action="locale" >
           <s:param name="request_locale" >en</s:param>
        </s:url>
        <!-- IF I CLICK ON A LINK BELOW, THE LANGUAGE CHANGES IN ALL THE PAGE EXCEPT THE CONTENT OF #content -->
        <a href="%{indexES}">Spanish</a>
        <a href="%{indexEn}">English</a>
        <s:text name="global.footerInfo" />
    </div>

    <script type="text/javascript">

    $(document).ready(function(){
        $("#cars").on("click", function(){
            $("#content").load("pages/cars.jsp#carsBox");
        });
        $("#plains").on("click", function(){
            $("#content").load("pages/plains.jsp#plainsBox");
        });
        $("#ships").on("click", function(){
            $("#content").load("pages/ships.jsp#shipsBox");
        });
    });

    </script>
</body>
</html>

i18n_en.properties:

global.cars=cars
global.plains=plains
global.ships=ships
global.footerInfo=This is a web page of cars, plains and ships
global.carsInfo=Cars have at least four wheels
global.plainsInfo=Plains have at least two wings
global.shipsInfo=Ships floating on water

i18n_es.properties:

global.cars=coches
global.plains=aviones
global.ships=barcos
global.footerInfo=Esto es una web de coches, aviones y barcos
global.carsInfo=Los coches tienen al menos cuatro ruedas
global.plainsInfo=Los aviones tienen al menos dos alas
global.shipsInfo=Los barcos flotan en el agua

的web.xml:

  <?xml version="1.0" encoding="UTF-8"?>
  <web-app  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
  <display-name>hpNetSimulator</display-name>
  <welcome-file-list>
    <welcome-file>main.jsp</welcome-file>
  </welcome-file-list>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    </web-app>

struts.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.custom.i18n.resources" value="i18n" />

    <package name="default" extends="struts-default" namespace="/">
        <action name="locale" class="vehicles.LocaleAction">
            <result name="success">main.jsp</result>
        </action>
    </package>
</struts>

那么,有谁知道为什么div#内容的内容没有被翻译?

1 个答案:

答案 0 :(得分:1)

这里

$("#cars").on("click", function(){
    $("#content").load("pages/cars.jsp#carsBox");
});

您绕过了框架工作流程,因此失去了i18n功能(以及所有其他功能)。

cars.jsp这样的JSP应该由Actions调用(通过整个Interceptor Stack,包括i18n Interceptor),而不是直接来自另一个JSP。

然后为您的页面创建简单的操作,例如

public class LoadCarsAction extends ActionSupport{
    public String execute(){
        return SUCCESS;
    }
}

在struts config中声明它们:

<action name="cars" class="vehicles.LoadCarsAction">
    <result>pages/cars.jsp</result>
</action>

并用动作调用替换您的JSP调用:

$("#cars").on("click", function(){
    $("#content").load('<s:url namespace="/" action="cars" anchor="carsBox" />');
});

这应该足够了。