2010-07-06 42 views

回答

5

,我已通過使用下面的代碼

locale.provider=com.sample.web.resource.I18nStruts2Adapter 
locale.resolver=com.sample.web.resource.I18nStruts2Adapter 



package com.sample.web.resource; 

import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.LocaleProvider; 
import com.opensymphony.xwork2.TextProvider; 
import com.opensymphony.xwork2.util.ValueStack; 
import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 
import org.apache.struts2.views.jsp.TagUtils; 
import org.displaytag.Messages; 
import org.displaytag.localization.I18nResourceProvider; 
import org.displaytag.localization.LocaleResolver; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.jsp.PageContext; 
import javax.servlet.jsp.tagext.Tag; 
import java.util.Iterator; 
import java.util.Locale; 


public class I18nStruts2Adapter implements LocaleResolver, I18nResourceProvider { 

    /** 
    * prefix/suffix for missing entries. 
    */ 
    public static final String UNDEFINED_KEY = "???"; //$NON-NLS-1$ 

    /** 
    * logger. 
    */ 
    private static Log log = LogFactory.getLog(I18nStruts2Adapter.class); 

    /** 
    * @see LocaleResolver#resolveLocale(javax.servlet.http.HttpServletRequest) 
    */ 
    public Locale resolveLocale(HttpServletRequest request) { 

     Locale result = null; 
     ValueStack stack = ActionContext.getContext().getValueStack(); 

     Iterator iterator = stack.getRoot().iterator(); 
     while (iterator.hasNext()) { 
      Object o = iterator.next(); 

      if (o instanceof LocaleProvider) { 
       LocaleProvider lp = (LocaleProvider) o; 
       result = lp.getLocale(); 

       break; 
      } 
     } 

     if (result == null) { 
      log.debug("Missing LocalProvider actions, init locale to default"); 
      result = Locale.getDefault(); 
     } 

     return result; 
    } 

    /** 
    * @see I18nResourceProvider#getResource(String, String, 
    *  javax.servlet.jsp.tagext.Tag , javax.servlet.jsp.PageContext) 
    */ 
    public String getResource(String resourceKey, String defaultValue, Tag tag, 
      PageContext pageContext) { 

     // if resourceKey isn't defined either, use defaultValue 
     String key = (resourceKey != null) ? resourceKey : defaultValue; 
     System.out.println("Key :"+key); 
     String message = null; 
     ValueStack stack = TagUtils.getStack(pageContext); 
     Iterator iterator = stack.getRoot().iterator(); 

     while (iterator.hasNext()) { 
      Object o = iterator.next(); 

      if (o instanceof TextProvider) { 
       TextProvider tp = (TextProvider) o; 
       message = tp.getText(key); 

       break; 
      } 
     } 

     // if user explicitely added a titleKey we guess this is an error 
     if (message == null && resourceKey != null) { 
      log.debug(Messages 
        .getString("Localization.missingkey", resourceKey)); //$NON-NLS-1$ 
      message = UNDEFINED_KEY + resourceKey + UNDEFINED_KEY; 
     } 

     return message; 
    } 

} 
解決它
1

請顯示一些代碼並排除故障。

使用<s:text name="my.key"/>確保您的標籤已正確加載到該頁面上。如果顯示my.key的值,那麼您知道您的語言文件已正確加載。

如果這是行之有效的titleKey="my.key"財產display:column也應該工作。另外titleKey是大小寫敏感的,它不是titlekey。

在顯示tag.properties
相關問題