Hello,
I'm using display tag whith the struts2 framework in my project and
I have coded a resource provider and locale resolver for it(custom i18n
localization), I18nWebwork2Adapter.
It is tested with diplaytag 1.1 and struts2 2.0.1
I hope you find this class worth enough to be added to your display tag
project.
I don't know if this is the right way, suppose is not, to propose a
contribution.
Forgive my ignorance in this subject.
Cheers, Carles
package org.displaytag.localization;
import java.util.Iterator;
import java.util.Locale;
import java.util.Collections;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.views.jsp.TagUtils ;
import org.displaytag.Messages;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.util.OgnlValueStack ;
import com.opensymphony.xwork2.util.ValueStack;
/**
* Struts2/Webwork2 implementation of a resource provider and locale
resolver.<br/>
* Tested with diplaytag 1.1 and struts2 2.0.1<br/>
* # Set this values in displaytag.properties #<br/>
* <code>
* locale.resolver = org.displaytag.localization.I18nWebwork2Adapter<br/>
* locale.provider = org.displaytag.localization.I18nWebwork2Adapter <br/>
* </code>
* Date: 08-mar-2007
* Time: 13:11:31
* @author Carles Gasques BarĂ³
* @version 1.0
*/
public class I18nWebwork2Adapter implements LocaleResolver,
I18nResourceProvider
{
public static final String UNDEFINED_KEY = "???";
private static Log log = LogFactory.getLog(I18nWebworkAdapter.class);
public Locale resolveLocale(HttpServletRequest request)
{
Locale result = null;
ValueStack stack = ActionContext.getContext().getValueStack();
Iterator iterator = stack.getRoot().iterator();
while (iterator.hasNext())
{
Object o = iterator.next();
if (o instanceof LocaleProvider)
{
LocaleProvider lp = (LocaleProvider) o;
result = lp.getLocale();
break;
}
}
if (result == null)
{
log.debug("Missing LocalProvider actions, init locale to
default");
result = Locale.getDefault ();
}
return result;
}
public String getResource(String resourceKey, String defaultValue, Tag
tag, PageContext pageContext)
{
String key = (resourceKey != null) ? resourceKey : defaultValue;
String message = null;
OgnlValueStack stack =
(OgnlValueStack)TagUtils.getStack(pageContext);
Iterator iterator = stack.getRoot().iterator();
while(iterator.hasNext())
{
Object o = iterator.next();
if(o instanceof TextProvider){
TextProvider tp = (TextProvider) o;
message = tp.getText( key, defaultValue,
Collections.EMPTY_LIST , stack);
break;
}
}
if (message == null && resourceKey != null)
{
log.debug(Messages.getString("Localization.missingkey ",
resourceKey));
message = UNDEFINED_KEY + resourceKey + UNDEFINED_KEY;
}
return message;
}
}
<<<<<<<<<
--
There is nothing like looking, if you want to find something. You certainly
usually find something, if you look, but it is not always quite the
something you were after.
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
displaytag-devel mailing list
displaytag-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/displaytag-devel