ICEfaces 和 Google Translate
2009-11-05 00:00:00 来源:WEB开发网大多数组件都通过 JSF 表达式语言绑定动态绑定到了 backing JavaBeans,如清单 2 所示。
清单 2. 绑定到 backing JavaBeans 的组件value="#{translate.outputText}"
value="#{translate.selectedOriginalLanguage}"
valueChangeListener="#{translate.originalLangChanged}"
value="#{translate.ORIGINAL_LANGUAGES}"
value="#{translate.selectedTranslatedLanguage}"
valueChangeListener="#{translate.translateLangChanged}"
value="#{translate.TRANSLATION_LANGUAGES}"
value="#{translate.outputText}"
effect="#{translate.textEffect}"
actionListener="#{translate.resetAll}"
actionListener="#{translate.updateText}"
步骤 2. 创建 backing JavaBean (GoogleTranslate.java)
com.ibm.translate.GoogleTranslate 类是 main.jspx 页面的 backing bean。该 bean 存储选择的当前状态以及所有的 Google Translate 信息。
清单 3 显示了 GoogleTranslate.java 类的代码。
清单 3. GoogleTranslate.javapublic class GoogleTranslate {
private Hashtable languages = new Hashtable();
private Hashtable langLabels = new Hashtable();
private Effect textEffect;
protected SelectItem[] ORIGINAL_LANGUAGES;
protected SelectItem[] TRANSLATION_LANGUAGES;
public void updateText(ActionEvent event) throws
Exception{
if(this.getText().length() > 1 ){
this.setOutputText(translate(getText()));
}
}
public void resetAll(ActionEvent event){
this.setText("");
this.setOutputText("");
}
public String translate(String input) throws Exception{
String out = "";
if(input != null){
out = Translate.translate(this.getText(),
getSelectedOriginalLanguage(),
getSelectedTranslatedLanguage());
invokeTextEffect();
}
return out;
}
public SelectItem[] getORIGINAL_LANGUAGES() {
if(ORIGINAL_LANGUAGES == null){
this.setLanguages();
this.setLangLabels();
Enumeration en = this.languages.keys();
Enumeration enLbls = this.langLabels.keys();
ORIGINAL_LANGUAGES = new
SelectItem[languages.size()];
for (int i = 0; i < languages.size(); i++) {
ORIGINAL_LANGUAGES[i] = new SelectItem(
(String)this.langLabels.get(enLbls.nextElement()),
(String) this.languages.get(en.nextElement()));
}
}
return ORIGINAL_LANGUAGES;
}
public SelectItem[] getTRANSLATION_LANGUAGES() {
if(TRANSLATION_LANGUAGES == null){
this.setLanguages();
this.setLangLabels();
Enumeration en = this.languages.keys();
Enumeration enLbls = this.langLabels.keys();
TRANSLATION_LANGUAGES = new
SelectItem[languages.size()];
for (int i = 0; i < languages.size(); i++) {
TRANSLATION_LANGUAGES[i] = new SelectItem(
(String) this.langLabels.get(enLbls.nextElement()),
(String) this.languages.get(en.nextElement()));
}
}
return TRANSLATION_LANGUAGES;
}
public void setLanguages() {
this.languages.put(Language.ARABIC, "ARABIC");
this.languages.put(Language.CHINESE_SIMPLIFIED,
"CHINESE_SIMPLIFIED");
this.languages.put(Language.CHINESE_TRADITIONAL,
"CHINESE_TRADITIONAL");
this.languages.put(Language.DUTCH, "DUTCH");
this.languages.put(Language.ENGLISH, "ENGLISH");
this.languages.put(Language.FRENCH, "FRENCH");
this.languages.put(Language.GERMAN, "GERMAN");
this.languages.put(Language.GREEK, "GREEK");
this.languages.put(Language.ITALIAN, "ITALIAN");
this.languages.put(Language.JAPANESE, "JAPANESE");
this.languages.put(Language.KOREAN, "KOREAN");
this.languages.put(Language.PORTUGESE, "PORTUGESE");
this.languages.put(Language.RUSSIAN, "RUSSIAN");
this.languages.put(Language.SPANISH, "SPANISH");
}
public void setLangLabels() {
this.langLabels.put(new Integer(0),
Language.PORTUGESE);
this.langLabels.put(new Integer(1),Language.KOREAN);
this.langLabels.put(new Integer(2),
Language.JAPANESE);
this.langLabels.put(new Integer(3), Language.SPANISH);
this.langLabels.put(new Integer(4), Language.GERMAN);
this.langLabels.put(new Integer(5), Language.FRENCH);
this.langLabels.put(new Integer(6), Language.ITALIAN);
this.langLabels.put(new Integer(7),
Language.CHINESE_SIMPLIFIED);
this.langLabels.put(new Integer(8), Language.ARABIC);
this.langLabels.put(new Integer(9), Language.RUSSIAN);
this.langLabels.put(new Integer(10),Language.GREEK);
this.langLabels.put(new Integer(11),
Language.ENGLISH);
this.langLabels.put(new Integer(12), Language.DUTCH);
this.langLabels.put(new Integer(13),
Language.CHINESE_TRADITIONAL);
}
}
}
- ››Google搜索引擎的奥秘
- ››Google测试搜索结果页面右侧内容更丰富的信息栏
- ››Google Dart精粹:应用构建,快照和隔离体
- ››google的代码审查
- ››google analytics清晰追踪爬虫的爬行信息
- ››Google+中文用户在两千万Google+大军中是少数派
- ››Google AdWords最昂贵点击成本的20种关键词分类
- ››Google运作经理Bryan Power给出的GOOGLE求职意见
- ››Google用户体验的十大设计原则
- ››Google Analytics(分析)能为网站带来什么
- ››Google goggles图片搜索 如何优化一个wap网站
- ››Google Docs将增加iPhone和Android编辑功能
更多精彩
赞助商链接