cmslocalemanager.java
来自「找了很久才找到到源代码」· Java 代码 · 共 932 行 · 第 1/3 页
JAVA
932 行
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/i18n/CmsLocaleManager.java,v $
* Date : $Date: 2007-08-29 16:22:22 $
* Version: $Revision: 1.55 $
*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (c) 2002 - 2007 Alkacon Software GmbH (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software GmbH, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.i18n;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProject;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsUser;
import org.opencms.main.CmsEvent;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.I_CmsEventListener;
import org.opencms.main.OpenCms;
import org.opencms.util.CmsStringUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
/**
* Manages the locales configured for this OpenCms installation.<p>
*
* Locale configuration is done in the configuration file <code>opencms-system.xml</code>
* in the <code>opencms/system/internationalization</code> node and it's sub-nodes.<p>
*
* @author Carsten Weinholz
* @author Alexander Kandzior
*
* @version $Revision: 1.55 $
*
* @since 6.0.0
*/
public class CmsLocaleManager implements I_CmsEventListener {
/** Runtime property name for locale handler. */
public static final String LOCALE_HANDLER = "class_locale_handler";
/** Request parameter to force encoding selection. */
public static final String PARAMETER_ENCODING = "__encoding";
/** Request parameter to force locale selection. */
public static final String PARAMETER_LOCALE = "__locale";
/** The log object for this class. */
private static final Log LOG = CmsLog.getLog(CmsLocaleManager.class);
/** The default locale, this is the first configured locale. */
private static Locale m_defaultLocale;
/** The set of available locale names. */
private List m_availableLocales;
/** The default locale names (must be a subset of the available locale names). */
private List m_defaultLocales;
/** Indicates if the locale manager is fully initialized. */
private boolean m_initialized;
/** The configured locale handler. */
private I_CmsLocaleHandler m_localeHandler;
/**
* Initializes a new CmsLocaleManager, called from the configuration.<p>
*/
public CmsLocaleManager() {
setDefaultLocale();
m_availableLocales = new ArrayList();
m_defaultLocales = new ArrayList();
m_localeHandler = new CmsDefaultLocaleHandler();
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_I18N_CONFIG_START_0));
}
// register this object as event listener
OpenCms.addCmsEventListener(this, new int[] {I_CmsEventListener.EVENT_CLEAR_CACHES});
}
/**
* Initializes a new CmsLocaleManager, used for OpenCms runlevel 1 (unit tests) only.<p>
*
* @param defaultLocale the default locale to use
*/
public CmsLocaleManager(Locale defaultLocale) {
setDefaultLocale();
m_initialized = false;
m_availableLocales = new ArrayList();
m_defaultLocales = new ArrayList();
m_localeHandler = new CmsDefaultLocaleHandler();
m_defaultLocale = defaultLocale;
m_defaultLocales.add(defaultLocale);
m_availableLocales.add(defaultLocale);
}
/**
* Required for setting the default locale on the first possible time.<p>
*/
static {
setDefaultLocale();
}
/**
* Returns the default locale configured in <code>opencms-system.xml</code>,
* that is the first locale from the list provided
* in the <code>opencms/system/internationalization/localesdefault</code> node.<p>
*
* @return the default locale configured in <code>opencms-system.xml</code>
*/
public static Locale getDefaultLocale() {
return m_defaultLocale;
}
/**
* Returns a locale created from the given full name.<p>
*
* The full name must consist of language code,
* country code(optional), variant(optional) separated by "_".<p>
*
* This method will always return a valid Locale!
* If the provided locale name is not valid (i.e. leads to an Exception
* when trying to create the Locale, then the configured default Locale is returned.<p>
*
* @param localeName the full locale name
* @return the locale or <code>null</code> if not available
*/
public static Locale getLocale(String localeName) {
if (CmsStringUtil.isEmpty(localeName)) {
return getDefaultLocale();
}
Locale locale = null;
if (OpenCms.getMemoryMonitor() != null) {
// this may be used AFTER shutdown
locale = OpenCms.getMemoryMonitor().getCachedLocale(localeName);
}
if (locale != null) {
return locale;
}
try {
String[] localeNames = CmsStringUtil.splitAsArray(localeName, '_');
locale = new Locale(
localeNames[0],
(localeNames.length > 1) ? localeNames[1] : "",
(localeNames.length > 2) ? localeNames[2] : "");
} catch (Throwable t) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_CREATE_LOCALE_FAILED_1, localeName), t);
// map this error to the default locale
locale = getDefaultLocale();
}
if (OpenCms.getMemoryMonitor() != null) {
// this may be used AFTER shutdown
OpenCms.getMemoryMonitor().cacheLocale(localeName, locale);
}
return locale;
}
/**
* Returns the locale names from the given List of locales as a comma separated String.<p>
*
* For example, if the input List contains <code>{@link Locale#ENGLISH}</code> and
* <code>{@link Locale#GERMANY}</code>, the result will be <code>"en, de_DE"</code>.<p>
*
* An empty String is returned if the input is <code>null</code>, or contains no elements.<p>
*
* @param localeNames the locale names to generate a String from
*
* @return the locale names from the given List of locales as a comma separated String
*/
public static String getLocaleNames(List localeNames) {
StringBuffer result = new StringBuffer();
if (localeNames != null) {
Iterator i = localeNames.iterator();
while (i.hasNext()) {
result.append(i.next().toString());
if (i.hasNext()) {
result.append(", ");
}
}
}
return result.toString();
}
/**
* Returns a List of locales from an array of locale names.<p>
*
* @param localeNames array of locale names
* @return a List of locales derived from the given locale names
*/
public static List getLocales(List localeNames) {
List result = new ArrayList(localeNames.size());
for (int i = 0; i < localeNames.size(); i++) {
result.add(getLocale(localeNames.get(i).toString().trim()));
}
return result;
}
/**
* Returns a List of locales from a comma-separated string of locale names.<p>
*
* @param localeNames a comma-separated string of locale names
* @return a List of locales derived from the given locale names
*/
public static List getLocales(String localeNames) {
if (localeNames == null) {
return null;
}
return getLocales(CmsStringUtil.splitAsList(localeNames, ','));
}
/**
* Returns the content encoding set for the given resource.<p>
*
* The content encoding is controlled by the property {@link CmsPropertyDefinition#PROPERTY_CONTENT_ENCODING},
* which can be set on the resource or on a parent folder for all resources in this folder.<p>
*
* In case no encoding has been set, the default encoding from
* {@link org.opencms.main.CmsSystemInfo#getDefaultEncoding()} is returned.<p>
*
* @param cms the current OpenCms user context
* @param res the resource to read the encoding for
*
* @return the content encoding set for the given resource
*/
public static final String getResourceEncoding(CmsObject cms, CmsResource res) {
String encoding = null;
// get the encoding
try {
encoding = cms.readPropertyObject(res, CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, true).getValue();
if (encoding != null) {
encoding = CmsEncoder.lookupEncoding(encoding.trim(), encoding);
}
} catch (CmsException e) {
if (LOG.isInfoEnabled()) {
LOG.info(Messages.get().getBundle().key(Messages.ERR_READ_ENCODING_PROP_1, res.getRootPath()), e);
}
}
if (encoding == null) {
encoding = OpenCms.getSystemInfo().getDefaultEncoding();
}
return encoding;
}
/**
* Sets the default locale of the Java VM to <code>{@link Locale#ENGLISH}</code> if the
* current default has any other language then English set.<p>
*
* This is required because otherwise the default (English) resource bundles
* would not be displayed for the English locale if a translated default locale exists.<p>
*
* Here's an example of how this issues shows up:
* On a German server, the default locale usually is <code>{@link Locale#GERMAN}</code>.
* All English translations for OpenCms are located in the "default" message files, for example
* <code>org.opencms.i18n.message.properties</code>. If the German localization is installed, it will be
* located in <code>org.opencms.i18n.message_de.properties</code>. If user has English selected
* as his locale, the default Java lookup mechanism first tries to find
* <code>org.opencms.i18n.message_en.properties</code>. However, this file does not exist, since the
* English localization is kept in the default file. Next, the Java lookup mechanism tries to find the servers
* default locale, which in this example is German. Since there is a German message file, the Java lookup mechanism
* is finished and uses this German localization, not the default file. Therefore the
* user get the German localization, not the English one.
* Setting the default locale explicitly to English avoids this issue.<p>
*/
private static void setDefaultLocale() {
// set the default locale to english
// this is required because otherwise the default (english) resource bundles
// would not be displayed for the english locale if a translated locale exists
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?