org.apache.cocoon.acting
public class: LocaleAction [javadoc |
source]
java.lang.Object
org.apache.avalon.framework.logger.AbstractLogEnabled
org.apache.cocoon.acting.AbstractAction
org.apache.cocoon.acting.ServiceableAction
org.apache.cocoon.acting.LocaleAction
All Implemented Interfaces:
org.apache.avalon.framework.thread.ThreadSafe, org.apache.avalon.framework.configuration.Configurable, org.apache.avalon.framework.service.Serviceable, Action
An action that locates and provides to the pipeline locale information
looked up in a range of ways.
Configuration
A sample configuration (given in the <map:matchers> section of the
sitemap) is given below. This configuration shows default values.
<map:action name="locale" src="org.apache.cocoon.acting.LocaleAction">
<locale-attribute>locale</locale-attribute>
<use-locale>true</use-locale>
<default-locale language="en" country="US"/>
<store-in-request>false<store-in-request>
<create-session>false<create-session>
<store-in-session>false<store-in-session>
<store-in-cookie>false<store-in-cookie>
</map:action>
Above configuration parameters mean:
- locale-attribute specifies the name of the request
parameter / session attribute / cookie that is to be used as a locale
(defaults to
locale)
- use-locale specifies whether the primary locale provided
by the user agent (or server default, is no locale passed by the agent)
is to be used
- default-locale specifies the default locale to be used when
none found.
- store-in-request specifies whether found locale should be
stored as request attribute.
- create-session specifies whether session should be created
when storing found locale as session attribute.
- store-in-session specifies whether found locale should be
stored as session attribute.
- store-in-cookie specifies whether found locale should be
stored as cookie.
Usage
This action will be used in a pipeline like so:
<map:act type="locale">
<map:generate src="file_{language}_{country}_{variant}.xml"/>
...
</map:match>
or
<map:act type="locale">
<map:generate src="file_{locale}.xml"/>
...
</map:match>
Locale Identification
Locales will be tested in following order:
- Locale provided as a request parameter
- Locale provided as a session attribute
- Locale provided as a cookie
- Locale provided using a sitemap parameter
(<map:parameter name="locale" value="{1}"/> style parameter within
the <map:match> node)
- Locale provided by the user agent, or server default,
if
use-locale is set to true
- The default locale, if specified in the matcher's configuration
First found locale will be returned.
Sitemap Variables
Once locale has been found, the following sitemap variables
will be available to sitemap elements contained within the action:
- {locale}: The locale string
- {language}: The language of the found locale
- {country}: The country of the found locale
- {variant}: The variant of the found locale
- author:
< - a href="mailto:Marcus.Crafter@osa.de">Marcus Crafter
- author:
< - a href="mailto:kpiroumian@flagship.ru">Konstantin Piroumian
- author:
< - a href="mailto:lassi.immonen@valkeus.com">Lassi Immonen
- author:
< - a href="mailto:vgritsenko@apache.org">Vadim Gritsenko
- version:
CVS - $Id: LocaleAction.java 433543 2006-08-22 06:22:54Z crossley $
| Field Summary |
|---|
| public static final String | LOCALE | Default locale attribute name. |
| public static final String | LOCALE_ATTR | Configuration element name for locale attribute name. |
| public static final String | STORE_REQUEST | Constant representing the request storage configuration attribute |
| public static final String | CREATE_SESSION | Constant representing the session creation configuration attribute |
| public static final String | STORE_SESSION | Constant representing the session storage configuration attribute |
| public static final String | STORE_COOKIE | Constant representing the cookie storage configuration attribute |
| Methods from org.apache.cocoon.acting.ServiceableAction: |
|---|
|
service |
| Method from org.apache.cocoon.acting.LocaleAction Detail: |
public Map act(Redirector redirector,
SourceResolver resolver,
Map objectModel,
String source,
Parameters params) throws Exception {
// Obtain locale information from request, session, cookies, or params
Locale locale = I18nUtils.findLocale(objectModel,
localeAttribute,
params,
defaultLocale,
useLocale);
if (locale == null) {
if (getLogger().isDebugEnabled()) {
getLogger().debug("No locale found.");
}
return null;
}
String localeStr = locale.toString();
if (getLogger().isDebugEnabled()) {
getLogger().debug("Found locale: " + localeStr);
}
I18nUtils.storeLocale(objectModel,
localeAttribute,
localeStr,
storeInRequest,
storeInSession,
storeInCookie,
createSession);
// Set up a map for sitemap parameters
Map map = new HashMap();
map.put("language", locale.getLanguage());
map.put("country", locale.getCountry());
map.put("variant", locale.getVariant());
map.put("locale", localeStr);
return map;
}
Action which obtains the current environments locale information, and
places it in the objectModel (and optionally in a session/cookie). |
public void configure(Configuration config) throws ConfigurationException {
this.storeInRequest = config.getChild(STORE_REQUEST).getValueAsBoolean(false);
this.createSession = config.getChild(CREATE_SESSION).getValueAsBoolean(false);
this.storeInSession = config.getChild(STORE_SESSION).getValueAsBoolean(false);
this.storeInCookie = config.getChild(STORE_COOKIE).getValueAsBoolean(false);
if (getLogger().isDebugEnabled()) {
getLogger().debug((this.storeInRequest ? "will" : "won't") + " set values in request");
getLogger().debug((this.createSession ? "will" : "won't") + " create session");
getLogger().debug((this.storeInSession ? "will" : "won't") + " set values in session");
getLogger().debug((this.storeInCookie ? "will" : "won't") + " set values in cookies");
}
this.localeAttribute = config.getChild(LOCALE_ATTR).getValue(LOCALE);
this.useLocale = config.getChild("use-locale").getValueAsBoolean(true);
Configuration child = config.getChild("default-locale", false);
if (child != null) {
this.defaultLocale = new Locale(child.getAttribute("language", DEFAULT_DEFAULT_LANG),
child.getAttribute("country", DEFAULT_DEFAULT_COUNTRY),
child.getAttribute("variant", DEFAULT_DEFAULT_VARIANT));
}
if (getLogger().isDebugEnabled()) {
getLogger().debug("Locale attribute name is " + this.localeAttribute);
getLogger().debug((this.useLocale ? "will" : "won't") + " use request locale");
getLogger().debug("default locale " + this.defaultLocale);
}
}
|
public static String getLocaleAttribute(Map objectModel,
String localeAttrName) {
Locale locale = I18nUtils.findLocale(objectModel,
localeAttrName,
null,
null,
true);
return locale.toString();
} Deprecated! See - I18nUtils.findLocale
Helper method to retreive the attribute value containing locale
information. See class documentation for locale determination algorythm. |