Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare locale String's in GWT?

Tags:

java

gwt

yeah, because GWT not support java.text.Collator (and java.util.Locale also) ;/

Any solutions?

like image 829
Samoth Avatar asked May 24 '11 14:05

Samoth


1 Answers

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.

like image 146
kensen john Avatar answered Nov 03 '22 07:11

kensen john