jspページに表示されるjavaコードの国際化


プロジェクトはstruts 1の、1つの古いプロジェクトのコードで、1つのプレゼンテーションのプロジェクトをして、持ってきて使って、その中で国際化が必要で、struts 1の持参した国際化の策略でページの上のホームページの要素に対して解決しました.しかし、プロジェクトがきついため、多くの新しく追加された機能がjspページに追加され、jspページにはいくつかの<%...%>が現れた.その中のJavaコードはstruts 1が提供するメカニズムを利用して国際化することはできません.
次のクラスがあります.

  
  
  
  
  1. /**   
  2. * jsp java    
  3. */package com.platform.I18N;   
  4. import java.util.MissingResourceException;   
  5. import java.util.ResourceBundle;   
  6. import javax.servlet.http.HttpServletRequest;   
  7. import com.platform.common.PropertiesHelper;     
  8. public class MessageResource {       
  9. public static String getString(HttpServletRequest request, String file, String key){           
  10. String ret = "";           
  11. String prefix = "";           
  12. //   substring,            
  13. String language = (String)request.getHeader("accept-language").substring(0,2);           
  14. // ,           
  15. PropertiesHelper helper = new PropertiesHelper();           
  16. String resourceName = helper.getPropertise4FileNodename("MessageResource.properties", file);           
  17. //            
  18. if ("zh".equals(language)){               
  19. prefix = "_zh_CN";           
  20. }else if ("en".equals(language)){               
  21. prefix = "_en_US";           
  22. }           
  23. //            
  24. ResourceBundle Resource_Bundle = ResourceBundle.getBundle(resourceName+prefix);           
  25. try{               
  26. ret = Resource_Bundle.getString(key);           
  27. }catch(MissingResourceException e){               
  28. e.printStackTrace();           
  29. }           
  30. return ret;       
  31. } } 

コードに注釈がはっきりしています...しばらくしてから、祈りが読めるように...
 
小菜コミュニティ:http://www.itcaicai.com