|
||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.opencms.i18n.CmsLocaleManager
public class CmsLocaleManager
Manages the locales configured for this OpenCms installation.
Locale configuration is done in the configuration file opencms-system.xml
in the opencms/system/internationalization
node and it's sub-nodes.
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. |
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<java.util.Locale> |
getAvailableLocales()
Returns the list of available Locale s configured in opencms-system.xml ,
in the opencms/system/internationalization/localesconfigured node. |
java.util.List<java.util.Locale> |
getAvailableLocales(CmsObject cms,
java.lang.String resourceName)
Returns an array of available locale names for the given resource. |
java.util.List<java.util.Locale> |
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<java.util.Locale> defaults,
java.util.Collection<java.util.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. |
static java.util.Locale |
getDefaultLocale()
Returns the default locale configured in opencms-system.xml ,
that is the first locale from the list provided
in the opencms/system/internationalization/localesdefault node. |
java.util.Locale |
getDefaultLocale(CmsObject cms,
java.lang.String resourceName)
Returns the "the" default locale for the given resource. |
java.util.List<java.util.Locale> |
getDefaultLocales()
Returns the list of default Locale s configured in opencms-system.xml ,
in the opencms/system/internationalization/localesdefault node. |
java.util.List<java.util.Locale> |
getDefaultLocales(CmsObject cms,
CmsResource resource)
Returns an array of default locales for the given resource. |
java.util.List<java.util.Locale> |
getDefaultLocales(CmsObject cms,
java.lang.String resourceName)
Returns an array of default locales for the given resource. |
java.util.Locale |
getFirstMatchingLocale(java.util.List<java.util.Locale> locales,
java.util.Collection<java.util.Locale> 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<java.util.Locale> locales)
Returns the locale names from the given List of locales as a comma separated String. |
static java.util.List<java.util.Locale> |
getLocales(java.util.List<java.lang.String> localeNames)
Returns a List of locales from an array of locale names. |
static java.util.List<java.util.Locale> |
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 |
---|
public static final java.lang.String LOCALE_HANDLER
public static final java.lang.String PARAMETER_ENCODING
public static final java.lang.String PARAMETER_LOCALE
Constructor Detail |
---|
public CmsLocaleManager()
public CmsLocaleManager(java.util.Locale defaultLocale)
defaultLocale
- the default locale to useMethod Detail |
---|
public static java.util.Locale getDefaultLocale()
opencms-system.xml
,
that is the first locale from the list provided
in the opencms/system/internationalization/localesdefault
node.
opencms-system.xml
public static java.util.Locale getLocale(java.lang.String localeName)
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.
localeName
- the full locale name
null
if not availablepublic static java.lang.String getLocaleNames(java.util.List<java.util.Locale> locales)
For example, if the input List contains
and
Locale.ENGLISH
, the result will be Locale.GERMANY
"en, de_DE"
.
An empty String is returned if the input is null
, or contains no elements.
locales
- the locales to generate a String from
public static java.util.List<java.util.Locale> getLocales(java.util.List<java.lang.String> localeNames)
localeNames
- array of locale names
public static java.util.List<java.util.Locale> getLocales(java.lang.String localeNames)
localeNames
- a comma-separated string of locale names
public static final java.lang.String getResourceEncoding(CmsObject cms, CmsResource res)
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.
cms
- the current OpenCms user contextres
- the resource to read the encoding for
public void addAvailableLocale(java.lang.String localeName)
localeName
- the locale to addpublic void addDefaultLocale(java.lang.String localeName)
localeName
- the locale to addpublic void cmsEvent(CmsEvent event)
cmsEvent
in interface I_CmsEventListener
event
- CmsEvent that has occurredpublic java.util.List<java.util.Locale> getAvailableLocales()
Locale
s configured in opencms-system.xml
,
in the opencms/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
en, de
getDefaultLocales()
public java.util.List<java.util.Locale> getAvailableLocales(CmsObject cms, java.lang.String resourceName)
cms
- the current cms permission objectresourceName
- the name of the resource
getAvailableLocales()
public java.util.List<java.util.Locale> getAvailableLocales(java.lang.String names)
All names are filtered against the allowed available locales
configured in opencms-system.xml
.
names
- a comma-separated String of locale names
getAvailableLocales()
public java.util.Locale getBestMatchingLocale(java.util.Locale requestedLocale, java.util.List<java.util.Locale> defaults, java.util.Collection<java.util.Locale> available)
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
public java.util.Locale getDefaultLocale(CmsObject cms, java.lang.String resourceName)
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 as getDefaultLocale()
is returned.
In case the property
has not been set
on the resource or a parent folder,
this method returns the same result as CmsPropertyDefinition.PROPERTY_LOCALE
getDefaultLocale()
.
cms
- the current cms permission objectresourceName
- the name of the resource
getDefaultLocales()
,
getDefaultLocales(CmsObject, String)
public java.util.List<java.util.Locale> getDefaultLocales()
Locale
s configured in opencms-system.xml
,
in the opencms/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.
en, de
getAvailableLocales()
public java.util.List<java.util.Locale> getDefaultLocales(CmsObject cms, CmsResource 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 as CmsPropertyDefinition.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)
cms
- the current cms permission objectresource
- the resource to read the default locale properties for
getDefaultLocales()
,
getDefaultLocale(CmsObject, String)
,
getDefaultLocales(CmsObject, String)
public java.util.List<java.util.Locale> getDefaultLocales(CmsObject cms, java.lang.String resourceName)
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 as CmsPropertyDefinition.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)
cms
- the current cms permission objectresourceName
- the name of the resource
getDefaultLocales()
,
getDefaultLocale(CmsObject, String)
,
getDefaultLocales(CmsObject, CmsResource)
public java.util.Locale getFirstMatchingLocale(java.util.List<java.util.Locale> locales, java.util.Collection<java.util.Locale> available)
In case no match is found, code null
is returned.
locales
- must be an ascending sorted list of locales in order of preferenceavailable
- the available locales to find a match in
null
in case no match is foundpublic CmsI18nInfo getI18nInfo(javax.servlet.http.HttpServletRequest req, CmsUser user, CmsProject project, java.lang.String 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.
req
- the current http requestuser
- the current userproject
- the current projectresource
- the URI of the requested resource (with full site root added)
public I_CmsLocaleHandler getLocaleHandler()
This handler is used to derive the appropriate locale/encoding for a request.
public void initialize(CmsObject cms)
cms
- an OpenCms context object that must have been initialized with "Admin" permissionspublic boolean isInitialized()
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.
public void setLocaleHandler(I_CmsLocaleHandler localeHandler)
localeHandler
- the locale handler to set
|
||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |