📄 cmslocalemanager.java
字号:
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
*/
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.properties</code>.<P>
*
* @param names a comma-separated String of locale names
* @return List of locales created from the given locale names
*/
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 "best" default locale for the given resource.<p>
*
* @param cms the current cms permission object
* @param resourceName the name of the resource
* @return an array of default locale names
*
* @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 locale names configured in <code>opencms.properties</code>.<p>
*
* @return the list of default locale names, e.g. <code>en, de</code>
*/
public List getDefaultLocales() {
return m_defaultLocales;
}
/**
* Returns an array of default locales for the given resource.<p>
*
* Use this method in case you need to get all available default options for a resource,
* if you just need the "best" default locale for a resource,
* use <code>{@link #getDefaultLocale(CmsObject, String)}</code>.<p>
*
* @param cms the current cms permission object
* @param resourceName the name of the resource
* @return an array of default locale names
*
* @see #getDefaultLocale(CmsObject, String)
*/
public List getDefaultLocales(CmsObject cms, String resourceName) {
String defaultNames = null;
try {
defaultNames = cms.readPropertyObject(resourceName, CmsPropertyDefinition.PROPERTY_LOCALE, true).getValue();
} catch (CmsException e) {
LOG.warn(Messages.get().getBundle().key(Messages.ERR_READ_ENCODING_PROP_1, resourceName), e);
}
List result = null;
if (defaultNames != null) {
result = getAvailableLocales(defaultNames);
}
if ((result == null) || (result.size() == 0)) {
return m_defaultLocales;
} else {
return result;
}
}
/**
* Returns the first matching locale (eventually simplified) from the available locales.<p>
*
* @param locales must be an ascending sorted list of locales in order of preference
* @param available the available locales to find a match in
*
* @return the first precise or simplified match
*/
public Locale getFirstMatchingLocale(List locales, Collection available) {
Iterator i;
// first try a precise match
i = locales.iterator();
while (i.hasNext()) {
Locale locale = (Locale)i.next();
if (available.contains(locale)) {
// precise match
return locale;
}
}
// now try a match only with language and country
i = locales.iterator();
while (i.hasNext()) {
Locale locale = (Locale)i.next();
if (locale.getVariant().length() > 0) {
// the locale has a variant, try to match without the variant
locale = new Locale(locale.getLanguage(), locale.getCountry(), "");
if (available.contains(locale)) {
// match
return locale;
}
}
}
// finally try a match only with language
i = locales.iterator();
while (i.hasNext()) {
Locale locale = (Locale)i.next();
if (locale.getCountry().length() > 0) {
// the locale has a country, try to match without the country
locale = new Locale(locale.getLanguage(), "", "");
if (available.contains(locale)) {
// match
return locale;
}
}
}
// no match
return null;
}
/**
* Returns the the appropriate locale/encoding for a request,
* using the "right" locale handler for the given resource.<p>
*
* Certain system folders (like the Workplace) require a special
* locale handler different from the configured handler.
* Use this method if you want to resolve locales exactly like
* the system does for a request.<p>
*
* @param req the current http request
* @param user the current user
* @param project the current project
* @param resource the URI of the requested resource (with full site root added)
*
* @return the i18n information to use for the given request context
*/
public CmsI18nInfo getI18nInfo(HttpServletRequest req, CmsUser user, CmsProject project, String resource) {
CmsI18nInfo i18nInfo = null;
// check if this is a request against a Workplace folder
if (OpenCms.getSiteManager().isWorkplaceRequest(req)) {
// The list of configured localized workplace folders
List wpLocalizedFolders = OpenCms.getWorkplaceManager().getLocalizedFolders();
for (int i = wpLocalizedFolders.size() - 1; i >= 0; i--) {
if (resource.startsWith((String)wpLocalizedFolders.get(i))) {
// use the workplace locale handler for this resource
i18nInfo = OpenCms.getWorkplaceManager().getI18nInfo(req, user, project, resource);
break;
}
}
}
if (i18nInfo == null) {
// use default locale handler
i18nInfo = m_localeHandler.getI18nInfo(req, user, project, resource);
}
// check the request for special parameters overriding the locale handler
Locale locale = null;
String encoding = null;
if (req != null) {
String localeParam = req.getParameter(CmsLocaleManager.PARAMETER_LOCALE);
// check request for parameters
if (localeParam != null) {
// "__locale" parameter found in request
locale = CmsLocaleManager.getLocale(localeParam);
}
// check for "__encoding" parameter in request
encoding = req.getParameter(CmsLocaleManager.PARAMETER_ENCODING);
}
// merge values from request with values from locale handler
if (locale == null) {
locale = i18nInfo.getLocale();
}
if (encoding == null) {
encoding = i18nInfo.getEncoding();
}
// still some values might be "null"
if (locale == null) {
locale = getDefaultLocale();
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_LOCALE_NOT_FOUND_1, locale));
}
}
if (encoding == null) {
encoding = OpenCms.getSystemInfo().getDefaultEncoding();
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_ENCODING_NOT_FOUND_1, encoding));
}
}
// return the merged values
return new CmsI18nInfo(locale, encoding);
}
/**
* Returns the configured locale handler.<p>
* This handler is used to derive the appropriate locale/encoding for a request.
*
* @return the locale handler
*/
public I_CmsLocaleHandler getLocaleHandler() {
return m_localeHandler;
}
/**
* Initializes this locale manager with the OpenCms system configuration.<p>
*
* @param cms an OpenCms context object that must have been initialized with "Admin" permissions
*/
public void initialize(CmsObject cms) {
// init the locale handler
m_localeHandler.initHandler(cms);
// set default locale
m_defaultLocale = (Locale)m_defaultLocales.get(0);
// set initialized status
m_initialized = true;
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_I18N_CONFIG_VFSACCESS_0));
}
}
/**
* Returns <code>true</code> if this locale manager is fully initialized.<p>
*
* This is required to prevent errors during unit tests,
* simple unit tests will usually not have a fully
* initialized locale manager available.<p>
*
* @return true if the locale manager is fully initialized
*/
public boolean isInitialized() {
return m_initialized;
}
/**
* Sets the configured locale handler.<p>
*
* @param localeHandler the locale handler to set
*/
public void setLocaleHandler(I_CmsLocaleHandler localeHandler) {
if (localeHandler != null) {
m_localeHandler = localeHandler;
}
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(Messages.get().getBundle().key(
Messages.INIT_I18N_CONFIG_LOC_HANDLER_1,
m_localeHandler.getClass().getName()));
}
}
/**
* Returns a list of available locale names derived from the given locale names.<p>
*
* Each name in the given list is checked against the internal hash map of allowed locales,
* and is appended to the resulting list only if the locale exists.<p>
*
* @param locales List of locales to check
* @return list of available locales derived from the given locale names
*/
private List checkLocaleNames(List locales) {
if (locales == null) {
return null;
}
List result = new ArrayList();
Iterator i = locales.iterator();
while (i.hasNext()) {
Locale locale = (Locale)i.next();
if (m_availableLocales.contains(locale)) {
result.add(locale);
}
}
return result;
}
/**
* Clears the caches in the locale manager.<p>
*/
private void clearCaches() {
// flush all caches
m_localeCache.clear();
CmsResourceBundleLoader.flushBundleCache();
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_LOCALE_MANAGER_FLUSH_CACHE_1, "EVENT_CLEAR_CACHES"));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -