1 package org.jahia.deprecated.taglibs.resourcebundle; 2 3 import java.io.IOException ; 4 import java.util.Locale ; 5 import java.util.MissingResourceException ; 6 import java.util.ResourceBundle ; 7 8 import javax.servlet.http.HttpServletRequest ; 9 import javax.servlet.jsp.JspException ; 10 import javax.servlet.jsp.JspWriter ; 11 import javax.servlet.jsp.tagext.TagSupport ; 12 13 import org.jahia.data.JahiaData; 14 15 16 22 public class ResourceBundleTag extends TagSupport { 23 24 private static org.apache.log4j.Logger logger = 25 org.apache.log4j.Logger.getLogger(ResourceBundleTag.class); 26 27 private String resourceBundle = ""; 28 private String resourceName = ""; 29 private String defaultValue = ""; 30 private String localeLangage = ""; 31 private String localeCountry = ""; 32 private String localeVariant = ""; 33 private Locale locale = null; 34 35 public void setResourceBundle(String resourceBundle) { 36 if ( resourceBundle == null ) 37 resourceBundle = ""; 38 this.resourceBundle = resourceBundle.trim(); 39 } 40 41 public void setResourceName(String resourceName) { 42 if ( resourceName == null ) 43 resourceName = ""; 44 this.resourceName = resourceName; 45 } 46 47 public void setDefaultValue(String value) { 48 this.defaultValue = value; 49 } 50 51 public void setLocaleLangage(String localeLangage) { 52 if ( localeLangage != null ) 53 this.localeLangage = localeLangage.trim(); 54 } 55 56 public void setLocaleCountry(String localeCountry) { 57 if ( localeCountry != null ) 58 this.localeCountry = localeCountry.trim(); 59 } 60 61 public void setLocaleVariant(String localeCountry) { 62 if ( localeVariant != null ) 63 this.localeVariant = localeVariant.trim(); 64 } 65 66 67 public int doStartTag() { 68 69 HttpServletRequest request = (HttpServletRequest )pageContext.getRequest(); 70 JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData"); 71 72 ResourceBundle res = null; 73 String resValue = null; 74 75 if ( localeLangage.equals("") ){ 76 locale = jData.params().getLocale(); 77 } else { 78 locale = new Locale (localeLangage,localeCountry,localeVariant); 79 } 80 try { 81 res = ResourceBundle.getBundle(resourceBundle,locale); 82 resValue = res.getString(resourceName); 83 } catch ( MissingResourceException mre ) { 84 logger.warn("Error accessing resource " + resourceName + 85 " in bundle " + resourceBundle + " for locale " + 86 locale + ":" + mre.getMessage()); 87 } 88 if ( resValue == null ){ 89 resValue = this.defaultValue; 90 } 91 92 if (resValue != null) { 93 try { 94 JspWriter out = pageContext.getOut(); 95 out.print( resValue ); 96 } catch (IOException ioe) { 97 logger.error("Error:", ioe); 98 } 99 } 100 return SKIP_BODY; 101 } 102 103 public int doEndTag() throws JspException { 104 resourceBundle = ""; 107 resourceName = ""; 108 localeLangage = ""; 109 localeCountry = ""; 110 localeVariant = ""; 111 locale = null; 112 return EVAL_PAGE; 113 } 114 115 } 116 | Popular Tags |