Hello,
I'm working on a project which needs to be fully internationalizable and
whose primary language will in fact not be English. I've written an
I18nBooleanConverter that I like, and have translated to a few languages
with help from people in the IRC channel. It's included below. I rather
think this should be the standard boolean conversion in wicket and
hereby release the code under the Apache license for anyone who wants to
make that happen.
Also, the converter can be configured to return "yes"/"no" or "on"/"off"
rather than "true"/"false" which is much more human friendly.
However, there is a snag in that f.ex. CheckBox, which will getValue()
and use String.isTrue() to decide if it needs to be checked or not, will
get really confused if it suddenly sees a "já" or a "oui" instead of
"true". Probably CheckBox (and perhaps other components) should be
calling the registered Boolean converter for this (which will currently
end up calling String.isTrue()). Even better would be to have a
non-final boolean isTrue(String) method which would default to calling
the registered boolean converter; or
The CheckBox.onComponentTag method where the String.isTrue call is made
is final so right now I'm using a complete copy of CheckBox which calls
into this isTrue method:
private boolean isTrue(String value) {
IConverter converter =
getApplication().getConverterLocator().getConverter(Boolean.class);
String nonEscapedValue = Strings.replaceHtmlEscapeNumber(value);
Locale locale = getSession().getLocale();
Boolean b = (Boolean) converter.convertToObject(nonEscapedValue,
locale);
return b != null && b.booleanValue();
}
Another path, which I haven't really examined, might be for whichever
component used the registered Boolean converter to create the string
which eventually got passed to CheckBox to not do that and just send
"true" or "false" and reserve the converter for conversion to and from
human readable string.
Regards,
Logi
I18nBooleanConverter follows:
package org.logi.wicket.converters;
import org.apache.wicket.util.convert.converters.AbstractConverter;
import org.apache.wicket.util.string.StringValueConversionException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
/**
* Internationalizable boolean converter. To translate to a language xx
(and by the usual extension to flavours of a language) create a
* resource <code>I18nBooleanConverter_xx.properties</code> in this
package containing the following (example in Icelandic):
*
* <pre>
* yes=já
* no=nei
* on=á
* off=af
* true=satt
* false=ósatt
* </pre>
*
* The following values will be interpreted as true: <i>já, satt, j, s,
1</i> and any other values will be interpreted as false.
*/
public class I18nBooleanConverter
extends AbstractConverter
{
private static final long serialVersionUID = 1L;
/** Convert true/false to yes/no */
public static int TYPE_YES_NO = 0;
/** Convert true/false to on/off */
public static int TYPE_ON_OFF = 1;
/** Convert true/false to true/false */
public static int TYPE_TRUE_FALSE = 2;
/** The singleton instance for a boolean converter using yes/no. */
public static final I18nBooleanConverter INSTANCE_YES_NO = new
I18nBooleanConverter(TYPE_YES_NO);
/** The singleton instance for a boolean converter using on/off. */
public static final I18nBooleanConverter INSTANCE_ON_OFF = new
I18nBooleanConverter(TYPE_ON_OFF);
/** The singleton instance for a boolean converter using yes/no. */
public static final I18nBooleanConverter INSTANCE_TRUE_FALSE = new
I18nBooleanConverter(TYPE_TRUE_FALSE);
private final Map<Locale, List<String>> yesLists = new HashMap<Locale,
List<String>>();
private final Map<Locale, List<String>> noLists = new HashMap<Locale,
List<String>>();
private final int type;
/** Create an i18n boolean converter of the given type. Use one of the
TYPE_ constants. */
public I18nBooleanConverter(int type) {
this.type = type;
}
public Object convertToObject(final String value, Locale locale) {
try {
if (value == null) return null;
List<String> yesList = getYesList(locale);
return yesList.contains(value);
}
catch (StringValueConversionException e) {
throw newConversionException("Cannot convert '" + value + "'
to Boolean", value, locale);
}
}
@Override public String convertToString(Object value, Locale locale) {
Boolean b = (Boolean) value;
if (b == null) return null;
if (b) {
return getYesList(locale).get(type);
} else {
return getNoList(locale).get(type);
}
}
private synchronized List<String> getYesList(Locale locale) {
List<String> yesList = yesLists.get(locale);
if (yesList == null) {
addLists(locale);
yesList = yesLists.get(locale);
}
return yesList;
}
private synchronized List<String> getNoList(Locale locale) {
List<String> noList = noLists.get(locale);
if (noList == null) {
addLists(locale);
noList = noLists.get(locale);
}
return noList;
}
private void addLists(Locale locale) {
ResourceBundle translation =
ResourceBundle.getBundle(getClass().getName(), locale);
List<String> yesList = new ArrayList<String>();
yesList.add(translation.getString("yes"));
yesList.add(translation.getString("on"));
yesList.add(translation.getString("true"));
yesList.add(yesList.get(0).substring(0, 1));// "y"
yesList.add(yesList.get(2).substring(0, 1));// "t"
yesList.add("1");// "1"
yesLists.put(locale, yesList);
List<String> noList = new ArrayList<String>();
noList.add(translation.getString("no"));
noList.add(translation.getString("off"));
noList.add(translation.getString("false"));
noList.add(noList.get(0).substring(0, 1));// "n"
noList.add(noList.get(2).substring(0, 1));// "f"
noList.add("0");// "0"
noLists.put(locale, noList);
}
/** @see AbstractConverter#getTargetType() */
protected Class getTargetType() {
return Boolean.class;
}
@Override public String toString() {
return "I18nBooleanConverter[" + type + "]";
}
}
begin:vcard
fn:Logi Ragnarsson
n:Ragnarsson;Logi
adr:;;;Reykjavik;;;Iceland
email;internet:[EMAIL PROTECTED]
version:2.1
end:vcard
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
Wicket-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-user