mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 03:32:51 -06:00
GWT: update to use new /grammar resource. Select from language based on Accept-Language header.
This commit is contained in:
@@ -26,21 +26,27 @@ public class GF {
|
|||||||
public void onError (Throwable e) ;
|
public void onError (Throwable e) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Languages */
|
/* Grammar */
|
||||||
|
|
||||||
public GFRequest languages (final LanguagesCallback callback) {
|
public GFRequest grammar (final GrammarCallback callback) {
|
||||||
return sendRequest("languages", null, callback);
|
return sendRequest("grammar", null, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface LanguagesCallback extends GFCallback<Languages> { }
|
public interface GrammarCallback extends GFCallback<Grammar> { }
|
||||||
|
|
||||||
public static class Languages extends JsArray<Language> {
|
public static class Grammar extends JavaScriptObject {
|
||||||
protected Languages() { }
|
protected Grammar() { }
|
||||||
|
|
||||||
|
public final native String getName() /*-{ return this.name; }-*/;
|
||||||
|
|
||||||
|
public final native String getUserLanguage() /*-{ return this.userLanguage; }-*/;
|
||||||
|
|
||||||
|
public final native JsArray<Language> getLanguages() /*-{ return this.languages; }-*/;
|
||||||
|
|
||||||
public final Language getLanguage(String name) {
|
public final Language getLanguage(String name) {
|
||||||
int c = length();
|
int c = getLanguages().length();
|
||||||
for (int i = 0; i < c; i++) {
|
for (int i = 0; i < c; i++) {
|
||||||
Language l = get(i);
|
Language l = getLanguages().get(i);
|
||||||
if (l.getName().equals(name))
|
if (l.getName().equals(name))
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,16 +36,16 @@ public class Translate implements EntryPoint {
|
|||||||
|
|
||||||
private CompletionOracle oracle;
|
private CompletionOracle oracle;
|
||||||
private SuggestBox suggest;
|
private SuggestBox suggest;
|
||||||
private GF.Languages availableLangs;
|
private GF.Grammar grammar;
|
||||||
private List<String> fromLangs;
|
private ListBox fromLangBox;
|
||||||
private List<String> toLangs;
|
private ListBox toLangBox;
|
||||||
private VerticalPanel outputPanel;
|
private VerticalPanel outputPanel;
|
||||||
private Label statusLabel;
|
private Label statusLabel;
|
||||||
|
|
||||||
private void addTranslation(String text, String toLang) {
|
private void addTranslation(String text, String toLang) {
|
||||||
Label l = new Label(text);
|
Label l = new Label(text);
|
||||||
l.addStyleName("my-translation");
|
l.addStyleName("my-translation");
|
||||||
GF.Language lang = availableLangs.getLanguage(toLang);
|
GF.Language lang = grammar.getLanguage(toLang);
|
||||||
if (lang != null) {
|
if (lang != null) {
|
||||||
l.getElement().setLang(lang.getLanguageCode());
|
l.getElement().setLang(lang.getLanguageCode());
|
||||||
}
|
}
|
||||||
@@ -53,13 +53,16 @@ public class Translate implements EntryPoint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void translate() {
|
private void translate() {
|
||||||
gf.translate(suggest.getText(), fromLangs, null, toLangs, new GF.TranslateCallback() {
|
outputPanel.clear();
|
||||||
|
setStatus("Translating...");
|
||||||
|
gf.translate(suggest.getText(), listBoxSelection(fromLangBox), null,
|
||||||
|
listBoxSelection(toLangBox), new GF.TranslateCallback() {
|
||||||
public void onResult (GF.Translations translations) {
|
public void onResult (GF.Translations translations) {
|
||||||
outputPanel.clear();
|
|
||||||
for (int i = 0; i < translations.length(); i++) {
|
for (int i = 0; i < translations.length(); i++) {
|
||||||
GF.Translation t = translations.get(i);
|
GF.Translation t = translations.get(i);
|
||||||
addTranslation(t.getText(), t.getTo());
|
addTranslation(t.getText(), t.getTo());
|
||||||
}
|
}
|
||||||
|
setStatus("Translation done.");
|
||||||
}
|
}
|
||||||
public void onError (Throwable e) {
|
public void onError (Throwable e) {
|
||||||
showError("Translation failed", e);
|
showError("Translation failed", e);
|
||||||
@@ -67,6 +70,10 @@ public class Translate implements EntryPoint {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updateLangs() {
|
||||||
|
oracle.setInputLangs(listBoxSelection(fromLangBox));
|
||||||
|
}
|
||||||
|
|
||||||
private List<String> listBoxSelection(ListBox box) {
|
private List<String> listBoxSelection(ListBox box) {
|
||||||
int c = box.getItemCount();
|
int c = box.getItemCount();
|
||||||
List<String> l = new ArrayList<String>();
|
List<String> l = new ArrayList<String>();
|
||||||
@@ -108,21 +115,20 @@ public class Translate implements EntryPoint {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
final ListBox fromLangBox = new ListBox();
|
fromLangBox = new ListBox();
|
||||||
fromLangBox.addItem("Any language", "");
|
fromLangBox.addItem("Any language", "");
|
||||||
fromLangBox.addChangeListener(new ChangeListener() {
|
fromLangBox.addChangeListener(new ChangeListener() {
|
||||||
public void onChange(Widget sender) {
|
public void onChange(Widget sender) {
|
||||||
fromLangs = listBoxSelection(fromLangBox);
|
updateLangs();
|
||||||
oracle.setInputLangs(fromLangs);
|
|
||||||
translate();
|
translate();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
final ListBox toLangBox = new ListBox();
|
toLangBox = new ListBox();
|
||||||
toLangBox.addItem("All languages", "");
|
toLangBox.addItem("All languages", "");
|
||||||
toLangBox.addChangeListener(new ChangeListener() {
|
toLangBox.addChangeListener(new ChangeListener() {
|
||||||
public void onChange(Widget sender) {
|
public void onChange(Widget sender) {
|
||||||
toLangs = listBoxSelection(toLangBox);
|
updateLangs();
|
||||||
translate();
|
translate();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -160,19 +166,21 @@ public class Translate implements EntryPoint {
|
|||||||
RootPanel.get().add(vPanel);
|
RootPanel.get().add(vPanel);
|
||||||
RootPanel.get().add(statusLabel, (Window.getClientWidth() - statusLabel.getOffsetWidth())/2, (Window.getClientHeight() - statusLabel.getOffsetHeight()));
|
RootPanel.get().add(statusLabel, (Window.getClientWidth() - statusLabel.getOffsetWidth())/2, (Window.getClientHeight() - statusLabel.getOffsetHeight()));
|
||||||
|
|
||||||
gf.languages(new GF.LanguagesCallback() {
|
gf.grammar(new GF.GrammarCallback() {
|
||||||
public void onResult(GF.Languages languages) {
|
public void onResult(GF.Grammar grammar) {
|
||||||
availableLangs = languages;
|
Translate.this.grammar = grammar;
|
||||||
for (int i = 0; i < languages.length(); i++) {
|
for (int i = 0; i < grammar.getLanguages().length(); i++) {
|
||||||
GF.Language l = languages.get(i);
|
GF.Language l = grammar.getLanguages().get(i);
|
||||||
|
String name = l.getName();
|
||||||
if (l.canParse()) {
|
if (l.canParse()) {
|
||||||
fromLangBox.addItem(l.getName());
|
fromLangBox.addItem(name);
|
||||||
|
if (name.equals(grammar.getUserLanguage())) {
|
||||||
|
fromLangBox.setSelectedIndex(fromLangBox.getItemCount()-1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
toLangBox.addItem(l.getName());
|
toLangBox.addItem(name);
|
||||||
}
|
}
|
||||||
gf.mylanguage(new GF.MyLanguageCallback() {
|
updateLangs();
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
setStatus("Loaded languages.");
|
setStatus("Loaded languages.");
|
||||||
}
|
}
|
||||||
@@ -180,9 +188,6 @@ public class Translate implements EntryPoint {
|
|||||||
public void onError (Throwable e) {
|
public void onError (Throwable e) {
|
||||||
showError("Error getting language information", e);
|
showError("Error getting language information", e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
GWT.log("Current locale: " + LocaleInfo.getCurrentLocale().getLocaleName(), null);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user