1 43 package net.jforum.util; 44 45 import java.io.FileInputStream ; 46 import java.io.IOException ; 47 import java.text.MessageFormat ; 48 import java.util.ArrayList ; 49 import java.util.HashMap ; 50 import java.util.List ; 51 import java.util.Map ; 52 import java.util.Properties ; 53 54 import net.jforum.SessionFacade; 55 import net.jforum.entities.UserSession; 56 import net.jforum.util.preferences.ConfigKeys; 57 import net.jforum.util.preferences.SystemGlobals; 58 59 import org.apache.log4j.Logger; 60 61 69 public class I18n 70 { 71 private static I18n classInstance = new I18n(); 72 private static Map messagesMap = new HashMap (); 73 private static Properties localeNames = new Properties (); 74 private static String defaultName; 75 private static String baseDir; 76 private static List watching = new ArrayList (); 77 public static final String CANNOT_DELETE_GROUP = "CannotDeleteGroup"; 78 public static final String CANNOT_DELETE_CATEGORY = "CannotDeleteCategory"; 79 public static final String CANNOT_DELETE_BANNER = "CannotDeleteBanner"; 80 private static final Logger logger = Logger.getLogger(I18n.class); 81 82 private I18n() {} 83 84 89 public static I18n getInstance() 90 { 91 return classInstance; 92 } 93 94 99 public static synchronized void load() throws IOException 100 { 101 baseDir = SystemGlobals.getApplicationResourceDir() + "/" + SystemGlobals.getValue(ConfigKeys.LOCALES_DIR); 102 103 loadLocales(); 104 105 defaultName = SystemGlobals.getValue(ConfigKeys.I18N_DEFAULT_ADMIN); 106 load(defaultName, null); 107 108 String custom = SystemGlobals.getValue(ConfigKeys.I18N_DEFAULT); 109 if (!custom.equals(defaultName)) { 110 load(custom, defaultName); 111 defaultName = custom; 112 } 113 } 114 115 public static void changeBoardDefault(String newDefaultLanguage) throws Exception 116 { 117 load(newDefaultLanguage, SystemGlobals.getValue(ConfigKeys.I18N_DEFAULT_ADMIN)); 118 defaultName = newDefaultLanguage; 119 } 120 121 private static void loadLocales() throws IOException 122 { 123 localeNames.load(new FileInputStream (baseDir + SystemGlobals.getValue(ConfigKeys.LOCALES_NAMES))); 124 } 125 126 static void load(String localeName, String mergeWith) throws IOException 127 { 128 load(localeName, mergeWith, false); 129 } 130 131 static void load(String localeName, String mergeWith, boolean force) throws IOException 132 { 133 if (!force && (localeName == null || localeName.trim().equals("") || I18n.contains(localeName))) { 134 return; 135 } 136 137 if (localeNames.size() == 0) { 138 loadLocales(); 139 } 140 141 Properties p = new Properties (); 142 143 if (mergeWith != null) { 144 if (!I18n.contains(mergeWith)) { 145 load(mergeWith, null); 146 } 147 148 p.putAll((Properties ) messagesMap.get(mergeWith)); 149 } 150 151 p.load(new FileInputStream (baseDir + localeNames.getProperty(localeName))); 152 messagesMap.put(localeName, p); 153 154 watchForChanges(localeName); 155 } 156 157 165 public static void load(String localeName) throws IOException 166 { 167 load(localeName, SystemGlobals.getValue(ConfigKeys.I18N_DEFAULT)); 168 } 169 170 public static void reset() 171 { 172 messagesMap = new HashMap (); 173 localeNames = new Properties (); 174 defaultName = null; 175 } 176 177 private static void watchForChanges(final String localeName) 178 { 179 if (!watching.contains(localeName)) { 180 watching.add(localeName); 181 182 int fileChangesDelay = SystemGlobals.getIntValue(ConfigKeys.FILECHANGES_DELAY); 183 184 if (fileChangesDelay > 0) { 185 FileMonitor.getInstance().addFileChangeListener(new FileChangeListener() { 186 189 public void fileChanged(String filename) 190 { 191 logger.info("Reloading i18n for " + localeName); 192 193 try { 194 I18n.load(localeName, SystemGlobals.getValue(ConfigKeys.I18N_DEFAULT), true); 195 } 196 catch (IOException e) { 197 logger.warn(e); 198 e.printStackTrace(); 199 } 200 } 201 }, baseDir + localeNames.getProperty(localeName), fileChangesDelay); 202 } 203 } 204 } 205 206 219 public static String getMessage(String localeName, String messageName, Object params[]) 220 { 221 return MessageFormat.format(((Properties ) messagesMap.get(localeName)).getProperty(messageName), params); 222 } 223 224 227 public static String getMessage(String messageName, Object params[]) 228 { 229 String lang = ""; 230 UserSession us = SessionFacade.getUserSession(); 231 if (us != null && us.getLang() != null) { 232 lang = us.getLang(); 233 } 234 235 if ("".equals(lang)) { 236 return getMessage(defaultName, messageName, params); 237 } 238 239 return getMessage(lang, messageName, params); 240 } 241 242 250 public static String getMessage(String localeName, String m) 251 { 252 if (!messagesMap.containsKey(localeName)) { 253 try { 254 load(localeName); 255 } 256 catch (IOException e) { 257 logger.warn("Error loading locale " + localeName + ". " + e.getMessage()); 258 return null; 259 } 260 } 261 262 return (((Properties ) messagesMap.get(localeName)).getProperty(m)); 263 } 264 265 public static String getMessage(String m) 266 { 267 return getMessage(getUserLanguage(), m); 268 } 269 270 public static String getMessage(String m, UserSession us) 271 { 272 if (us == null || us.getLang() == null || us.getLang().equals("")) { 273 return getMessage(defaultName, m); 274 } 275 276 return getMessage(us.getLang(), m); 277 } 278 279 285 public static String getUserLanguage() 286 { 287 UserSession us = SessionFacade.getUserSession(); 288 289 if (us == null || us.getLang() == null || us.getLang().trim().equals("")) { 290 return defaultName; 291 } 292 293 return us.getLang(); 294 } 295 296 302 public static boolean contains(String language) 303 { 304 return messagesMap.containsKey(language); 305 } 306 307 314 public static boolean languageExists(String language) 315 { 316 return (localeNames.getProperty(language) != null); 317 } 318 } | Popular Tags |