1 31 32 package org.opencms.i18n; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.file.CmsProject; 36 import org.opencms.file.CmsPropertyDefinition; 37 import org.opencms.file.CmsResource; 38 import org.opencms.file.CmsUser; 39 import org.opencms.main.CmsException; 40 import org.opencms.main.CmsLog; 41 import org.opencms.main.OpenCms; 42 43 import java.io.UnsupportedEncodingException ; 44 import java.util.List ; 45 import java.util.Locale ; 46 47 import javax.servlet.http.HttpServletRequest ; 48 49 import org.apache.commons.logging.Log; 50 51 61 public class CmsDefaultLocaleHandler implements I_CmsLocaleHandler { 62 63 64 private static final Log LOG = CmsLog.getLog(CmsDefaultLocaleHandler.class); 65 66 67 private CmsObject m_adminCmsObject; 68 69 72 public CmsDefaultLocaleHandler() { 73 74 } 76 77 80 public CmsI18nInfo getI18nInfo(HttpServletRequest req, CmsUser user, CmsProject project, String resourceName) { 81 82 CmsLocaleManager localeManager = OpenCms.getLocaleManager(); 83 List defaultLocales = null; 84 String encoding = null; 85 86 CmsObject adminCms = null; 87 try { 88 adminCms = OpenCms.initCmsObject(m_adminCmsObject); 90 } catch (CmsException e) { 91 } 93 94 if (adminCms != null) { 95 96 adminCms.getRequestContext().setCurrentProject(project); 98 adminCms.getRequestContext().setUri(resourceName); 99 100 CmsResource res = null; 102 try { 103 res = adminCms.readResource(resourceName); 104 } catch (CmsException e) { 105 } 107 if (res == null) { 108 try { 109 res = OpenCms.initResource(adminCms, resourceName, req, null); 110 } catch (CmsException e) { 111 } 113 } 114 115 String defaultNames = null; 116 117 if (res != null) { 118 try { 120 defaultNames = adminCms.readPropertyObject(res, CmsPropertyDefinition.PROPERTY_LOCALE, true).getValue(); 121 } catch (CmsException e) { 122 LOG.warn(Messages.get().getBundle().key(Messages.ERR_READ_ENCODING_PROP_1, resourceName), e); 123 } 124 if (defaultNames != null) { 125 defaultLocales = localeManager.getAvailableLocales(defaultNames); 126 } 127 128 try { 130 encoding = adminCms.readPropertyObject(res, CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, true).getValue( 131 OpenCms.getSystemInfo().getDefaultEncoding()); 132 } catch (CmsException e) { 133 if (LOG.isInfoEnabled()) { 134 LOG.info(Messages.get().getBundle().key(Messages.ERR_READ_ENCODING_PROP_1, resourceName), e); 135 } 136 } 137 } 138 } 139 140 if ((defaultLocales == null) || (defaultLocales.isEmpty())) { 141 defaultLocales = localeManager.getDefaultLocales(); 143 } 144 if (encoding == null) { 145 encoding = OpenCms.getSystemInfo().getDefaultEncoding(); 147 } 148 149 if (req != null) { 151 try { 152 req.setCharacterEncoding(encoding); 153 } catch (UnsupportedEncodingException e) { 154 LOG.error(Messages.get().getBundle().key(Messages.ERR_UNSUPPORTED_REQUEST_ENCODING_1, encoding), e); 155 } 156 } 157 158 Locale locale; 159 if ((defaultLocales != null) && (defaultLocales.size() > 0)) { 161 locale = (Locale )defaultLocales.get(0); 162 } else { 163 locale = CmsLocaleManager.getDefaultLocale(); 164 } 165 166 return new CmsI18nInfo(locale, encoding); 167 } 168 169 172 public void initHandler(CmsObject cms) { 173 174 m_adminCmsObject = cms; 175 } 176 } | Popular Tags |