org.apache.cocoon.transformation
Class I18nTransformer

java.lang.Object
  |
  +--org.apache.avalon.framework.logger.AbstractLoggable
        |
        +--org.apache.cocoon.xml.AbstractXMLProducer
              |
              +--org.apache.cocoon.xml.AbstractXMLPipe
                    |
                    +--org.apache.cocoon.transformation.AbstractTransformer
                          |
                          +--org.apache.cocoon.transformation.I18nTransformer
All Implemented Interfaces:
org.apache.avalon.framework.component.Component, org.apache.avalon.framework.component.Composable, org.apache.avalon.framework.configuration.Configurable, org.xml.sax.ContentHandler, org.apache.avalon.framework.activity.Disposable, org.xml.sax.ext.LexicalHandler, org.apache.avalon.framework.logger.Loggable, org.apache.avalon.excalibur.pool.Poolable, org.apache.avalon.excalibur.pool.Recyclable, SitemapModelComponent, Transformer, XMLConsumer, XMLPipe, XMLProducer

public class I18nTransformer
extends AbstractTransformer
implements org.apache.avalon.framework.component.Composable, org.apache.avalon.framework.configuration.Configurable, org.apache.avalon.framework.activity.Disposable

Internationalization transformer is used to transform i18n markup into text based on a particular locale.

i18n transformer

The i18n transformer works by obtaining the users locale based on request, session attributes or a cookie data. See LocaleAction.getLocaleAttribute(Map) for details. It then attempts to find a message catalogue that satisifies the particular locale, and use it for for text replacement within i18n markup.

Catalogues are maintained in separate files, with a naming convention similar to that of ResourceBundle (See java.util.ResourceBundle). ie. basename_locale, where basename can be any name, and locale can be any locale specified using ISO 639/3166 characters (eg. en_AU, de_AT, es).
NOTE: ISO 639 is not a stable standard; some of the language codes it defines (specifically iw, ji, and in) have changed (see java.util.Locale for details).

Catalogues

Catalogues are of the following format:

 <?xml version="1.0"?>
 <!-- message catalogue file for locale ... -->
 <catalogue xml:lang="locale">
        <message key="key">text</message>
        ....
 </catalogue>
 
Where key specifies a particular message for that language.

Usage

Files to be translated contain the following markup:

 <?xml version="1.0"?>
 ... some text, translate <i18n:text>key</i18n:text>
 
At runtime, the i18n transformer will find a message catalogue for the user's locale, and will appropriately replace the text between the <i18n:text> markup, using the value between the tags as the lookup key.

If the i18n transformer cannot find an appropriate message catalogue for the user's given locale, it will recursively try to locate a parent message catalogue, until a valid catalogue can be found. ie:

eg: Assuming a basename of messages and a locale of en_AU (no variant), the following search will occur: This allows the developer to write a hierarchy of message catalogues, at each defining messages with increasing depth of variation.

Sitemap configuration

 <map:transformer name="i18n"
     src="org.apache.cocoon.transformation.I18nTransformer">

     <catalogue-name>messages</catalogue-name>
     <catalogue-location>translations</catalogue-location>
     <untranslated-text>untranslated</untranslated-text>
     <cache-at-startup>true</cache-at-startup>
 </map:transformer>
 
where:

To use the transformer in a pipeline, simply specify it in a particular transform. eg:

 <map:match pattern="file">
     <map:generate src="file.xml"/>
     <map:transform type="i18n"/>
     <map:serialize/>
 </map:match>
 

Note, catalogue-name, catalogue-location and untranslated-text can all be overridden at the pipeline level by specifying them as parameters to the transform statement.

For date, time and number formatting use the following tags:

For date, date-time and time the pattern and src-pattern attribute may have also values of: short, medium, long or full. (See java.text.DateFormat for more info on this).

For date, date-time, time and number a different locale and source-locale can be specified:

 <i18n:date src-pattern="short" src-locale="en_US" locale="de_DE">
      12/24/01
 </i18n:date>
 
will result in 24.12.2001.

A given real pattern and src-pattern (not short, medium, long, full) overwrites the locale and src-locale

Future work coming:

Version:
CVS $Id: I18nTransformer.java,v 1.13.2.6 2002/11/27 10:51:29 kpiroumian Exp $
Author:
Konstantin Piroumian, Marcus Crafter, Michael Enke, Lassi Immonen

Field Summary
static java.lang.String I18N_ATTR_ATTRIBUTE
          This attribute is used with any element (even not i18n) to translate attribute values.
static java.lang.String I18N_CACHE_STARTUP
          This configuration parameter specifies if the message catalog should be cached at startup.
static java.lang.String I18N_CATALOGUE_LOCATION
          This configuration parameter specifies the message catalog location relative to the current sitemap.
static java.lang.String I18N_CATALOGUE_NAME
          This configuration parameter specifies the message catalog name.
static java.lang.String I18N_CATALOGUE_PREFIX
          This constant specifies the XPath prefix that will be used to retrieve a value from a message catalogue.
static java.lang.String I18N_CURRENCY_ELEMENT
          currency element
static java.lang.String I18N_CURRENCY_NO_UNIT_ELEMENT
          currency without unit element
static java.lang.String I18N_DATE_ELEMENT
          i18n:date is used to provide a localized date string.
static java.lang.String I18N_DATE_TIME_ELEMENT
          i18n:date-time is used to provide a localized date and time string.
static java.lang.String I18N_FRACTION_DIGITS_ATTRIBUTE
          fraction-digits attribute is used with i18:number to indicate the number of digits behind the fraction
static java.lang.String I18N_INT_CURRENCY_ELEMENT
          integer currency element
static java.lang.String I18N_INT_CURRENCY_NO_UNIT_ELEMENT
          integer currency without unit element
static java.lang.String I18N_KEY_ATTRIBUTE
          This attribute is used with i18n:text element to indicate the key of the according message.
static java.lang.String I18N_LOCALE
          This configuration parameter specifies the default locale to be used.
static java.lang.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 java.lang.String I18N_NAMESPACE_URI
          The namespace for i18n is "http://apache.org/cocoon/i18n/2.0".
static java.lang.String I18N_NUMBER_ELEMENT
          i18n:number is used to provide a localized number string.
static java.lang.String I18N_PARAM_ELEMENT
          i18n:param is used with i18n:translate to provide substitution params.
static java.lang.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 java.lang.String I18N_PERCENT_ELEMENT
          percent element
static java.lang.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 java.lang.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 java.lang.String I18N_TEXT_ELEMENT
          i18n:text element is used to translate simple text, e.g.:
<i18n:text>This is a multilanguage string</i18n:text>
static java.lang.String I18N_TIME_ELEMENT
          i18n:time is used to provide a localized time string.
static java.lang.String I18N_TRANSLATE_ELEMENT
          i18n:translate element is used to translate text with parameter substitution, e.g.:
<i18n:translate> This is a multilanguage string with {0} param <i18n:param>1</i18n:param> </i18n:translate>
static java.lang.String I18N_TYPE_ATTRIBUTE
          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.
static java.lang.String I18N_UNTRANSLATED
          This configuration parameter specifies the message that should be displayed in case of a not translated text (message not found).
static java.lang.String I18N_VALUE_ATTRIBUTE
          This attribute is used with date and number formatting elements to indicate the value that should be parsed and formatted.
 
Fields inherited from class org.apache.cocoon.xml.AbstractXMLProducer
contentHandler, 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 compose(org.apache.avalon.framework.component.ComponentManager manager)
           
 void configure(org.apache.avalon.framework.configuration.Configuration conf)
          Configure this transformer.
 void dispose()
           
 void endElement(java.lang.String uri, java.lang.String name, java.lang.String raw)
          Receive notification of the end of an element.
 java.util.Locale getLocale()
          Returns the current locale setting of this transformer instance.
 void recycle()
          Recycle the producer by removing references
 void setup(SourceResolver resolver, java.util.Map objectModel, java.lang.String source, org.apache.avalon.framework.parameters.Parameters parameters)
          Uses org.apache.cocoon.acting.LocaleAction.getLocale() to get language user has selected.
 void startElement(java.lang.String uri, java.lang.String name, java.lang.String raw, org.xml.sax.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.AbstractLoggable
getLogger, setLogger, 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 java.lang.String I18N_NAMESPACE_URI
The namespace for i18n is "http://apache.org/cocoon/i18n/2.0".

I18N_TEXT_ELEMENT

public static final java.lang.String I18N_TEXT_ELEMENT
i18n:text element is used to translate simple text, e.g.:
  <i18n:text>This is a multilanguage string</i18n:text>
 

I18N_TRANSLATE_ELEMENT

public static final java.lang.String I18N_TRANSLATE_ELEMENT
i18n:translate element is used to translate text with parameter substitution, e.g.:
 <i18n:translate>
     This is a multilanguage string with {0} param
     <i18n:param>1</i18n:param>
 </i18n:translate>
 
See Also:
I18N_TEXT_ELEMENT, I18N_PARAM_ELEMENT

I18N_PARAM_ELEMENT

public static final java.lang.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

I18N_DATE_ELEMENT

public static final java.lang.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

I18N_DATE_TIME_ELEMENT

public static final java.lang.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

I18N_TIME_ELEMENT

public static final java.lang.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

I18N_NUMBER_ELEMENT

public static final java.lang.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

I18N_CURRENCY_ELEMENT

public static final java.lang.String I18N_CURRENCY_ELEMENT
currency element

I18N_PERCENT_ELEMENT

public static final java.lang.String I18N_PERCENT_ELEMENT
percent element

I18N_INT_CURRENCY_ELEMENT

public static final java.lang.String I18N_INT_CURRENCY_ELEMENT
integer currency element

I18N_CURRENCY_NO_UNIT_ELEMENT

public static final java.lang.String I18N_CURRENCY_NO_UNIT_ELEMENT
currency without unit element

I18N_INT_CURRENCY_NO_UNIT_ELEMENT

public static final java.lang.String I18N_INT_CURRENCY_NO_UNIT_ELEMENT
integer currency without unit element

I18N_KEY_ATTRIBUTE

public static final java.lang.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>
 

I18N_ATTR_ATTRIBUTE

public static final java.lang.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. E.g.
 <para title="first" name="article" i18n:attr="title name" />
 

I18N_SRC_PATTERN_ATTRIBUTE

public static final java.lang.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

I18N_PATTERN_ATTRIBUTE

public static final java.lang.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

I18N_LOCALE_ATTRIBUTE

public static final java.lang.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.
See Also:
I18N_PARAM_ELEMENT, I18N_DATE_TIME_ELEMENT, I18N_DATE_ELEMENT, I18N_TIME_ELEMENT, I18N_NUMBER_ELEMENT

I18N_SRC_LOCALE_ATTRIBUTE

public static final java.lang.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

I18N_VALUE_ATTRIBUTE

public static final java.lang.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

I18N_TYPE_ATTRIBUTE

public static final java.lang.String I18N_TYPE_ATTRIBUTE
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.

I18N_LOCALE

public static final java.lang.String I18N_LOCALE
This configuration parameter specifies the default locale to be used.
Since:
2.0.4

I18N_CATALOGUE_NAME

public static final java.lang.String I18N_CATALOGUE_NAME
This configuration parameter specifies the message catalog name.

I18N_CATALOGUE_LOCATION

public static final java.lang.String I18N_CATALOGUE_LOCATION
This configuration parameter specifies the message catalog location relative to the current sitemap.

I18N_UNTRANSLATED

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

I18N_CACHE_STARTUP

public static final java.lang.String I18N_CACHE_STARTUP
This configuration parameter specifies if the message catalog should be cached at startup.

I18N_CATALOGUE_PREFIX

public static final java.lang.String I18N_CATALOGUE_PREFIX
This constant specifies the XPath prefix that will be used to retrieve a value from a message catalogue. The resulting XPath is looks like this: /catalogue/message[@key='key_value']
FIXME (KP): We need a more generic way of key interpretation: to use XPath expression as keys, or keys with non-XML dictionaries.

I18N_FRACTION_DIGITS_ATTRIBUTE

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

I18nTransformer

public I18nTransformer()
Method Detail

getLocale

public java.util.Locale getLocale()
Returns the current locale setting of this transformer instance.
Returns:
current Locale object

configure

public void configure(org.apache.avalon.framework.configuration.Configuration conf)
               throws org.apache.avalon.framework.configuration.ConfigurationException
Configure this transformer.
Specified by:
configure in interface org.apache.avalon.framework.configuration.Configurable

setup

public void setup(SourceResolver resolver,
                  java.util.Map objectModel,
                  java.lang.String source,
                  org.apache.avalon.framework.parameters.Parameters parameters)
           throws ProcessingException,
                  org.xml.sax.SAXException,
                  java.io.IOException
Uses org.apache.cocoon.acting.LocaleAction.getLocale() to get language user has selected. FIXME (KP): Why should I18nTransformer depend on an action?

compose

public void compose(org.apache.avalon.framework.component.ComponentManager manager)
Specified by:
compose in interface org.apache.avalon.framework.component.Composable

startElement

public void startElement(java.lang.String uri,
                         java.lang.String name,
                         java.lang.String raw,
                         org.xml.sax.Attributes attr)
                  throws org.xml.sax.SAXException
Description copied from class: AbstractXMLPipe
Receive notification of the beginning of an element.
Overrides:
startElement in class AbstractXMLPipe
Following copied from class: org.apache.cocoon.xml.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.
loc - 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.
a - The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.

endElement

public void endElement(java.lang.String uri,
                       java.lang.String name,
                       java.lang.String raw)
                throws org.xml.sax.SAXException
Description copied from class: AbstractXMLPipe
Receive notification of the end of an element.
Overrides:
endElement in class AbstractXMLPipe
Following copied from class: org.apache.cocoon.xml.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.
loc - 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.

characters

public void characters(char[] ch,
                       int start,
                       int len)
                throws org.xml.sax.SAXException
Description copied from class: AbstractXMLPipe
Receive notification of character data.
Overrides:
characters in class AbstractXMLPipe
Following copied from class: org.apache.cocoon.xml.AbstractXMLPipe
Parameters:
c - The characters from the XML document.
start - The start position in the array.
len - The number of characters to read from the array.

recycle

public void recycle()
Description copied from class: AbstractXMLProducer
Recycle the producer by removing references
Overrides:
recycle in class AbstractXMLProducer

dispose

public void dispose()
Specified by:
dispose in interface org.apache.avalon.framework.activity.Disposable


Copyright © 1999-2002 Apache Software Foundation. All Rights Reserved.