NOTE: This information applies to an older version of Tapestry. For current information on Tapestry's localization support, see http://tapestry.apache.org/localization.html. |
LocaleSelectionFlags component
Here is a component that offer to the visitor to change the site Locale by clicking on a flag.
Usage
<span jwcid="@Util/LocaleSelectionFlags" flagsfolder="/Public/flags"></span>
The 'parameter' flagsfolder tell the component where are flags images.
Todo
- change the hardcoded .gif extension to a optional parameter with .gif as default-value.
- change hardcoded list of locales to a list of locales codes parameter like "fr,en,de,it".
Others sources of documentation
http://tapestry.apache.org/tapestry4.1/usersguide/template.html#Localization
http://tapestry.apache.org/tapestry4.1/usersguide/localization.html
http://wiki.apache.org/tapestry/DefaultLocale
LocaleSelectionFlags.jwc
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE component-specification PUBLIC "-//Apache Software Foundation//Tapestry Specification 4.0//EN" "http://tapestry.apache.org/dtd/Tapestry_4_0.dtd"> <component-specification class="web.components.util.LocaleSelectionFlags" > <parameter name="flagsfolder" required="yes" cache="yes" /> <property name="currentlocaleCode" /> <property name="localeCodeItem" /> <component id="localesLoop" type="For" > <binding name="source" value="availablesLocalesCode" /> <binding name="value" value="localeCodeItem" /> </component> </component-specification>
LocaleSelectionFlags.html
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>LocaleSelectionFlags component's template</title>
</head>
<body>
<p>Component LocaleSelectionFlags</p>
<span jwcid="$content$">
<span jwcid="@Any" value="ognl:currentlocaleCode=page.engine.locale.language"/>
<span jwcid="localesLoop">
<span jwcid="@Insert" value="ognl:localeCodeItemName" />
<span jwcid="@If" condition="ognl:currentlocaleCode==localeCodeItem" >
<img jwcid="@Any" src="ognl:localeCodeItemFlagUrl" border="0" />
</span>
<span jwcid="@Else">
<a jwcid="@DirectLink" listener="listener:selectLocale" parameters="ognl:localeCodeItem">
<img jwcid="@Any" src="ognl:localeCodeItemFlagUrl" border="0" />
</a>
</span>
</span>
</span>
</body>
</html>
LocaleSelectionFlags.java
package web.components.util;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Set;
import org.apache.tapestry.BaseComponent;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.InjectObject;
public abstract class LocaleSelectionFlags extends BaseComponent
{
LinkedHashMap<String,Locale> _availablesLocales;
@InjectObject("infrastructure:contextPath")
public abstract String getContextPath();
public abstract String getFlagsfolder();
public abstract String getLocaleCodeItem();
public LinkedHashMap<String,Locale> getAvailablesLocales()
{
if( _availablesLocales == null )
{
_availablesLocales = new LinkedHashMap<String,Locale>();
_availablesLocales.put( Locale.ENGLISH.getLanguage(), Locale.ENGLISH );
_availablesLocales.put( Locale.FRENCH.getLanguage(),Locale.FRENCH );
_availablesLocales.put( Locale.GERMAN.getLanguage(),Locale.GERMAN );
_availablesLocales.put( Locale.ITALIAN.getLanguage(),Locale.ITALIAN );
}
return _availablesLocales;
}
public Set<String> getAvailablesLocalesCode()
{
return getAvailablesLocales().keySet();
}
public String getLocaleCodeItemName()
{
Locale itemLocale = getAvailablesLocales().get(getLocaleCodeItem());
return itemLocale.getDisplayName(itemLocale);
}
public String getLocaleCodeItemFlagUrl()
{
return getContextPath() + getFlagsfolder() + "/" + getLocaleCodeItem() + ".gif";
}
public void selectLocale(String localeCode)
{
Locale newLocale = getAvailablesLocales().get(localeCode);
this.getPage().getEngine().setLocale( newLocale );
IRequestCycle cycle = this.getPage().getEngine().getInfrastructure().getRequestCycle();
cycle.cleanup();
cycle.activate(getPage().getPageName());
}
}