org.apache.cocoon.transformation
Class I18nTransformer

java.lang.Object
  extended by org.apache.avalon.framework.logger.AbstractLogEnabled
      extended by org.apache.cocoon.xml.AbstractXMLProducer
          extended by org.apache.cocoon.xml.AbstractXMLPipe
              extended by org.apache.cocoon.transformation.AbstractTransformer
                  extended by org.apache.cocoon.transformation.I18nTransformer
All Implemented Interfaces:
Poolable, Recyclable, Disposable, Component, Configurable, LogEnabled, Serviceable, CacheableProcessingComponent, SitemapModelComponent, Transformer, XMLPipe, XMLProducer, XMLConsumer, ContentHandler, LexicalHandler
Direct Known Subclasses:
PortalToolsI18nTransformer

public class I18nTransformer
extends AbstractTransformer
implements CacheableProcessingComponent, Serviceable, Configurable, Disposable

Version:
$Id: I18nTransformer.html 1304258 2012-03-23 10:09:27Z ilgrosso $
Author:
Konstantin Piroumian, Matthieu Sozeau, Marcus Crafter, Michael Enke

Nested Class Summary
 class I18nTransformer.CatalogueInfo
          Holds information about one catalogue.
 
Field Summary
static String CURRENCY_LOCALE_ATTRIBUTE
          This attribute is used to specify a different locale for the currency.
protected  BundleFactory factory
          Message bundle loader factory component (service)
static String I18N_ATTR_ATTRIBUTE
          This attribute is used with any element (even not i18n) to translate attribute values.
static String I18N_CATALOGUE_ATTRIBUTE
          This attribute can be used on i18n:text to indicate the catalogue from which the key should be retrieved.
static String I18N_CHOOSE_ELEMENT
          i18n:choose element is used to translate elements in-place.
static String I18N_CURRENCY_ELEMENT
          Currency element name
static String I18N_CURRENCY_NO_UNIT_ELEMENT
          Currency without unit element name
static String I18N_DATE_ELEMENT
          i18n:date is used to provide a localized date string.
static String I18N_DATE_TIME_ELEMENT
          i18n:date-time is used to provide a localized date and time string.
static String I18N_DEFAULT_CATALOGUE_ID
          This configuration parameter specifies the id of the catalogue to be used as default catalogue, allowing to redefine the default catalogue on the pipeline level.
static String I18N_EXPR_ATTRIBUTE
          This attribute is used with any element (even not i18n) to evaluate attribute values.
static String I18N_FRACTION_DIGITS_ATTRIBUTE
          fraction-digits attribute is used with i18:number to indicate the number of digits behind the fraction
static String I18N_IF_ELEMENT
          i18n:if is used to test a locale.
static String I18N_INT_CURRENCY_ELEMENT
          Integer currency element name
static String I18N_INT_CURRENCY_NO_UNIT_ELEMENT
          Integer currency without unit element name
static String I18N_KEY_ATTRIBUTE
          This attribute is used with i18n:text element to indicate the key of the according message.
static String I18N_LOCALE
          This configuration parameter specifies the default locale to be used.
static String I18N_LOCALE_ATTRIBUTE
          This attribute is used with date and number formatting elements to indicate the locale that should be used to format the element value.
static String I18N_NAMESPACE_URI
          The namespace for i18n is "http://apache.org/cocoon/i18n/2.1".
static String I18N_NUMBER_ELEMENT
          i18n:number is used to provide a localized number string.
static String I18N_OLD_NAMESPACE_URI
          The old namespace for i18n is "http://apache.org/cocoon/i18n/2.0".
static String I18N_OTHERWISE_ELEMENT
          i18n:otherwise is used to match any locale when no matching locale has been found inside an i18n:choose block.
static String I18N_PARAM_ELEMENT
          i18n:param is used with i18n:translate to provide substitution params.
static String I18N_PARAM_NAME_ATTRIBUTE
          This attribute affects a name to the param that could be used for substitution.
static String I18N_PATTERN_ATTRIBUTE
          This attribute is used with date and number formatting elements to indicate the pattern that should be used to format the element value.
static String I18N_PERCENT_ELEMENT
          Percent element name
static String I18N_PRELOAD
          This configuration parameter specifies locale for which catalogues should be preloaded.
static String I18N_SRC_LOCALE_ATTRIBUTE
          This attribute is used with date and number formatting elements to indicate the locale that should be used to parse the element value.
static String I18N_SRC_PATTERN_ATTRIBUTE
          This attribute is used with date and number formatting elements to indicate the pattern that should be used to parse the element value.
static String I18N_TEXT_ELEMENT
          i18n:text element is used to translate any text, with or without markup.
static String I18N_TIME_ELEMENT
          i18n:time is used to provide a localized time string.
static String I18N_TRANSLATE_ELEMENT
          i18n:translate element is used to translate text with parameter substitution.
static String I18N_TYPE_ATTRIBUTE
          Deprecated. since 2.1. Use nested tags instead, e.g.: <i18n:param><i18n:date/></i18n:param>
static String I18N_UNTRANSLATED
          This configuration parameter specifies the message that should be displayed in case of a not translated text (message not found).
static String I18N_VALUE_ATTRIBUTE
          This attribute is used with date and number formatting elements to indicate the value that should be parsed and formatted.
static String I18N_WHEN_ELEMENT
          i18n:when is used to test a locale.
protected  Locale locale
          Locale
protected  ServiceManager manager
          Component (service) manager
protected  Map objectModel
           
 
Fields inherited from class org.apache.cocoon.xml.AbstractXMLProducer
contentHandler, EMPTY_CONTENT_HANDLER, lexicalHandler, xmlConsumer
 
Fields inherited from interface org.apache.cocoon.transformation.Transformer
ROLE
 
Constructor Summary
I18nTransformer()
           
 
Method Summary
 void characters(char[] ch, int start, int len)
          Receive notification of character data.
 void configure(Configuration conf)
          Implementation of Configurable interface.
 void dispose()
           
 void endElement(String uri, String name, String raw)
          Receive notification of the end of an element.
 Serializable getKey()
          Implemenation of CacheableProcessingComponents.
 Locale getLocale()
          Returns the current locale setting of this transformer instance.
protected  ParamSaxBuffer getMessage(String catalogueID, String key)
          Helper method to retrieve a message from the dictionary.
 SourceValidity getValidity()
          Implementation of CacheableProcessingComponent.
 void recycle()
          Recycle the producer by removing references, and resetting handlers to null (empty) implementations.
 void service(ServiceManager manager)
          Look up the BundleFactory to be used.
 void setup(SourceResolver resolver, Map objectModel, String source, Parameters parameters)
          Setup current instance of transformer.
 void startElement(String uri, String name, String raw, Attributes attr)
          Receive notification of the beginning of an element.
 
Methods inherited from class org.apache.cocoon.xml.AbstractXMLPipe
comment, endCDATA, endDocument, endDTD, endEntity, endPrefixMapping, ignorableWhitespace, processingInstruction, setDocumentLocator, skippedEntity, startCDATA, startDocument, startDTD, startEntity, startPrefixMapping
 
Methods inherited from class org.apache.cocoon.xml.AbstractXMLProducer
setConsumer, setContentHandler, setLexicalHandler
 
Methods inherited from class org.apache.avalon.framework.logger.AbstractLogEnabled
enableLogging, getLogger, setupLogger, setupLogger, setupLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.xml.sax.ContentHandler
endDocument, endPrefixMapping, ignorableWhitespace, processingInstruction, setDocumentLocator, skippedEntity, startDocument, startPrefixMapping
 
Methods inherited from interface org.xml.sax.ext.LexicalHandler
comment, endCDATA, endDTD, endEntity, startCDATA, startDTD, startEntity
 
Methods inherited from interface org.apache.cocoon.xml.XMLProducer
setConsumer
 

Field Detail

I18N_NAMESPACE_URI

public static final String I18N_NAMESPACE_URI
The namespace for i18n is "http://apache.org/cocoon/i18n/2.1".

See Also:
Constant Field Values

I18N_OLD_NAMESPACE_URI

public static final String I18N_OLD_NAMESPACE_URI
The old namespace for i18n is "http://apache.org/cocoon/i18n/2.0".

See Also:
Constant Field Values

I18N_TEXT_ELEMENT

public static final String I18N_TEXT_ELEMENT
i18n:text element is used to translate any text, with or without markup. Example:
   <i18n:text>
     This is <strong>translated</strong> string.
   </i18n:text>
 

See Also:
Constant Field Values

I18N_TRANSLATE_ELEMENT

public static final String I18N_TRANSLATE_ELEMENT
i18n:translate element is used to translate text with parameter substitution. Example:
 <i18n:translate>
   <i18n:text>This is translated string with {0} param</i18n:text>
   <i18n:param>1</i18n:param>
 </i18n:translate>
 
The i18n:text fragment can include markup and parameters at any place. Also do parameters, which can include i18n:text, i18n:date, etc. elements (without keys only).

See Also:
I18N_TEXT_ELEMENT, I18N_PARAM_ELEMENT, Constant Field Values

I18N_CHOOSE_ELEMENT

public static final String I18N_CHOOSE_ELEMENT
i18n:choose element is used to translate elements in-place. The first i18n:when element matching the current locale is selected and the others are discarded.

To specify what to do if no locale matched, simply add a node with locale="*". Note that this element must be the last child of <i18n:choose>.

 <i18n:choose>
   <i18n:when locale="en">
     Good Morning
   </en>
   <i18n:when locale="fr">
     Bonjour
   </jp>
   <i18n:when locale="jp">
     Aligato?
   </jp>
   <i18n:otherwise>
     Sorry, i don't know how to say hello in your language
   </jp>
 <i18n:translate>
 

You can include any markup within i18n:when elements, with the exception of other i18n:* elements.

Since:
2.1
See Also:
I18N_IF_ELEMENT, I18N_LOCALE_ATTRIBUTE, Constant Field Values

I18N_WHEN_ELEMENT

public static final String I18N_WHEN_ELEMENT
i18n:when is used to test a locale. It can be used within i18:choose elements or alone. Note: Using locale="*" here has no sense. Example:
 <greeting>
   <i18n:when locale="en">Hello</i18n:when>
   <i18n:when locale="fr">Bonjour</i18n:when>
 </greeting>
 

Since:
2.1
See Also:
I18N_LOCALE_ATTRIBUTE, I18N_CHOOSE_ELEMENT, Constant Field Values

I18N_IF_ELEMENT

public static final String I18N_IF_ELEMENT
i18n:if is used to test a locale. Example:
 <greeting>
   <i18n:if locale="en">Hello</i18n:when>
   <i18n:if locale="fr">Bonjour</i18n:when>
 </greeting>
 

Since:
2.1
See Also:
I18N_LOCALE_ATTRIBUTE, I18N_CHOOSE_ELEMENT, I18N_WHEN_ELEMENT, Constant Field Values

I18N_OTHERWISE_ELEMENT

public static final String I18N_OTHERWISE_ELEMENT
i18n:otherwise is used to match any locale when no matching locale has been found inside an i18n:choose block.

Since:
2.1
See Also:
I18N_CHOOSE_ELEMENT, I18N_WHEN_ELEMENT, Constant Field Values

I18N_PARAM_ELEMENT

public static final String I18N_PARAM_ELEMENT
i18n:param is used with i18n:translate to provide substitution params. The param can have i18n:text as its value to provide multilungual value. Parameters can have additional attributes to be used for formatting:

See Also:
I18N_TRANSLATE_ELEMENT, I18N_DATE_ELEMENT, I18N_TIME_ELEMENT, I18N_DATE_TIME_ELEMENT, I18N_NUMBER_ELEMENT, Constant Field Values

I18N_PARAM_NAME_ATTRIBUTE

public static final String I18N_PARAM_NAME_ATTRIBUTE
This attribute affects a name to the param that could be used for substitution.

Since:
2.1
See Also:
Constant Field Values

I18N_DATE_ELEMENT

public static final String I18N_DATE_ELEMENT
i18n:date is used to provide a localized date string. Allowed attributes are: pattern, src-pattern, locale, src-locale. Usage examples:
  <i18n:date src-pattern="short" src-locale="en_US" locale="de_DE">
    12/24/01
  </i18n:date>

  <i18n:date pattern="dd/MM/yyyy" />
 
If no value is specified then the current date will be used. E.g.:
   <i18n:date />
 
Displays the current date formatted with default pattern for the current locale.

See Also:
I18N_PARAM_ELEMENT, I18N_DATE_TIME_ELEMENT, I18N_TIME_ELEMENT, I18N_NUMBER_ELEMENT, Constant Field Values

I18N_DATE_TIME_ELEMENT

public static final String I18N_DATE_TIME_ELEMENT
i18n:date-time is used to provide a localized date and time string. Allowed attributes are: pattern, src-pattern, locale, src-locale. Usage examples:
  <i18n:date-time src-pattern="short" src-locale="en_US" locale="de_DE">
    12/24/01 1:00 AM
  </i18n:date>

  <i18n:date-time pattern="dd/MM/yyyy hh:mm" />
 
If no value is specified then the current date and time will be used. E.g.:
  <i18n:date-time />
 
Displays the current date formatted with default pattern for the current locale.

See Also:
I18N_PARAM_ELEMENT, I18N_DATE_ELEMENT, I18N_TIME_ELEMENT, I18N_NUMBER_ELEMENT, Constant Field Values

I18N_TIME_ELEMENT

public static final String I18N_TIME_ELEMENT
i18n:time is used to provide a localized time string. Allowed attributes are: pattern, src-pattern, locale, src-locale. Usage examples:
  <i18n:time src-pattern="short" src-locale="en_US" locale="de_DE">
    1:00 AM
  </i18n:time>

 <i18n:time pattern="hh:mm:ss" />
 
If no value is specified then the current time will be used. E.g.:
  <i18n:time />
 
Displays the current time formatted with default pattern for the current locale.

See Also:
I18N_PARAM_ELEMENT, I18N_DATE_TIME_ELEMENT, I18N_DATE_ELEMENT, I18N_NUMBER_ELEMENT, Constant Field Values

I18N_NUMBER_ELEMENT

public static final String I18N_NUMBER_ELEMENT
i18n:number is used to provide a localized number string. Allowed attributes are: pattern, src-pattern, locale, src-locale, type. Usage examples:
  <i18n:number src-pattern="short" src-locale="en_US" locale="de_DE">
    1000.0
  </i18n:number>

 <i18n:number type="currency" />
 
If no value is specifies then 0 will be used.

See Also:
I18N_PARAM_ELEMENT, I18N_DATE_TIME_ELEMENT, I18N_TIME_ELEMENT, I18N_DATE_ELEMENT, Constant Field Values

I18N_CURRENCY_ELEMENT

public static final String I18N_CURRENCY_ELEMENT
Currency element name

See Also:
Constant Field Values

I18N_PERCENT_ELEMENT

public static final String I18N_PERCENT_ELEMENT
Percent element name

See Also:
Constant Field Values

I18N_INT_CURRENCY_ELEMENT

public static final String I18N_INT_CURRENCY_ELEMENT
Integer currency element name

See Also:
Constant Field Values

I18N_CURRENCY_NO_UNIT_ELEMENT

public static final String I18N_CURRENCY_NO_UNIT_ELEMENT
Currency without unit element name

See Also:
Constant Field Values

I18N_INT_CURRENCY_NO_UNIT_ELEMENT

public static final String I18N_INT_CURRENCY_NO_UNIT_ELEMENT
Integer currency without unit element name

See Also:
Constant Field Values

I18N_KEY_ATTRIBUTE

public static final String I18N_KEY_ATTRIBUTE
This attribute is used with i18n:text element to indicate the key of the according message. The character data of the element will be used if no message is found by this key. E.g.:
 <i18n:text i18n:key="a_key">article_text1</i18n:text>
 

See Also:
Constant Field Values

I18N_ATTR_ATTRIBUTE

public static final String I18N_ATTR_ATTRIBUTE
This attribute is used with any element (even not i18n) to translate attribute values. Should contain whitespace separated attribute names that should be translated:
 <para title="first" name="article" i18n:attr="title name"/>
 
Attribute value considered as key in message catalogue.

See Also:
Constant Field Values

I18N_EXPR_ATTRIBUTE

public static final String I18N_EXPR_ATTRIBUTE
This attribute is used with any element (even not i18n) to evaluate attribute values. Should contain whitespace separated attribute names that should be evaluated:
 <para title="first" name="{one} {two}" i18n:attr="name"/>
 
Attribute value considered as expression containing text and catalogue keys in curly braces.

See Also:
Constant Field Values

I18N_SRC_PATTERN_ATTRIBUTE

public static final String I18N_SRC_PATTERN_ATTRIBUTE
This attribute is used with date and number formatting elements to indicate the pattern that should be used to parse the element value.

See Also:
I18N_PARAM_ELEMENT, I18N_DATE_TIME_ELEMENT, I18N_DATE_ELEMENT, I18N_TIME_ELEMENT, I18N_NUMBER_ELEMENT, Constant Field Values

I18N_PATTERN_ATTRIBUTE

public static final String I18N_PATTERN_ATTRIBUTE
This attribute is used with date and number formatting elements to indicate the pattern that should be used to format the element value.

See Also:
I18N_PARAM_ELEMENT, I18N_DATE_TIME_ELEMENT, I18N_DATE_ELEMENT, I18N_TIME_ELEMENT, I18N_NUMBER_ELEMENT, Constant Field Values

I18N_LOCALE_ATTRIBUTE

public static final String I18N_LOCALE_ATTRIBUTE
This attribute is used with date and number formatting elements to indicate the locale that should be used to format the element value. Also used for in-place translations.

See Also:
I18N_PARAM_ELEMENT, I18N_DATE_TIME_ELEMENT, I18N_DATE_ELEMENT, I18N_TIME_ELEMENT, I18N_NUMBER_ELEMENT, I18N_WHEN_ELEMENT, Constant Field Values

I18N_SRC_LOCALE_ATTRIBUTE

public static final String I18N_SRC_LOCALE_ATTRIBUTE
This attribute is used with date and number formatting elements to indicate the locale that should be used to parse the element value.

See Also:
I18N_PARAM_ELEMENT, I18N_DATE_TIME_ELEMENT, I18N_DATE_ELEMENT, I18N_TIME_ELEMENT, I18N_NUMBER_ELEMENT, Constant Field Values

I18N_VALUE_ATTRIBUTE

public static final String I18N_VALUE_ATTRIBUTE
This attribute is used with date and number formatting elements to indicate the value that should be parsed and formatted. If value attribute is not used then the character data of the element will be used.

See Also:
I18N_PARAM_ELEMENT, I18N_DATE_TIME_ELEMENT, I18N_DATE_ELEMENT, I18N_TIME_ELEMENT, I18N_NUMBER_ELEMENT, Constant Field Values

I18N_TYPE_ATTRIBUTE

public static final String I18N_TYPE_ATTRIBUTE
Deprecated. since 2.1. Use nested tags instead, e.g.: <i18n:param><i18n:date/></i18n:param>
This attribute is used with i18:param to indicate the parameter type: date, time, date-time or number, currency, percent, int-currency, currency-no-unit, int-currency-no-unit. Also used with i18:translate to indicate inplace translations: inplace

See Also:
Constant Field Values

CURRENCY_LOCALE_ATTRIBUTE

public static final String CURRENCY_LOCALE_ATTRIBUTE
This attribute is used to specify a different locale for the currency. When specified, this locale will be combined with the "normal" locale: e.g. the seperator symbols are taken from the normal locale but the currency symbol and possition will be taken from the currency locale. This enables to see a currency formatted for Euro but with US grouping and decimal char.

See Also:
Constant Field Values

I18N_CATALOGUE_ATTRIBUTE

public static final String I18N_CATALOGUE_ATTRIBUTE
This attribute can be used on i18n:text to indicate the catalogue from which the key should be retrieved. This attribute is optional, if it is not mentioned the default catalogue is used.

See Also:
Constant Field Values

I18N_LOCALE

public static final String I18N_LOCALE
This configuration parameter specifies the default locale to be used.

See Also:
Constant Field Values

I18N_DEFAULT_CATALOGUE_ID

public static final String I18N_DEFAULT_CATALOGUE_ID
This configuration parameter specifies the id of the catalogue to be used as default catalogue, allowing to redefine the default catalogue on the pipeline level.

See Also:
Constant Field Values

I18N_UNTRANSLATED

public static final String I18N_UNTRANSLATED
This configuration parameter specifies the message that should be displayed in case of a not translated text (message not found).

See Also:
Constant Field Values

I18N_PRELOAD

public static final String I18N_PRELOAD
This configuration parameter specifies locale for which catalogues should be preloaded.

See Also:
Constant Field Values

I18N_FRACTION_DIGITS_ATTRIBUTE

public static final String I18N_FRACTION_DIGITS_ATTRIBUTE
fraction-digits attribute is used with i18:number to indicate the number of digits behind the fraction

See Also:
Constant Field Values

manager

protected ServiceManager manager
Component (service) manager


factory

protected BundleFactory factory
Message bundle loader factory component (service)


objectModel

protected Map objectModel

locale

protected Locale locale
Locale

Constructor Detail

I18nTransformer

public I18nTransformer()
Method Detail

getLocale

public Locale getLocale()
Returns the current locale setting of this transformer instance.

Returns:
current Locale object

getKey

public Serializable getKey()
Implemenation of CacheableProcessingComponents. Generates unique key for the current locale.

Specified by:
getKey in interface CacheableProcessingComponent
Returns:
The generated key or null if the component is currently not cacheable.

getValidity

public SourceValidity getValidity()
Implementation of CacheableProcessingComponent. Generates validity object for this transformer or null if this instance is not cacheable.

Specified by:
getValidity in interface CacheableProcessingComponent
Returns:
The generated validity object or null if the component is currently not cacheable.

service

public void service(ServiceManager manager)
             throws ServiceException
Look up the BundleFactory to be used.

Specified by:
service in interface Serviceable
Throws:
ServiceException

configure

public void configure(Configuration conf)
               throws ConfigurationException
Implementation of Configurable interface. Configure this transformer.

Specified by:
configure in interface Configurable
Throws:
ConfigurationException

setup

public void setup(SourceResolver resolver,
                  Map objectModel,
                  String source,
                  Parameters parameters)
           throws ProcessingException,
                  SAXException,
                  IOException
Setup current instance of transformer.

Specified by:
setup in interface SitemapModelComponent
Throws:
ProcessingException
SAXException
IOException

startElement

public void startElement(String uri,
                         String name,
                         String raw,
                         Attributes attr)
                  throws SAXException
Description copied from class: AbstractXMLPipe
Receive notification of the beginning of an element.

Specified by:
startElement in interface ContentHandler
Overrides:
startElement in class AbstractXMLPipe
Parameters:
uri - The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
name - The local name (without prefix), or the empty string if Namespace processing is not being performed.
raw - The raw XML 1.0 name (with prefix), or the empty string if raw names are not available.
attr - The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.
Throws:
SAXException

endElement

public void endElement(String uri,
                       String name,
                       String raw)
                throws SAXException
Description copied from class: AbstractXMLPipe
Receive notification of the end of an element.

Specified by:
endElement in interface ContentHandler
Overrides:
endElement in class AbstractXMLPipe
Parameters:
uri - The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
name - The local name (without prefix), or the empty string if Namespace processing is not being performed.
raw - The raw XML 1.0 name (with prefix), or the empty string if raw names are not available.
Throws:
SAXException

characters

public void characters(char[] ch,
                       int start,
                       int len)
                throws SAXException
Description copied from class: AbstractXMLPipe
Receive notification of character data.

Specified by:
characters in interface ContentHandler
Overrides:
characters in class AbstractXMLPipe
Parameters:
ch - The characters from the XML document.
start - The start position in the array.
len - The number of characters to read from the array.
Throws:
SAXException

getMessage

protected ParamSaxBuffer getMessage(String catalogueID,
                                    String key)
Helper method to retrieve a message from the dictionary.

Parameters:
catalogueID - if not null, this catalogue will be used instead of the default one.
Returns:
SaxBuffer containing message, or null if not found.

recycle

public void recycle()
Description copied from class: AbstractXMLProducer
Recycle the producer by removing references, and resetting handlers to null (empty) implementations.

Specified by:
recycle in interface Recyclable
Overrides:
recycle in class AbstractXMLProducer

dispose

public void dispose()
Specified by:
dispose in interface Disposable


Copyright © 1999-2010 The Apache Software Foundation. All Rights Reserved.