1 13 package info.magnolia.cms.i18n; 14 15 import info.magnolia.cms.util.ClasspathResourcesUtil; 16 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.util.Iterator ; 20 import java.util.Locale ; 21 import java.util.MissingResourceException ; 22 import java.util.PropertyResourceBundle ; 23 import java.util.ResourceBundle ; 24 25 import org.apache.commons.collections.IteratorUtils; 26 import org.apache.commons.lang.StringUtils; 27 import org.apache.commons.io.IOUtils; 28 29 30 34 public class DefaultMessagesImpl extends AbstractMessagesImpl { 35 36 40 protected DefaultMessagesImpl(String basename, Locale locale) { 41 super(basename, locale); 42 } 43 44 49 public String get(String key) { 50 if(key == null){ 51 return "??????"; 52 } 53 try { 54 return getBundle().getString(key); 55 } 56 catch (MissingResourceException e) { 57 return "???" + key + "???"; } 59 } 60 61 64 protected ResourceBundle getBundle() { 65 if (bundle == null) { 66 InputStream stream = null; 67 try { 68 stream = ClasspathResourcesUtil.getStream("/" 69 + StringUtils.replace(basename, ".", "/") 70 + "_" 71 + getLocale().getLanguage() 72 + "_" 73 + getLocale().getCountry() 74 + ".properties", false); 75 if (stream == null) { 76 stream = ClasspathResourcesUtil.getStream("/" 77 + StringUtils.replace(basename, ".", "/") 78 + "_" 79 + getLocale().getLanguage() 80 + ".properties", false); 81 } 82 if (stream == null) { 83 stream = ClasspathResourcesUtil.getStream("/" 84 + StringUtils.replace(basename, ".", "/") 85 + "_" 86 + MessagesManager.getDefaultLocale().getLanguage() 87 + ".properties", false); 88 } 89 if (stream == null) { 90 stream = ClasspathResourcesUtil.getStream("/" 91 + StringUtils.replace(basename, ".", "/") 92 + ".properties", false); 93 } 94 95 if (stream != null) { 96 bundle = new PropertyResourceBundle (stream); 97 } 98 else { 99 bundle = ResourceBundle.getBundle(getBasename(), getLocale()); 100 } 101 } 102 catch (IOException e) { 103 log.error("can't load messages for " + basename); 104 } finally { 105 IOUtils.closeQuietly(stream); 106 } 107 } 108 return bundle; 109 } 110 111 public void reload() throws Exception { 112 this.bundle = null; 113 } 114 115 118 public Iterator keys() { 119 return IteratorUtils.asIterator(this.getBundle().getKeys()); 120 } 121 122 } 123 | Popular Tags |