org.apache.velocity.tools.view
Class MultiViewsTool

java.lang.Object
  extended by org.apache.velocity.tools.view.MultiViewsTool
Direct Known Subclasses:
MultiViewsTool

@DefaultKey(value="i18n")
@InvalidScope(value="application")
public class MultiViewsTool
extends java.lang.Object

Allows for transparent content negotiation in a manner mimicking Apache httpd's MultiViews.

Reads the default language out of the ViewToolContext as org.apache.velocity.tools.view.i18n.defaultLanguage. See find(String, String) and find(String, Locale) for usage.

Since:
VelocityTools 2.0
Version:
$Id: MultiViewsTool.java 722517 2008-12-02 16:38:19Z nbubna $
Author:
Daniel Rall

Field Summary
protected static java.lang.String DEFAULT_LANGUAGE_KEY
          The key used to search initialization, context, and JVM parameters for the default language to use.
protected  java.lang.String defaultLanguage
          The two character abbreviation for the request's default language.
protected  org.apache.velocity.app.VelocityEngine engine
           
 
Constructor Summary
MultiViewsTool()
           
 
Method Summary
 void configure(java.util.Map params)
          Extracts the default language from the specified ViewContext, looking first at the Velocity context, then the servlet context, then lastly at the JVM default.
protected  void configure(ViewToolContext ctx)
           
 java.lang.String find(java.lang.String name)
          Calls find(String, String) using the default language.
 java.lang.String find(java.lang.String name, java.util.Locale locale)
          Calls find(String, String) using the language extracted from locale.
 java.lang.String find(java.lang.String name, java.lang.String language)
          Finds the a localized version of the requested Velocity resource (such as a file or template) which is most appropriate for the locale of the current request.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_LANGUAGE_KEY

protected static final java.lang.String DEFAULT_LANGUAGE_KEY
The key used to search initialization, context, and JVM parameters for the default language to use.

See Also:
Constant Field Values

defaultLanguage

protected java.lang.String defaultLanguage
The two character abbreviation for the request's default language.


engine

protected org.apache.velocity.app.VelocityEngine engine
Constructor Detail

MultiViewsTool

public MultiViewsTool()
Method Detail

configure

public void configure(java.util.Map params)
Extracts the default language from the specified ViewContext, looking first at the Velocity context, then the servlet context, then lastly at the JVM default. This "narrow scope to wide scope" pattern makes it easy to setup language overrides at different levels within your application.

Parameters:
params - the Map of configuration parameters
Throws:
java.lang.IllegalArgumentException - if the param is not a ViewContext

configure

protected void configure(ViewToolContext ctx)

find

public java.lang.String find(java.lang.String name,
                             java.util.Locale locale)
Calls find(String, String) using the language extracted from locale.

See Also:
find(String, String)

find

public java.lang.String find(java.lang.String name)
Calls find(String, String) using the default language.

See Also:
find(String, String)

find

public java.lang.String find(java.lang.String name,
                             java.lang.String language)

Finds the a localized version of the requested Velocity resource (such as a file or template) which is most appropriate for the locale of the current request. Use in conjuction with Apache httpd's MultiViews, or by itself.

Usage from a template would be something like the following:

 #parse( $i18n.find('header.vm', 'en') )
 #include( $i18n.find('my_page.html', 'en') )
 #parse( $i18n.find('footer.vm', 'en') )
 
You might also wrap this method using another pull/view tool which does internationalization/localization/content negation for a single point of access.

Parameters:
name - The unlocalized name of the file to find.
language - The language to find localized context for.
Returns:
The localized file name, or name if it is not localizable.


Copyright (c) 2003-2007 Apache Software Foundation