1 package org.alfresco.i18n; 2 3 import java.text.MessageFormat ; 4 import java.util.Enumeration ; 5 import java.util.HashMap ; 6 import java.util.HashSet ; 7 import java.util.Locale ; 8 import java.util.Map ; 9 import java.util.ResourceBundle ; 10 import java.util.Set ; 11 import java.util.concurrent.locks.Lock ; 12 import java.util.concurrent.locks.ReadWriteLock ; 13 import java.util.concurrent.locks.ReentrantReadWriteLock ; 14 15 21 public class I18NUtil 22 { 23 26 private static ThreadLocal <Locale > currentLocale = new ThreadLocal <Locale >(); 27 28 31 private static Set <String > resouceBundleBaseNames = new HashSet <String >(); 32 33 36 private static Map <Locale , Set <String >> loadedResourceBundles = new HashMap <Locale , Set <String >>(); 37 38 41 private static Map <Locale , Map <String , String >> cachedMessages = new HashMap <Locale , Map <String , String >>(); 42 43 46 private static ReadWriteLock lock = new ReentrantReadWriteLock (); 47 private static Lock readLock = lock.readLock(); 48 private static Lock writeLock = lock.writeLock(); 49 50 55 public static void setLocale(Locale locale) 56 { 57 currentLocale.set(locale); 58 } 59 60 66 public static Locale getLocale() 67 { 68 Locale locale = currentLocale.get(); 69 if (locale == null) 70 { 71 locale = Locale.getDefault(); 73 } 74 return locale; 75 } 76 77 86 public static void registerResourceBundle(String bundleBaseName) 87 { 88 try 89 { 90 writeLock.lock(); 91 resouceBundleBaseNames.add(bundleBaseName); 92 } 93 finally 94 { 95 writeLock.unlock(); 96 } 97 } 98 99 105 public static String getMessage(String messageKey) 106 { 107 return getMessage(messageKey, getLocale()); 108 } 109 110 117 public static String getMessage(String messageKey, Locale locale) 118 { 119 String message = null; 120 Map <String , String > props = getLocaleProperties(locale); 121 if (props != null) 122 { 123 message = props.get(messageKey); 124 } 125 return message; 126 } 127 128 135 public static String getMessage(String messageKey, Object ... params) 136 { 137 return getMessage(messageKey, getLocale(), params); 138 } 139 140 148 public static String getMessage(String messageKey, Locale locale, Object ... params) 149 { 150 String message = getMessage(messageKey, locale); 151 if (message != null && params != null) 152 { 153 message = MessageFormat.format(message, params); 154 } 155 return message; 156 } 157 158 166 private static Map <String , String > getLocaleProperties(Locale locale) 167 { 168 Set <String > loadedBundles = null; 169 Map <String , String > props = null; 170 int loadedBundleCount = 0; 171 try 172 { 173 readLock.lock(); 174 loadedBundles = loadedResourceBundles.get(locale); 175 props = cachedMessages.get(locale); 176 loadedBundleCount = resouceBundleBaseNames.size(); 177 } 178 finally 179 { 180 readLock.unlock(); 181 } 182 183 if (loadedBundles == null) 184 { 185 try 186 { 187 writeLock.lock(); 188 loadedBundles = new HashSet <String >(); 189 loadedResourceBundles.put(locale, loadedBundles); 190 } 191 finally 192 { 193 writeLock.unlock(); 194 } 195 } 196 197 if (props == null) 198 { 199 try 200 { 201 writeLock.lock(); 202 props = new HashMap <String , String >(); 203 cachedMessages.put(locale, props); 204 } 205 finally 206 { 207 writeLock.unlock(); 208 } 209 } 210 211 if (loadedBundles.size() != loadedBundleCount) 212 { 213 try 214 { 215 writeLock.lock(); 216 for (String resourceBundleBaseName : resouceBundleBaseNames) 217 { 218 if (loadedBundles.contains(resourceBundleBaseName) == false) 219 { 220 ResourceBundle resourcebundle = ResourceBundle.getBundle(resourceBundleBaseName, locale); 221 Enumeration <String > enumKeys = resourcebundle.getKeys(); 222 while (enumKeys.hasMoreElements() == true) 223 { 224 String key = enumKeys.nextElement(); 225 props.put(key, resourcebundle.getString(key)); 226 } 227 loadedBundles.add(resourceBundleBaseName); 228 } 229 } 230 } 231 finally 232 { 233 writeLock.unlock(); 234 } 235 } 236 237 return props; 238 } 239 } 240 | Popular Tags |