org.opencms.i18n
Class CmsLocaleManager

java.lang.Object
  extended by org.opencms.i18n.CmsLocaleManager
All Implemented Interfaces:
I_CmsEventListener

public class CmsLocaleManager
extends java.lang.Object
implements I_CmsEventListener

Manages the locales configured for this OpenCms installation.

Locale configuration is done in opencms.xml.

Since:
6.0.0
Version:
$Revision: 1.51 $
Author:
Carsten Weinholz, Alexander Kandzior

Field Summary
static java.lang.String LOCALE_HANDLER
          Runtime property name for locale handler.
static java.lang.String PARAMETER_ENCODING
          Request parameter to force encoding selection.
static java.lang.String PARAMETER_LOCALE
          Request parameter to force locale selection.
 
Fields inherited from interface org.opencms.main.I_CmsEventListener
EVENT_BEFORE_PUBLISH_PROJECT, EVENT_CLEAR_CACHES, EVENT_CLEAR_OFFLINE_CACHES, EVENT_CLEAR_ONLINE_CACHES, EVENT_CLEAR_PRINCIPAL_CACHES, EVENT_FLEX_CACHE_CLEAR, EVENT_FLEX_PURGE_JSP_REPOSITORY, EVENT_FULLSTATIC_EXPORT, EVENT_LOGIN_USER, EVENT_PROJECT_MODIFIED, EVENT_PROPERTY_DEFINITION_CREATED, EVENT_PROPERTY_DEFINITION_MODIFIED, EVENT_PROPERTY_MODIFIED, EVENT_PUBLISH_PROJECT, EVENT_RESOURCE_AND_PROPERTIES_MODIFIED, EVENT_RESOURCE_COPIED, EVENT_RESOURCE_CREATED, EVENT_RESOURCE_DELETED, EVENT_RESOURCE_LIST_MODIFIED, EVENT_RESOURCE_MODIFIED, EVENT_RESOURCE_MOVED, EVENT_RESOURCES_AND_PROPERTIES_MODIFIED, EVENT_RESOURCES_MODIFIED, EVENT_UPDATE_EXPORTS, KEY_DBCONTEXT, KEY_PROJECTID, KEY_PUBLISHID, KEY_PUBLISHLIST, KEY_REPORT, LISTENERS_FOR_ALL_EVENTS
 
Constructor Summary
CmsLocaleManager()
          Initializes a new CmsLocaleManager, called from the configuration.
CmsLocaleManager(java.util.Locale defaultLocale)
          Initializes a new CmsLocaleManager, used for OpenCms runlevel 1 (unit tests) only.
 
Method Summary
 void addAvailableLocale(java.lang.String localeName)
          Adds a locale to the list of available locales.
 void addDefaultLocale(java.lang.String localeName)
          Adds a locale to the list of default locales.
 void cmsEvent(CmsEvent event)
          Implements the CmsEvent interface, the locale manager the events to clear the list of cached keys .
 java.util.List getAvailableLocales()
          Returns the list of available locales configured in.
 java.util.List getAvailableLocales(CmsObject cms, java.lang.String resourceName)
          Returns an array of available locale names for the given resource.
 java.util.List getAvailableLocales(java.lang.String names)
          Returns a List of available locales from a comma separated string of locale names.
 java.util.Locale getBestMatchingLocale(java.util.Locale requestedLocale, java.util.List defaults, java.util.Collection available)
          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.
static java.util.Locale getDefaultLocale()
          Returns the default locale configured in opencms-system.xml.
 java.util.Locale getDefaultLocale(CmsObject cms, java.lang.String resourceName)
          Returns the "best" default locale for the given resource.
 java.util.List getDefaultLocales()
          Returns the list of default locale names configured in opencms.properties.
 java.util.List getDefaultLocales(CmsObject cms, java.lang.String resourceName)
          Returns an array of default locales for the given resource.
 java.util.Locale getFirstMatchingLocale(java.util.List locales, java.util.Collection available)
          Returns the first matching locale (eventually simplified) from the available locales.
 CmsI18nInfo getI18nInfo(javax.servlet.http.HttpServletRequest req, CmsUser user, CmsProject project, java.lang.String resource)
          Returns the the appropriate locale/encoding for a request, using the "right" locale handler for the given resource.
static java.util.Locale getLocale(java.lang.String localeName)
          Returns a locale created from the given full name.
 I_CmsLocaleHandler getLocaleHandler()
          Returns the configured locale handler.
static java.lang.String getLocaleNames(java.util.List localeNames)
          Returns the locale names from the given List of locales as a comma separated String.
static java.util.List getLocales(java.util.List localeNames)
          Returns a List of locales from an array of locale names.
static java.util.List getLocales(java.lang.String localeNames)
          Returns a List of locales from a comma-separated string of locale names.
static java.lang.String getResourceEncoding(CmsObject cms, CmsResource res)
          Returns the content encoding set for the given resource.
 void initialize(CmsObject cms)
          Initializes this locale manager with the OpenCms system configuration.
 boolean isInitialized()
          Returns true if this locale manager is fully initialized.
 void setLocaleHandler(I_CmsLocaleHandler localeHandler)
          Sets the configured locale handler.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOCALE_HANDLER

public static final java.lang.String LOCALE_HANDLER
Runtime property name for locale handler.

See Also:
Constant Field Values

PARAMETER_ENCODING

public static final java.lang.String PARAMETER_ENCODING
Request parameter to force encoding selection.

See Also:
Constant Field Values

PARAMETER_LOCALE

public static final java.lang.String PARAMETER_LOCALE
Request parameter to force locale selection.

See Also:
Constant Field Values
Constructor Detail

CmsLocaleManager

public CmsLocaleManager()
Initializes a new CmsLocaleManager, called from the configuration.


CmsLocaleManager

public CmsLocaleManager(java.util.Locale defaultLocale)
Initializes a new CmsLocaleManager, used for OpenCms runlevel 1 (unit tests) only.

Parameters:
defaultLocale - the default locale to use
Method Detail

getDefaultLocale

public static java.util.Locale getDefaultLocale()
Returns the default locale configured in opencms-system.xml.

The default locale is the first locale int the list of configured default locales.

Returns:
the default locale

getLocale

public static java.util.Locale getLocale(java.lang.String localeName)
Returns a locale created from the given full name.

The full name must consist of language code, country code(optional), variant(optional) separated by "_".

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.

Parameters:
localeName - the full locale name
Returns:
the locale or null if not available

getLocaleNames

public static java.lang.String getLocaleNames(java.util.List localeNames)
Returns the locale names from the given List of locales as a comma separated String.

For example, if the input List contains Locale.ENGLISH and Locale.GERMANY, the result will be "en, de_DE".

An empty String is returned if the input is null, or contains no elements.

Parameters:
localeNames - the locale names to generate a String from
Returns:
the locale names from the given List of locales as a comma separated String

getLocales

public static java.util.List getLocales(java.util.List localeNames)
Returns a List of locales from an array of locale names.

Parameters:
localeNames - array of locale names
Returns:
a List of locales derived from the given locale names

getLocales

public static java.util.List getLocales(java.lang.String localeNames)
Returns a List of locales from a comma-separated string of locale names.

Parameters:
localeNames - a comma-separated string of locale names
Returns:
a List of locales derived from the given locale names

getResourceEncoding

public static final java.lang.String getResourceEncoding(CmsObject cms,
                                                         CmsResource res)
Returns the content encoding set for the given resource.

The content encoding is controlled by the property CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, which can be set on the resource or on a parent folder for all resources in this folder.

In case no encoding has been set, the default encoding from CmsSystemInfo.getDefaultEncoding() is returned.

Parameters:
cms - the current OpenCms user context
res - the resource to read the encoding for
Returns:
the content encoding set for the given resource

addAvailableLocale

public void addAvailableLocale(java.lang.String localeName)
Adds a locale to the list of available locales.

Parameters:
localeName - the locale to add

addDefaultLocale

public void addDefaultLocale(java.lang.String localeName)
Adds a locale to the list of default locales.

Parameters:
localeName - the locale to add

cmsEvent

public void cmsEvent(CmsEvent event)
Implements the CmsEvent interface, the locale manager the events to clear the list of cached keys .

Specified by:
cmsEvent in interface I_CmsEventListener
Parameters:
event - CmsEvent that has occurred

getAvailableLocales

public java.util.List getAvailableLocales()
Returns the list of available locales configured in.

Returns:
the list of available locale names, e.g. en, de

getAvailableLocales

public java.util.List getAvailableLocales(CmsObject cms,
                                          java.lang.String resourceName)
Returns an array of available locale names for the given resource.

Parameters:
cms - the current cms permission object
resourceName - the name of the resource
Returns:
an array of available locale names

getAvailableLocales

public java.util.List getAvailableLocales(java.lang.String names)
Returns a List of available locales from a comma separated string of locale names.

All names are filtered against the allowed available locales configured in opencms.properties.

Parameters:
names - a comma-separated String of locale names
Returns:
List of locales created from the given locale names

getBestMatchingLocale

public java.util.Locale getBestMatchingLocale(java.util.Locale requestedLocale,
                                              java.util.List defaults,
                                              java.util.Collection available)
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.

Parameters:
requestedLocale - the requested locale, if this (or a simplified version of it) is available it will be returned
defaults - a list of default locales to use in case the requested locale is not available
available - the available locales to find a match in
Returns:
the best matching locale name or null if no name matches

getDefaultLocale

public java.util.Locale getDefaultLocale(CmsObject cms,
                                         java.lang.String resourceName)
Returns the "best" default locale for the given resource.

Parameters:
cms - the current cms permission object
resourceName - the name of the resource
Returns:
an array of default locale names
See Also:
getDefaultLocales(CmsObject, String)

getDefaultLocales

public java.util.List getDefaultLocales()
Returns the list of default locale names configured in opencms.properties.

Returns:
the list of default locale names, e.g. en, de

getDefaultLocales

public java.util.List getDefaultLocales(CmsObject cms,
                                        java.lang.String resourceName)
Returns an array of default locales for the given resource.

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 getDefaultLocale(CmsObject, String).

Parameters:
cms - the current cms permission object
resourceName - the name of the resource
Returns:
an array of default locale names
See Also:
getDefaultLocale(CmsObject, String)

getFirstMatchingLocale

public java.util.Locale getFirstMatchingLocale(java.util.List locales,
                                               java.util.Collection available)
Returns the first matching locale (eventually simplified) from the available locales.

Parameters:
locales - must be an ascending sorted list of locales in order of preference
available - the available locales to find a match in
Returns:
the first precise or simplified match

getI18nInfo

public CmsI18nInfo getI18nInfo(javax.servlet.http.HttpServletRequest req,
                               CmsUser user,
                               CmsProject project,
                               java.lang.String resource)
Returns the the appropriate locale/encoding for a request, using the "right" locale handler for the given resource.

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.

Parameters:
req - the current http request
user - the current user
project - the current project
resource - the URI of the requested resource (with full site root added)
Returns:
the i18n information to use for the given request context

getLocaleHandler

public I_CmsLocaleHandler getLocaleHandler()
Returns the configured locale handler.

This handler is used to derive the appropriate locale/encoding for a request.

Returns:
the locale handler

initialize

public void initialize(CmsObject cms)
Initializes this locale manager with the OpenCms system configuration.

Parameters:
cms - an OpenCms context object that must have been initialized with "Admin" permissions

isInitialized

public boolean isInitialized()
Returns true if this locale manager is fully initialized.

This is required to prevent errors during unit tests, simple unit tests will usually not have a fully initialized locale manager available.

Returns:
true if the locale manager is fully initialized

setLocaleHandler

public void setLocaleHandler(I_CmsLocaleHandler localeHandler)
Sets the configured locale handler.

Parameters:
localeHandler - the locale handler to set