cmslocalemanager.java
来自「找了很久才找到到源代码」· Java 代码 · 共 932 行 · 第 1/3 页
JAVA
932 行
Locale oldLocale = Locale.getDefault();
if (!(Locale.ENGLISH.getLanguage().equals(oldLocale.getLanguage()))) {
// default language is not English
try {
Locale.setDefault(Locale.ENGLISH);
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(Messages.get().getBundle().key(
Messages.INIT_I18N_DEFAULT_LOCALE_2,
Locale.ENGLISH,
oldLocale));
}
} catch (Exception e) {
// any Exception: the locale has not been changed, so there may be issues with the English
// localization but OpenCms will run in general
CmsLog.INIT.error(Messages.get().getBundle().key(
Messages.LOG_UNABLE_TO_SET_DEFAULT_LOCALE_2,
Locale.ENGLISH,
oldLocale), e);
}
} else {
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_I18N_KEEPING_DEFAULT_LOCALE_1, oldLocale));
}
}
// initialize the static member with the new default
m_defaultLocale = Locale.getDefault();
}
/**
* Adds a locale to the list of available locales.<p>
*
* @param localeName the locale to add
*/
public void addAvailableLocale(String localeName) {
Locale locale = getLocale(localeName);
// add full variation (language / country / variant)
if (!m_availableLocales.contains(locale)) {
m_availableLocales.add(locale);
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_I18N_CONFIG_ADD_LOCALE_1, locale));
}
}
// add variation with only language and country
locale = new Locale(locale.getLanguage(), locale.getCountry());
if (!m_availableLocales.contains(locale)) {
m_availableLocales.add(locale);
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_I18N_CONFIG_ADD_LOCALE_1, locale));
}
}
// add variation with language only
locale = new Locale(locale.getLanguage());
if (!m_availableLocales.contains(locale)) {
m_availableLocales.add(locale);
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_I18N_CONFIG_ADD_LOCALE_1, locale));
}
}
}
/**
* Adds a locale to the list of default locales.<p>
*
* @param localeName the locale to add
*/
public void addDefaultLocale(String localeName) {
Locale locale = getLocale(localeName);
if (!m_defaultLocales.contains(locale)) {
m_defaultLocales.add(locale);
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(Messages.get().getBundle().key(
Messages.INIT_I18N_CONFIG_DEFAULT_LOCALE_2,
new Integer(m_defaultLocales.size()),
locale));
}
}
}
/**
* Implements the CmsEvent interface,
* the locale manager the events to clear
* the list of cached keys .<p>
*
* @param event CmsEvent that has occurred
*/
public void cmsEvent(CmsEvent event) {
switch (event.getType()) {
case I_CmsEventListener.EVENT_CLEAR_CACHES:
clearCaches();
break;
default: // no operation
}
}
/**
* Returns the list of available {@link Locale}s configured in <code>opencms-system.xml</code>,
* in the <code>opencms/system/internationalization/localesconfigured</code> node.<p>
*
* The list of configured available locales contains all locales that are allowed to be used in the VFS,
* for example as languages in XML content files.<p>
*
* The available locales are a superset of the default locales, see {@link #getDefaultLocales()}.<p>
*
* It's possible to reduce the system default by setting the propery
* <code>{@link CmsPropertyDefinition#PROPERTY_AVAILABLE_LOCALES}</code>
* to a comma separated list of locale names. However, you can not add new available locales,
* only remove from the configured list.<p>
*
* @return the list of available locale names, e.g. <code>en, de</code>
*
* @see #getDefaultLocales()
*/
public List getAvailableLocales() {
return m_availableLocales;
}
/**
* Returns an array of available locale names for the given resource.<p>
*
* @param cms the current cms permission object
* @param resourceName the name of the resource
*
* @return an array of available locale names
*
* @see #getAvailableLocales()
*/
public List getAvailableLocales(CmsObject cms, String resourceName) {
String availableNames = null;
try {
availableNames = cms.readPropertyObject(
resourceName,
CmsPropertyDefinition.PROPERTY_AVAILABLE_LOCALES,
true).getValue();
} catch (CmsException exc) {
// noop
}
List result = null;
if (availableNames != null) {
result = getAvailableLocales(availableNames);
}
if ((result == null) || (result.size() == 0)) {
return m_availableLocales;
} else {
return result;
}
}
/**
* Returns a List of available locales from a comma separated string of locale names.<p>
*
* All names are filtered against the allowed available locales
* configured in <code>opencms-system.xml</code>.<P>
*
* @param names a comma-separated String of locale names
* @return List of locales created from the given locale names
*
* @see #getAvailableLocales()
*/
public List getAvailableLocales(String names) {
return checkLocaleNames(getLocales(names));
}
/**
* Tries to find the given requested locale (eventually simplified) in the collection of available locales,
* if the requested locale is not found it will return the first match from the given list of default locales.<p>
*
* @param requestedLocale the requested locale, if this (or a simplified version of it) is available it will be returned
* @param defaults a list of default locales to use in case the requested locale is not available
* @param available the available locales to find a match in
*
* @return the best matching locale name or null if no name matches
*/
public Locale getBestMatchingLocale(Locale requestedLocale, List defaults, Collection available) {
if ((available == null) || available.isEmpty()) {
// no locales are available at all
return null;
}
// the requested locale is the match we want to find most
if (available.contains(requestedLocale)) {
// check if the requested locale is directly available
return requestedLocale;
}
if (requestedLocale.getVariant().length() > 0) {
// locale has a variant like "en_EN_whatever", try only with language and country
Locale check = new Locale(requestedLocale.getLanguage(), requestedLocale.getCountry(), "");
if (available.contains(check)) {
return check;
}
}
if (requestedLocale.getCountry().length() > 0) {
// locale has a country like "en_EN", try only with language
Locale check = new Locale(requestedLocale.getLanguage(), "", "");
if (available.contains(check)) {
return check;
}
}
// available locales do not match the requested locale
if ((defaults == null) || defaults.isEmpty()) {
// if we have no default locales we are out of luck
return null;
}
// no match found for the requested locale, return the first match from the default locales
return getFirstMatchingLocale(defaults, available);
}
/**
* Returns the "the" default locale for the given resource.<p>
*
* It's possible to override the system default (see {@link #getDefaultLocale()}) by setting the property
* <code>{@link CmsPropertyDefinition#PROPERTY_LOCALE}</code> to a comma separated list of locale names.
* This property is inherited from the parent folders.
* This method will return the first locale from that list.<p>
*
* The default locale must be contained in the set of configured available locales,
* see {@link #getAvailableLocales()}.
* In case an invalid locale has been set with the property, this locale is ignored and the
* same result as {@link #getDefaultLocale()} is returned.<p>
*
* In case the property <code>{@link CmsPropertyDefinition#PROPERTY_LOCALE}</code> has not been set
* on the resource or a parent folder,
* this method returns the same result as {@link #getDefaultLocale()}.<p>
*
* @param cms the current cms permission object
* @param resourceName the name of the resource
* @return an array of default locale names
*
* @see #getDefaultLocales()
* @see #getDefaultLocales(CmsObject, String)
*/
public Locale getDefaultLocale(CmsObject cms, String resourceName) {
List defaultLocales = getDefaultLocales(cms, resourceName);
Locale result;
if (defaultLocales.size() > 0) {
result = (Locale)defaultLocales.get(0);
} else {
result = getDefaultLocale();
}
return result;
}
/**
* Returns the list of default {@link Locale}s configured in <code>opencms-system.xml</code>,
* in the <code>opencms/system/internationalization/localesdefault</code> node.<p>
*
* Since the default locale is always available, the result list will always contain at least one Locale.<p>
*
* It's possible to override the system default by setting the property
* <code>{@link CmsPropertyDefinition#PROPERTY_LOCALE}</code> to a comma separated list of locale names.
* This property is inherited from the parent folders.<p>
*
* The default locales must be a subset of the configured available locales, see {@link #getAvailableLocales()}.
* In case an invalid locale has been set with the property, this locale is ignored.<p>
*
* The default locale names are used as a fallback mechanism in case a locale is requested
* that can not be found, for example when delivering content form an XML content.<p>
*
* There is a list of default locales (instead of just one default locale) since there
* are scenarios when one default is not enough. Consider the following example:<i>
* The main default locale is set to "en". An example XML content file contains just one language,
* in this case "de" and not "en". Now a request is made to the file for the locale "fr". If
* there would be only one default locale ("en"), we would have to give up. But since we allow more then
* one default, we can deliver the "de" content instead of a blank page.</I><p>
*
* @return the list of default locale names, e.g. <code>en, de</code>
*
* @see #getAvailableLocales()
*/
public List getDefaultLocales() {
return m_defaultLocales;
}
/**
* Returns an array of default locales for the given resource.<p>
*
* Since the default locale is always available, the result list will always contain at least one Locale.<p>
*
* It's possible to override the system default (see {@link #getDefaultLocales()}) by setting the property
* <code>{@link CmsPropertyDefinition#PROPERTY_LOCALE}</code> to a comma separated list of locale names.
* This property is inherited from the parent folders.<p>
*
* The default locales must be a subset of the configured available locales, see {@link #getAvailableLocales()}.
* In case an invalid locale has been set with the property, this locale is ignored.<p>
*
* In case the property <code>{@link CmsPropertyDefinition#PROPERTY_LOCALE}</code> has not been set
* on the resource or a parent folder,
* this method returns the same result as {@link #getDefaultLocales()}.<p>
*
* Use this method in case you need to get all configured default options for a resource,
* if you just need the "the" default locale for a resource,
* use <code>{@link #getDefaultLocale(CmsObject, String)}</code>.<p>
*
* @param cms the current cms permission object
* @param resource the resource to read the default locale properties for
* @return an array of default locale names
*
* @see #getDefaultLocales()
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?