1 28 package com.opencms.workplace; 29 30 42 import org.opencms.file.CmsObject; 43 import org.opencms.file.CmsPropertyDefinition; 44 import org.opencms.i18n.CmsMessageException; 45 import org.opencms.i18n.CmsMessages; 46 import org.opencms.i18n.CmsResourceBundleLoader; 47 import org.opencms.main.CmsException; 48 import org.opencms.main.CmsLog; 49 import org.opencms.main.OpenCms; 50 51 import java.util.HashMap ; 52 import java.util.HashSet ; 53 import java.util.Iterator ; 54 import java.util.Locale ; 55 import java.util.Map ; 56 import java.util.MissingResourceException ; 57 import java.util.ResourceBundle ; 58 import java.util.Set ; 59 60 public class CmsXmlLanguageFile { 61 62 63 public static final String C_BUNDLE_NAME = "com.opencms.workplace.workplace"; 64 65 66 public static final String C_COMPATIBILITY_OLDLOCALES = "compatibility.support.oldlocales"; 67 68 69 private CmsMessages m_messages; 70 71 72 private CmsObject m_cms; 73 74 75 private String m_locale; 76 77 79 private static String m_workplaceDefaultEncoding; 80 81 82 private static Map m_allModuleMessages = null; 83 84 85 private static Set m_moduleMessages = null; 86 87 88 private static Map m_allEncodings = null; 89 90 91 private static Boolean m_supportOldLocale = null; 92 93 94 private static final int DEBUG = 0; 95 96 102 public CmsXmlLanguageFile(CmsObject cms) throws CmsException { 103 this(cms, getCurrentUserLanguage(cms)); 104 } 105 106 113 public CmsXmlLanguageFile(CmsObject cms, String locale) throws CmsException { 114 m_cms = cms; 115 m_locale = locale; 116 m_messages = new CmsMessages(C_BUNDLE_NAME, m_locale); 117 if (m_allEncodings == null) { 119 if (DEBUG > 0) System.err.println("CmsXmlLanguageFile(): initializing the static encodings"); 120 synchronized (this) { 121 m_allEncodings = new HashMap (); 122 m_workplaceDefaultEncoding = OpenCms.getWorkplaceManager().getEncoding(); 123 } 124 } 125 if (m_allModuleMessages == null) { 127 if (DEBUG > 0) System.err.println("CmsXmlLanguageFile(): initializing module messages hash"); 128 synchronized (this) { 129 m_allModuleMessages = new HashMap (); 130 } 131 } 132 Object obj = m_allModuleMessages.get(m_locale); 134 if (obj == null) { 135 if (DEBUG > 0) System.err.println("CmsXmlLanguageFile(): collecting static module messages"); 136 synchronized (this) { 137 m_moduleMessages = collectModuleMessages(m_cms, m_locale); 138 m_allModuleMessages.put(m_locale, m_moduleMessages); 139 } 140 } else { 141 m_moduleMessages = (Set )obj; 142 } 143 if (m_supportOldLocale == null) { 144 if (DEBUG > 0) System.err.println("CmsXmlLanguageFile(): reading old locale support property"); 145 synchronized (this) { 146 Boolean flag = Boolean.valueOf((String )OpenCms.getRuntimeProperty(C_COMPATIBILITY_OLDLOCALES)); 148 m_supportOldLocale = (flag != null)?flag:new Boolean (false); 149 if (CmsLog.INIT.isInfoEnabled()) { 150 CmsLog.INIT.info(". Old locale support : " + (m_supportOldLocale.booleanValue() ? "enabled" : "disabled")); 151 } 152 } 153 } 154 } 155 156 165 private synchronized Set collectModuleMessages(CmsObject cms, String locale) { 166 HashSet bundles = new HashSet (); 167 Set names = OpenCms.getModuleManager().getModuleNames(); 168 if (names != null) { 169 Iterator i = names.iterator(); 170 while (i.hasNext()) { 171 String bundleName = ((String )i.next()) + ".workplace"; 172 try { 174 ResourceBundle bundle = CmsResourceBundleLoader.getBundle(bundleName, new Locale (locale)); 175 bundles.add(bundle); 176 } catch (MissingResourceException e) { 177 } 179 } 180 } 181 return bundles; 182 } 183 184 189 public String getEncoding() { 190 String result = (String )m_allEncodings.get(m_locale); 192 if (result != null) return result; 193 194 if (DEBUG > 0) System.err.println("CmsXmlLanguageFile.getEncoding(): looking up encoding for locale " + m_locale); 196 try { 197 result = m_messages.getString(CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING); 198 } catch (CmsMessageException e) { 199 result = m_workplaceDefaultEncoding; 201 } 202 if (result.startsWith("{")) { 203 if (result.indexOf(m_workplaceDefaultEncoding) >= 0) { 205 result = m_workplaceDefaultEncoding; 207 } else { 208 int index = result.indexOf(";"); 210 if (index <= 1) { 211 result = m_workplaceDefaultEncoding; 212 } else { 213 result = result.substring(1, index); 214 } 215 } 216 } 217 m_allEncodings.put(m_locale, result); 219 return result; 220 } 221 222 227 public CmsMessages getMessages() { 228 return m_messages; 229 } 230 231 237 public String getLanguageValue(String keyName) { 238 if (DEBUG > 2) System.err.println("CmsXmlLanguageFile.getLanguageValue(): looking key " + keyName); 239 try { 240 return m_messages.getString(keyName); 241 } catch (CmsMessageException e) {} 242 243 if (DEBUG > 1) System.err.println("CmsXmlLanguageFile.getLanguageValue(): '" + keyName + "' not found in workplace messages"); 245 Iterator i = m_moduleMessages.iterator(); 246 while (i.hasNext()) { 247 try { 248 return ((ResourceBundle ) i.next()).getString(keyName); 249 } catch (MissingResourceException e) { 251 } 253 } 254 if (DEBUG > 1) System.err.println("CmsXmlLanguageFile.getLanguageValue(): '" + keyName + "' also not found in module messages (this is not good)"); 255 if (m_supportOldLocale.booleanValue()) { 256 try { 259 CmsXmlLanguageFileContent langFile = new CmsXmlLanguageFileContent(m_cms, m_locale); 260 String value = langFile.getLanguageValue(keyName); 261 if (value != null) return value; 262 } catch (CmsException e) { 263 } 265 } 266 267 if (keyName.startsWith("help.")) { 268 return "index.html"; 270 } 271 272 if (DEBUG > 1) System.err.println("CmsXmlLanguageFile.getLanguageValue(): '" + keyName + "' not found at all (this is bad)"); 274 if (CmsLog.getLog(this).isWarnEnabled()) { 275 CmsLog.getLog(this).warn("Missing value for locale key: " + keyName); 276 } 277 return "??? " + keyName + " ???"; 278 } 279 280 286 public static String getCurrentUserLanguage(CmsObject cms) { 287 return cms.getRequestContext().getLocale().toString(); 288 } 289 } 290
| Popular Tags
|