如何在GWT中比较locale String?

时间:2011-05-24 14:25:34

标签: java gwt

是的,因为GWT不支持java.text.Collat​​or(和java.util.Locale); /

任何解决方案?

2 个答案:

答案 0 :(得分:5)

我发现这个解决方案使用了javascript。这是因为GWT不支持Collat​​or。

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 );
   }-*/
}

我从未亲自使用它,但看起来很有希望。 您可能必须进行更改,以便不存在跨浏览器问题。

修改
阅读JSNI 这允许GWT在Java代码中调用原始的“javascript”代码。这就是我们在上面的课程中所做的。 “比较”方法对javascript进行本地调用 http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html#writing

将Collat​​or.java添加到当前工作区。

您应该能够进行如下比较。

Collator customCollator = Collator.getInstance();
if ( customCollator.compare( srcString , targetString ) > 0 )
{
     //the compare method will return an int.
     // write your code here.
}

希望这有帮助。

答案 1 :(得分:0)

最简单的方法是将一个locale属性添加到常量类中。

我有一个GeneralConstants类,其定义如此

import com.google.gwt.i18n.client.Constants;

public interface GeneralConstants extends Constants {

   @DefaultStringValue( "de" )
   String locale();

在每个常量类中,我可以继承语言环境,我可以轻松地在每种语言中定义它

知道我所在的语言环境。我只是调用属性

private static GeneralConstants c = GWT.create( GeneralConstants.class );

...

formData.getForm().getName( c.locale() )

还有一种方法可以获取当前设置的语言环境,但我记不清了: - )