Class CmsLocaleManager
- All Implemented Interfaces:
I_CmsEventListener
Locale configuration is done in the configuration file opencms-system.xml
in the opencms/system/internationalization
node and it's sub-nodes.
- Since:
- 6.0.0
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Runtime property name for locale handler.static final Locale
Locale to use for storing locale-independent XML contents.static final String
Request parameter to force encoding selection.static final String
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_GROUP_MODIFIED, EVENT_LOGIN_USER, EVENT_OU_MODIFIED, EVENT_PROJECT_MODIFIED, EVENT_PROPERTY_DEFINITION_CREATED, EVENT_PROPERTY_DEFINITION_MODIFIED, EVENT_PROPERTY_MODIFIED, EVENT_PUBLISH_PROJECT, EVENT_REBUILD_SEARCHINDEXES, EVENT_REINDEX_OFFLINE, EVENT_REINDEX_ONLINE, EVENT_RESOURCE_AND_PROPERTIES_MODIFIED, EVENT_RESOURCE_COPIED, EVENT_RESOURCE_CREATED, EVENT_RESOURCE_DELETED, EVENT_RESOURCE_MODIFIED, EVENT_RESOURCE_MOVED, EVENT_RESOURCES_AND_PROPERTIES_MODIFIED, EVENT_RESOURCES_MODIFIED, EVENT_SITEMAP_CHANGED, EVENT_UPDATE_EXPORTS, EVENT_USER_MODIFIED, KEY_CHANGE, KEY_DBCONTEXT, KEY_GROUP_ID, KEY_GROUP_NAME, KEY_INDEX_NAMES, KEY_IS_ONLINE, KEY_OU_ID, KEY_OU_NAME, KEY_PROJECTID, KEY_PUBLISHID, KEY_PUBLISHLIST, KEY_REINDEX_RELATED, KEY_REPORT, KEY_RESOURCE, KEY_RESOURCES, KEY_SKIPINDEX, KEY_USER_ACTION, KEY_USER_CHANGES, KEY_USER_ID, KEY_USER_NAME, LISTENERS_FOR_ALL_EVENTS, VALUE_CREATE_SIBLING, VALUE_GROUP_MODIFIED_ACTION_CREATE, VALUE_GROUP_MODIFIED_ACTION_DELETE, VALUE_GROUP_MODIFIED_ACTION_WRITE, VALUE_OU_MODIFIED_ACTION_CREATE, VALUE_OU_MODIFIED_ACTION_DELETE, VALUE_USER_MODIFIED_ACTION_ADD_USER_TO_GROUP, VALUE_USER_MODIFIED_ACTION_CREATE_USER, VALUE_USER_MODIFIED_ACTION_DELETE_USER, VALUE_USER_MODIFIED_ACTION_REMOVE_USER_FROM_GROUP, VALUE_USER_MODIFIED_ACTION_RESET_PASSWORD, VALUE_USER_MODIFIED_ACTION_SET_OU, VALUE_USER_MODIFIED_ACTION_WRITE_USER
-
Constructor Summary
ConstructorDescriptionInitializes a new CmsLocaleManager, called from the configuration.CmsLocaleManager
(Locale defaultLocale) Initializes a new CmsLocaleManager, used for OpenCms runlevel 1 (unit tests) only. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAvailableLocale
(String localeName) Adds a locale to the list of available locales.void
addDefaultLocale
(String localeName) Adds a locale to the list of default locales.void
Implements the CmsEvent interface, the locale manager the events to clear the list of cached keys .Returns the list of availableLocale
s configured inopencms-system.xml
, in theopencms/system/internationalization/localesconfigured
node.getAvailableLocales
(String names) Returns a List of available locales from a comma separated string of locale names.getAvailableLocales
(CmsObject cms, String resourceName) Returns an array of available locale names for the given resource.getAvailableLocales
(CmsObject cms, CmsResource resource) Returns an array of available locale names for the given resource.getBestAvailableLocaleForXmlContent
(CmsObject cms, CmsResource resource, I_CmsXmlDocument content) Returns the best available locale present in the given XML content, or the default locale.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 Locale
Returns the default locale configured inopencms-system.xml
, that is the first locale from the list provided in theopencms/system/internationalization/localesdefault
node.getDefaultLocale
(CmsObject cms, String resourceName) Returns the "the" default locale for the given resource.getDefaultLocale
(CmsObject cms, CmsResource resource) Returns the "the" default locale for the given resource.Returns the list of defaultLocale
s configured inopencms-system.xml
, in theopencms/system/internationalization/localesdefault
node.getDefaultLocales
(CmsObject cms, String resourceName) Returns an array of default locales for the given resource.getDefaultLocales
(CmsObject cms, CmsResource resource) Returns an array of default locales for the given resource.getFirstMatchingLocale
(List<Locale> locales, List<Locale> available) Returns the first matching locale (eventually simplified) from the available locales.getI18nInfo
(javax.servlet.http.HttpServletRequest req, CmsUser user, CmsProject project, String resource) Returns the the appropriate locale/encoding for a request, using the "right" locale handler for the given resource.static Locale
Returns a locale created from the given full name.Returns the configured locale handler.static String
getLocaleNames
(List<Locale> locales) Returns the locale names from the given List of locales as a comma separated String.getLocales
(String localeNames) Returns a List of locales from a comma-separated string of locale names.getLocales
(List<String> localeNames) Returns a List of locales from an array of locale names.getLocaleVariants
(String basename, Locale locale, boolean wantBase, boolean defaultAsBase) Extends a base name with locale suffixes and yields the list of extended names in the order they typically should be used according to the given locale.static Locale
getMainLocale
(CmsObject cms, CmsResource res) Utility method to get the primary locale for a given resource.static final String
getResourceEncoding
(CmsObject cms, CmsResource res) Returns the content encoding set for the given resource.Gets the string value of the 'reuse-elements' option.Returns the OpenCms default the time zone.void
initialize
(CmsObject cms) Initializes this locale manager with the OpenCms system configuration.boolean
Returnstrue
if this locale manager is fully initialized.void
setLocaleHandler
(I_CmsLocaleHandler localeHandler) Sets the configured locale handler.void
setReuseElements
(String reuseElements) Sets the 'reuse-elemnts option value.void
setTimeZone
(String timeZoneName) Sets OpenCms default the time zone.boolean
Returns true if the 'copy page' dialog should reuse elements in auto mode when copying to a different locale.
-
Field Details
-
LOCALE_HANDLER
Runtime property name for locale handler.- See Also:
-
MASTER_LOCALE
Locale to use for storing locale-independent XML contents. -
PARAMETER_ENCODING
Request parameter to force encoding selection.- See Also:
-
PARAMETER_LOCALE
Request parameter to force locale selection.- See Also:
-
-
Constructor Details
-
CmsLocaleManager
public CmsLocaleManager()Initializes a new CmsLocaleManager, called from the configuration. -
CmsLocaleManager
Initializes a new CmsLocaleManager, used for OpenCms runlevel 1 (unit tests) only.- Parameters:
defaultLocale
- the default locale to use
-
-
Method Details
-
getDefaultLocale
Returns the default locale configured inopencms-system.xml
, that is the first locale from the list provided in theopencms/system/internationalization/localesdefault
node.- Returns:
- the default locale configured in
opencms-system.xml
-
getLocale
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
Returns the locale names from the given List of locales as a comma separated String.For example, if the input List contains
andLocale.ENGLISH
, the result will beLocale.GERMANY
"en, de_DE"
.An empty String is returned if the input is
null
, or contains no elements.- Parameters:
locales
- the locales to generate a String from- Returns:
- the locale names from the given List of locales as a comma separated String
-
getLocales
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
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
-
getLocaleVariants
public static List<String> getLocaleVariants(String basename, Locale locale, boolean wantBase, boolean defaultAsBase) Extends a base name with locale suffixes and yields the list of extended names in the order they typically should be used according to the given locale.
Example: If you have base name
base
and the locale withString
representationde_DE
, the result will be (assumingen
is the default locale):- for
wantBase == false
anddefaultAsBase == false
:[base_de_DE, base_de]
- for
wantBase == true
anddefaultAsBase == false
:[base_de_DE, base_de, base]
- for
wantBase == false
anddefaultAsBase == true
:[base_de_DE, base_de, base_en]
- for
wantBase == true
anddefaultAsBase == true
:[base_de_DE, base_de, base, base_en]
- Parameters:
basename
- the base name that should be extended by locale post-fixeslocale
- the locale for which the list of extensions should be generated.wantBase
- flag, indicating if the base name without locale post-fix should be yielded as well.defaultAsBase
- flag, indicating, if the variant with the default locale should be used as base.- Returns:
- the list of locale variants of the base name in the order they should be used.
- for
-
getMainLocale
Utility method to get the primary locale for a given resource.- Parameters:
cms
- the current CMS contextres
- the resource for which the locale should be retrieved- Returns:
- the primary locale
-
getResourceEncoding
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 contextres
- the resource to read the encoding for- Returns:
- the content encoding set for the given resource
-
addAvailableLocale
Adds a locale to the list of available locales.- Parameters:
localeName
- the locale to add
-
addDefaultLocale
Adds a locale to the list of default locales.- Parameters:
localeName
- the locale to add
-
cmsEvent
Implements the CmsEvent interface, the locale manager the events to clear the list of cached keys .- Specified by:
cmsEvent
in interfaceI_CmsEventListener
- Parameters:
event
- CmsEvent that has occurred
-
getAvailableLocales
Returns the list of availableLocale
s configured inopencms-system.xml
, in theopencms/system/internationalization/localesconfigured
node.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.
The available locales are a superset of the default locales, see
getDefaultLocales()
.It's possible to reduce the system default by setting the propery
to a comma separated list of locale names. However, you can not add new available locales, only remove from the configured list.CmsPropertyDefinition.PROPERTY_AVAILABLE_LOCALES
Note that if the
localesconfigured
node contains a locale variant for a specific country (e.g. de_DE), then both that locale and the locale without the country suffix will be in the returned list.
- Returns:
- the list of available locale names, e.g.
en, de
- See Also:
-
getAvailableLocales
Returns an array of available locale names for the given resource.- Parameters:
cms
- the current cms permission objectresource
- the resource- Returns:
- an array of available locale names
- See Also:
-
getAvailableLocales
Returns an array of available locale names for the given resource.- Parameters:
cms
- the current cms permission objectresourceName
- the name of the resource- Returns:
- an array of available locale names
- See Also:
-
getAvailableLocales
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-system.xml
.- Parameters:
names
- a comma-separated String of locale names- Returns:
- List of locales created from the given locale names
- See Also:
-
getBestAvailableLocaleForXmlContent
public Locale getBestAvailableLocaleForXmlContent(CmsObject cms, CmsResource resource, I_CmsXmlDocument content) Returns the best available locale present in the given XML content, or the default locale.- Parameters:
cms
- the current OpenCms user contextresource
- the resourcecontent
- the XML content- Returns:
- the locale
-
getBestMatchingLocale
public Locale getBestMatchingLocale(Locale requestedLocale, List<Locale> defaults, List<Locale> 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 returneddefaults
- a list of default locales to use in case the requested locale is not availableavailable
- the available locales to find a match in- Returns:
- the best matching locale name or null if no name matches
-
getDefaultLocale
Returns the "the" default locale for the given resource.It's possible to override the system default (see
getDefaultLocale()
) by setting the property
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.CmsPropertyDefinition.PROPERTY_LOCALE
The default locale must be contained in the set of configured available locales, see
getAvailableLocales()
. In case an invalid locale has been set with the property, this locale is ignored and the same result asgetDefaultLocale()
is returned.In case the property
has not been set on the resource or a parent folder, this method returns the same result asCmsPropertyDefinition.PROPERTY_LOCALE
getDefaultLocale()
.- Parameters:
cms
- the current cms permission objectresource
- the resource- Returns:
- an array of default locale names
- See Also:
-
getDefaultLocale
Returns the "the" default locale for the given resource.It's possible to override the system default (see
getDefaultLocale()
) by setting the property
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.CmsPropertyDefinition.PROPERTY_LOCALE
The default locale must be contained in the set of configured available locales, see
getAvailableLocales()
. In case an invalid locale has been set with the property, this locale is ignored and the same result asgetDefaultLocale()
is returned.In case the property
has not been set on the resource or a parent folder, this method returns the same result asCmsPropertyDefinition.PROPERTY_LOCALE
getDefaultLocale()
.- Parameters:
cms
- the current cms permission objectresourceName
- the name of the resource- Returns:
- an array of default locale names
- See Also:
-
getDefaultLocales
Returns the list of defaultLocale
s configured inopencms-system.xml
, in theopencms/system/internationalization/localesdefault
node.Since the default locale is always available, the result list will always contain at least one Locale.
It's possible to override the system default by setting the property
to a comma separated list of locale names. This property is inherited from the parent folders.CmsPropertyDefinition.PROPERTY_LOCALE
The default locales must be a subset of the configured available locales, see
getAvailableLocales()
. In case an invalid locale has been set with the property, this locale is ignored.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.
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: 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.
- Returns:
- the list of default locale names, e.g.
en, de
- See Also:
-
getDefaultLocales
Returns an array of default locales for the given resource.Since the default locale is always available, the result list will always contain at least one Locale.
It's possible to override the system default (see
getDefaultLocales()
) by setting the property
to a comma separated list of locale names. This property is inherited from the parent folders.CmsPropertyDefinition.PROPERTY_LOCALE
The default locales must be a subset of the configured available locales, see
getAvailableLocales()
. In case an invalid locale has been set with the property, this locale is ignored.In case the property
has not been set on the resource or a parent folder, this method returns the same result asCmsPropertyDefinition.PROPERTY_LOCALE
getDefaultLocales()
.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
.getDefaultLocale(CmsObject, String)
- Parameters:
cms
- the current cms permission objectresource
- the resource to read the default locale properties for- Returns:
- an array of default locale names
- Since:
- 7.0.2
- See Also:
-
getDefaultLocales
Returns an array of default locales for the given resource.Since the default locale is always available, the result list will always contain at least one Locale.
It's possible to override the system default (see
getDefaultLocales()
) by setting the property
to a comma separated list of locale names. This property is inherited from the parent folders.CmsPropertyDefinition.PROPERTY_LOCALE
The default locales must be a subset of the configured available locales, see
getAvailableLocales()
. In case an invalid locale has been set with the property, this locale is ignored.In case the property
has not been set on the resource or a parent folder, this method returns the same result asCmsPropertyDefinition.PROPERTY_LOCALE
getDefaultLocales()
.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
.getDefaultLocale(CmsObject, String)
- Parameters:
cms
- the current cms permission objectresourceName
- the name of the resource- Returns:
- an array of default locale names
- See Also:
-
getFirstMatchingLocale
Returns the first matching locale (eventually simplified) from the available locales.In case no match is found, code
null
is returned.- Parameters:
locales
- must be an ascending sorted list of locales in order of preferenceavailable
- the available locales to find a match in- Returns:
- the first precise or simplified match, or
null
in case no match is found
-
getI18nInfo
public CmsI18nInfo getI18nInfo(javax.servlet.http.HttpServletRequest req, CmsUser user, CmsProject project, 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 requestuser
- the current userproject
- the current projectresource
- the URI of the requested resource (with full site root added)- Returns:
- the i18n information to use for the given request context
-
getLocaleHandler
Returns the configured locale handler.This handler is used to derive the appropriate locale/encoding for a request.
- Returns:
- the locale handler
-
getReuseElementsStr
Gets the string value of the 'reuse-elements' option.- Returns:
- the string value of the 'reuse-elements' option
-
getTimeZone
Returns the OpenCms default the time zone.- Returns:
- the OpenCms default the time zone
-
initialize
Initializes this locale manager with the OpenCms system configuration.- Parameters:
cms
- an OpenCms context object that must have been initialized with "Admin" permissions
-
isInitialized
Returnstrue
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
Sets the configured locale handler.- Parameters:
localeHandler
- the locale handler to set
-
setReuseElements
Sets the 'reuse-elemnts option value.- Parameters:
reuseElements
- the option value
-
setTimeZone
Sets OpenCms default the time zone.If the name can not be resolved as time zone ID, then "GMT" is used.
- Parameters:
timeZoneName
- the name of the time zone to set, for example "GMT"
-
shouldReuseElements
Returns true if the 'copy page' dialog should reuse elements in auto mode when copying to a different locale.- Returns:
- true if auto mode of the 'copy page' dialog should reuse elements
-