通过网上一位仁兄的实例我 最近练习了一个在线翻译的项目下面把主要代码贴下
多谢那位仁兄
源地址和目标地址
package com.srcAndDest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* 源地址和目标地址
* @author wdw
*
*/
public class SrcAndDest {
public List<HashMap<String,Object>> list=null;
public List<HashMap<String,Object>> getSrcAndDest(){
list=new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("enc", "zh-CN");
map.put("text", "中文(简体)");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "zh-TW");
map.put("text", "中文(繁体)");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "en");
map.put("text", "英语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "sq");
map.put("text", "阿尔巴尼亚人");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "am");
map.put("text", "阿姆哈拉语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "hy");
map.put("text", "亚美尼亚");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "az");
map.put("text", "阿塞拜疆");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "be");
map.put("text", "白俄罗斯");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "bn");
map.put("text", "孟加拉");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "bh");
map.put("text", "比哈里");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "bg");
map.put("text", "保加利亚语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "my");
map.put("text", "缅甸语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "ca");
map.put("text", "加泰罗尼亚");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "chr");
map.put("text", "切诺基");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "hr");
map.put("text", "克罗地亚");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "cs");
map.put("text", "捷克");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "da");
map.put("text", "丹麦");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "nl");
map.put("text", "荷兰");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "et");
map.put("text", "爱沙尼亚语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "tl");
map.put("text", "菲律宾");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "fi");
map.put("text", "芬兰");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "fr");
map.put("text", "法语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "gl");
map.put("text", "加利西亚");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "ka");
map.put("text", "格鲁吉亚");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "de");
map.put("text", "德语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "el");
map.put("text", "希腊语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "iw");
map.put("text", "希伯来");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "hi");
map.put("text", "印地文");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "hu");
map.put("text", "匈牙利");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "is");
map.put("text", "冰岛");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "id");
map.put("text", "印度尼西亚");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "iu");
map.put("text", "因纽特语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "it");
map.put("text", "意大利");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "ja");
map.put("text", "日文");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "kn");
map.put("text", "卡纳达");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "km");
map.put("text", "高棉");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "ko");
map.put("text", "韩国");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "ku");
map.put("text", "库尔德");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "ky");
map.put("text", "吉尔吉斯");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "lo");
map.put("text", "老挝语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "lv");
map.put("text", "拉脱维亚");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "lt");
map.put("text", "立陶宛");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "mk");
map.put("text", "马其顿");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "ms");
map.put("text", "马来");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "ml");
map.put("text", "马来亚");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "mt");
map.put("text", "马耳他语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "mr");
map.put("text", "马拉");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "mn");
map.put("text", "蒙古族");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "ne");
map.put("text", "尼泊尔");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "no");
map.put("text", "挪威的");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "or");
map.put("text", "奥里雅");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "ps");
map.put("text", "普什图语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "fa");
map.put("text", "波斯湾");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "pl");
map.put("text", "波兰语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "pt-PT");
map.put("text", "葡");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "pa");
map.put("text", "旁遮普语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "ro");
map.put("text", "罗马尼亚");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "ru");
map.put("text", "俄罗斯");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "sa");
map.put("text", "梵语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "sr");
map.put("text", "塞尔维亚");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "sd");
map.put("text", "信德语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "si");
map.put("text", "僧伽罗人");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "sk");
map.put("text", "斯洛伐克");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "sl");
map.put("text", "斯洛文尼亚");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "es");
map.put("text", "西班牙");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "sw");
map.put("text", "斯瓦希里");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "sv");
map.put("text", "瑞典语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "tg");
map.put("text", "塔吉克");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "ta");
map.put("text", "泰米尔人");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "tl");
map.put("text", "他加禄语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "te");
map.put("text", "泰卢固");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "th");
map.put("text", "泰国");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "bo");
map.put("text", "西藏");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "tr");
map.put("text", "土耳其语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "uk");
map.put("text", "乌克兰");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "ur");
map.put("text", "乌尔都语");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "uz");
map.put("text", "乌孜别克");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "ug");
map.put("text", "维吾尔");
list.add(map);
map = new HashMap<String,Object>();
map.put("enc", "vi");
map.put("text", "越南人");
list.add(map);
return list;
}
}
activity的主要代码
public void onClick(View v)
{
if (v.getId()==interCha.getId())
{
//自定义dialog
LayoutInflater flater=this.getLayoutInflater();
View view = flater.inflate(R.layout.alertview, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);
builder.setTitle("languageCode设置");
spsrc=(Spinner)view.findViewById(R.id.spsrc);
spdest=(Spinner)view.findViewById(R.id.spdest);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,altext);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spsrc.setAdapter(adapter);
spsrc.setSelection(srcIndex);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,altext);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spdest.setAdapter(adapter);
spdest.setSelection(destIndex);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
srcIndex = spsrc.getSelectedItemPosition();
destIndex = spdest.getSelectedItemPosition();
//先保存后获取设置
putSetting(srcIndex,destIndex);
getSetting();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
builder.create().show();
}
else if (v.getId()==interButton.getId())
{
tileView.setVisibility(TextView.VISIBLE);
myWView.setVisibility(WebView.VISIBLE);
myHandler.post(new Runnable(){
public void run() {
String textString = myEText.getText().toString();
if (!textString.equals(""))
{
myWView.loadUrl("javascript:translate('"+
textString + "','"+src+"','"+dest+"')");
}
}
});
}
else if (v.getId()==readBt.getId())
{
mSpeech.speak(readEt.getText().toString(),
TextToSpeech.QUEUE_FLUSH, null);
}
else {
//交换目标地址和源地址
this.putSetting(destIndex, srcIndex);
this.getSetting();
}
}
/*
* 初始化上一次src和dest
*/
private void getSetting(){
SharedPreferences settings=getPreferences(MODE_PRIVATE);
srcIndex=settings.getInt("srcIndex", 0);
destIndex=settings.getInt("destIndex", 0);
//得到src和dest语言LanguageCode(例如:简体中文:zh-cn,繁体中文:zh-tw)
src=alenc.get(srcIndex);//alenc一个ArrayList<String>对象,保存enc
dest=alenc.get(destIndex);
stext=altext.get(srcIndex);//altext一个ArrayList<String>对象,保存text
dtext=altext.get(destIndex);
String text=stext+">>"+dtext;
if(srcIndex!=destIndex){
interCha.setText(text);
}else{
if(srcIndex==0){
interCha.setText("翻译设置");
}else{
interCha.setText(text);
}
}
}
private void putSetting(int srcindex,int destindex)
{
SharedPreferences setting = this.getPreferences(MODE_PRIVATE);
Editor editor = setting.edit();
editor.putInt("srcIndex", srcindex);
editor.putInt("destIndex", destindex);
editor.commit();
}
public void onDestroy(){
this.putSetting(srcIndex, destIndex);
super.onDestroy();
}
//检查网络是否连接
public boolean checkIntent(){
ConnectivityManager mannager=(ConnectivityManager)
this.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info=mannager.getActiveNetworkInfo();
if(info==null || !info.isConnected()){
return false;
}
if(info.isRoaming()){
return true;
}
return true;
}
JavaScript代码
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("language", "1");
function g_translate(strInput,src,dest,out){
google.language.translate(strInput, src, dest, function(result){
if (!result.error){
document.getElementById(out).innerHTML=result.translation;
}else
document.getElementById(out).innerHTML="google_translate Error!";
});
}
</script>
<body style="background-color:#fff">
<div id="show" style="font-size:16px; color:#666;"></div>
<script language="javascript">
function translate(strTranslate,src,dest)
{
document.getElementById("show").innerHTML="翻译中.....";
g_translate(strTranslate,src,dest,"show");
}
</script>
</body>
</html>