2011-05-24 66 views

回答

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. 
} 

希望這會有所幫助。

+0

我發現它也,但我不知道如何使用它;/ – Samoth 2011-05-30 10:30:13

+0

@Samoth希望幫助。編輯我的回答 – 2011-05-30 12:37:03

+0

是的,它幫助我:)謝謝! – Samoth 2011-05-30 12:56:57

0

最簡單的方法是將locale屬性添加到常量類。

我有的像這樣

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

public interface GeneralConstants extends Constants { 

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

定義的GeneralConstants類在每一個常量I類可以繼承的語言環境,我可以把它在每一種語言很容易地定義

知道我是什麼樣的語言環境。我只是叫屬性

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

...

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

還有一種方式來獲得當前設置的語言環境,但我不記得:-)

+2

這對字符串比較有幫助嗎? – Smalcat 2013-04-30 10:15:09