How to compare locale String's in GWT?
yeah, because GWT not support java.text.Collator (and java.uti开发者_如何学JAVAl.Locale also) ;/
Any solutions?
I found this solution which uses javascript. This is because GWT doesn't support Collator.
http://osdir.com/ml/GoogleWebToolkit/2009-06/msg01572.html
public class Collator {
public static final Collator getInstance() {
return instance;
}
private static final Collator instance = new Collator();
public native int compare( String source, String target ); /*-{
return source.localeCompare( target );
}-*/
}
I have never used it personally, but it looks promising. You might have to make changes so that there are no cross browser issues.
Edit:
Read up on JSNI.
This allows GWT to invoke raw "javascript" code within your Java code. This is what we are doing in the above class. The method "compare" makes a native call to javascript.
http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html#writing
Add Collator.java to your current work space.
You should be able to compare as follows.
Collator customCollator = Collator.getInstance();
if ( customCollator.compare( srcString , targetString ) > 0 )
{
//the compare method will return an int.
// write your code here.
}
Hope this helps.
the easiest way is to add a locale property to the constants class.
I have a GeneralConstants class which is defined like so
import com.google.gwt.i18n.client.Constants;
public interface GeneralConstants extends Constants {
@DefaultStringValue( "de" )
String locale();
in every constants class I can inherit the locale and I can easily define it in every language
to know what locale I'm in. I just call the attribute
private static GeneralConstants c = GWT.create( GeneralConstants.class );
...
formData.getForm().getName( c.locale() )
there is also a way to get the currently set locales but I can't remember :-)
精彩评论