Last active
January 3, 2016 23:29
-
-
Save AvatarQing/8535604 to your computer and use it in GitHub Desktop.
以Http Post方式调用谷歌翻译接口。支持翻译文本文件。需要引入HttpClient4包。解决HTML特殊字符编码问题http://blog.csdn.net/lonfee88/article/details/8642584
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package translate; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map.Entry; | |
import java.util.Set; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class AndroidStringXmlParser { | |
/** 左分隔符 */ | |
public static final String DIVIDER_LEFT = "[[["; | |
/** 右分隔符 */ | |
public static final String DIVIDER_RIGHT = "]]]"; | |
/** 匹配诸如<string name = "xxx" >yyy</string >的字符串 */ | |
private static String mStringTagRegExp = "(<string\\s+name\\s*=\\s*\"\\w+\"\\s*>)([\\s\\S]*?)(</string\\s*>)"; | |
// private static String mDividerRegExp = | |
// "[\\(|(]{3}([\\s\\S]*?)[)|\\)]{3}"; | |
private static String mDividerRegExp = "\\[\\s*?\\[\\s*?\\[\\s*?([\\s\\S]*?)\\]\\s*\\]\\s*\\]\\s*?"; | |
/** | |
* 从string.xml中解析出<string>标记间的内容 | |
* | |
* @param xmlText | |
* string.xml的整个文本内容 | |
*/ | |
public static String parseTextFromStringTag(String xmlText) { | |
// 最终文本 | |
StringBuilder finalText = new StringBuilder(); | |
// 匹配正则表达式 | |
Matcher matcher = Pattern.compile(mStringTagRegExp).matcher(xmlText); | |
while (matcher.find()) { | |
// 截取<string></string>标记中间的内容 | |
finalText.append(DIVIDER_LEFT + matcher.group(2) + DIVIDER_RIGHT); | |
} | |
System.out.println(finalText.toString() + "\n" | |
+ "^^^^^^^^^^^^^^^^^^^^^^^^^"); | |
// 返回最终结果 | |
return finalText.toString(); | |
} | |
/** | |
* 按分隔符切割文本,生成为string.xml的格式内容 | |
* | |
* @param xmlText | |
* 原来的string.xml | |
* @param text | |
* 待切割的文本 | |
*/ | |
public static String convertTextToStringTag(String xmlText, String text) { | |
String finalText = null; | |
if (xmlText != null && text != null) { | |
// 临时变量声明 | |
int i = 0; | |
String tagStart = null; | |
String tagEnd = null; | |
finalText = new String(xmlText); | |
List<String> contents = new ArrayList<String>(); | |
System.out.println(text + "\n" + "!!!!!!!!!!!!!!!!!!!!"); | |
// 分割出各个string标签的内容 | |
// 匹配正则表达式 | |
Matcher matcherForDivider = Pattern.compile(mDividerRegExp) | |
.matcher(text); | |
while (matcherForDivider.find()) { | |
System.out.println(matcherForDivider.group(0) + "\n" | |
+ "============"); | |
contents.add(matcherForDivider.group(1)); | |
} | |
// 匹配正则表达式 | |
Matcher matcherForStringTag = Pattern.compile(mStringTagRegExp) | |
.matcher(xmlText); | |
while (matcherForStringTag.find() && i < contents.size()) { | |
// 替换<string></string>标记中间的内容 | |
tagStart = matcherForStringTag.group(1); | |
tagEnd = matcherForStringTag.group(3); | |
System.out.println(matcherForStringTag.group(0) + "\n" | |
+ tagStart + contents.get(i) + tagEnd + "\n" | |
+ "-------------"); | |
finalText = finalText.replaceFirst( | |
matcherForStringTag.group(0), | |
tagStart + contents.get(i) + tagEnd); | |
i++; | |
} | |
} | |
return finalText; | |
} | |
/** 翻译strings.xml各节点的内容,重新生成strings.xml */ | |
public static void translateStringXml(String originalLaunguage, | |
String destLaunguage, String destPath, String rawText, | |
String trimmedText) { | |
final String fileName = "strings.xml"; | |
final String subDirNamePattern = "values-%s"; | |
String subDirName = null; | |
String finalSaveDir = null; | |
// 生成目录名 | |
subDirName = String.format(subDirNamePattern, destLaunguage); | |
// 最终保存路径 | |
finalSaveDir = destPath + File.separator + subDirName + File.separator | |
+ fileName; | |
// 调用翻译接口 | |
String transText = GoogleTranslate.translate(trimmedText, | |
originalLaunguage, destLaunguage); | |
// 处理翻译后的文本,格式化为需要的格式 | |
String finalText = AndroidStringXmlParser.convertTextToStringTag( | |
rawText, transText); | |
// 将翻译出的文本保存到目标路径 | |
FileUtils.writeStringToFile(finalText, finalSaveDir); | |
} | |
/** 将指定文件文本翻译为多国语言 */ | |
public static void translateToMultipleLaunguage(String originalLaunguage, | |
String originalPath, String destPath) { | |
String destLaunguage = null; | |
// 读取源文本 | |
String rawText = FileUtils.readStringFromFile(originalPath); | |
// 截取所要翻译的片段 | |
String trimmedText = AndroidStringXmlParser | |
.parseTextFromStringTag(rawText); | |
// 获取所有语言的编码集合 | |
Set<Entry<String, String>> allLanguageEntries = Language.LANGUAGE | |
.entrySet(); | |
for (Entry<String, String> entry : allLanguageEntries) { | |
// 获取某一个语言编码代号 | |
destLaunguage = entry.getValue(); | |
translateStringXml(originalLaunguage, destLaunguage, destPath, | |
rawText, trimmedText); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package translate; | |
public class Decorder { | |
/** 将html特殊字符编码转化为正常显示的字符 */ | |
public static String encodeCesToChars(String paramStr) { | |
paramStr = paramStr.replace(" ", " ").replace("<", "<") | |
.replace(">", ">").replace("&", "&") | |
.replace(""", "\"").replace("'", "'"); | |
return new String(paramStr); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package translate; | |
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
public class FileUtils { | |
public static final String ENCODING = "utf-8"; | |
/** | |
* 从指定文件读出文本字符串 | |
* | |
* @param path | |
* 文件路径 | |
*/ | |
public static String readStringFromFile(String path) { | |
String result = null; | |
if (path != null) { | |
File file = new File(path); | |
if (file.exists()) { | |
BufferedReader br = null; | |
try { | |
br = new BufferedReader(new InputStreamReader( | |
new FileInputStream(file), ENCODING)); | |
String temp = null; | |
StringBuilder sb = new StringBuilder(); | |
while ((temp = br.readLine()) != null) { | |
sb.append(temp); | |
} | |
result = sb.toString(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
if (br != null) { | |
try { | |
br.close(); | |
System.out.println("Succeeded to read text from " | |
+ path); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
br = null; | |
} | |
} | |
} | |
} | |
} | |
return result; | |
} | |
/** | |
* 将文本字符串写到一个指定的文件中 | |
* | |
* @param text | |
* 欲写入的文本字符串 | |
* @param path | |
* 保存的文件路径 | |
* | |
*/ | |
public static void writeStringToFile(String text, String path) { | |
if (path != null) { | |
File file = new File(path); | |
if (file.getParentFile() != null) { | |
file.getParentFile().mkdirs(); | |
} | |
BufferedWriter bw = null; | |
try { | |
bw = new BufferedWriter(new OutputStreamWriter( | |
new FileOutputStream(file), ENCODING)); | |
bw.write(text); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
if (bw != null) { | |
try { | |
bw.close(); | |
System.out | |
.println("Succeeded to write text to " + path); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
bw = null; | |
System.out.println("Failed to write text to " + path); | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package translate; | |
/** | |
* 原文:http://ahomeeye.javaeye.com/blog/841298 | |
* 需要引入HttpClient4包 | |
* @author ahomeeye | |
* @modifer Avatar Qing | |
*/ | |
import java.net.URLEncoder; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.client.entity.UrlEncodedFormEntity; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.message.BasicNameValuePair; | |
import org.apache.http.protocol.HTTP; | |
import org.apache.http.util.EntityUtils; | |
public class GoogleTranslate { | |
private static final String UTF_8 = "UTF-8"; | |
private static HttpPost httppost; | |
private static HttpResponse response; | |
private static final DefaultHttpClient httpclient = new DefaultHttpClient(); | |
public static String encode(String value, String charset) { | |
try { | |
return URLEncoder.encode(value, charset); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
/* | |
* @param text 翻译原文 | |
* | |
* @param lanFrom 源语言 | |
* | |
* @param lanTo 目标语言 | |
* | |
* @return 翻译后的字符串 | |
*/ | |
public static String translate(String text, String lanFrom, String lanTo) { | |
List<NameValuePair> qparams = new ArrayList<NameValuePair>(); | |
qparams.add(new BasicNameValuePair("client", "t")); | |
qparams.add(new BasicNameValuePair("hl", "en")); | |
qparams.add(new BasicNameValuePair("ie", UTF_8)); | |
qparams.add(new BasicNameValuePair("oe", UTF_8)); | |
qparams.add(new BasicNameValuePair("sl", lanFrom)); | |
qparams.add(new BasicNameValuePair("text", text)); | |
qparams.add(new BasicNameValuePair("tl", lanTo)); | |
// HttpClient提交post请求 | |
httppost = new HttpPost("http://translate.google.com/translate_t#"); | |
String cookie = "Cookie PREF=ID=8daa1f767f10d1fe:U=f5ac701cf7d3f2e0:FF=0:LD=en:CR=2:TM=1277174286:LM=1289370601:S=q7yslRWEZs3uK1H8; NID=39=UO-TWo9HzzjHc-d_wYm7BVR1cH33KpqaN5h5877_i29nERA93FeG1GSuV3ZSvsOx8D-TnHKpB9m0KhZRH8U9uPwoE-arYd0bAyAlILyXZxLO2_TyGQhJpcMiOLVEuCpq; SID=DQAAAHoAAADMlGzeKhnGkbkIJ36tVO0ZPXgmQ6Cth7Oa6geyyE1WJooW8P01uKUHNrsRkjggvFMAWIWB9J5i18z0F6GjC_oV79mSwXEDGuRFGhRnDyJdid3ptjFW0pIyt4_2D6AMIqtOWF71aWdvY7IvAU1AWMNs8fBZHAOgRqtf3aCUkr36ZA; HSID=A6-YJTnhjBdFWukoR"; | |
httppost.addHeader("Cookie", cookie); | |
String responseBody = ""; | |
String content = ""; | |
try { | |
// 将参数封装到post数据包中 | |
httppost.setEntity(new UrlEncodedFormEntity(qparams, HTTP.UTF_8)); | |
response = httpclient.execute(httppost); | |
responseBody = EntityUtils.toString(response.getEntity()); | |
// 过滤出所需翻译后的内容 | |
int tmp1 = responseBody.indexOf("result_box"); | |
int tmp2 = responseBody.indexOf(">", tmp1); | |
int tmp3 = responseBody.indexOf("</div>", tmp2); | |
// 替换换行符和其他网页标签 | |
String htmlTagRegExp = "<[^>]*>"; | |
content = responseBody.substring(tmp2 + 1, tmp3) | |
.replaceAll("<br>", "\n").replaceAll(htmlTagRegExp, ""); | |
content = Decorder.encodeCesToChars(content); | |
} catch (Exception e) { | |
return content; | |
} finally { | |
httppost.abort(); | |
} | |
return content; | |
} | |
/* | |
* 重载上面的translate()方法,实现文本数组的翻译 | |
*/ | |
public static String[] translate(String[] text, String lanFrom, String lanTo) { | |
if (text == null || text.length < 1) { | |
return null; | |
} | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < text.length; i++) { | |
if (i < text.length - 1) { | |
sb.append(text[i]).append("BBaaBB"); | |
} else { | |
sb.append(text[i]); | |
} | |
} | |
return translate(sb.toString(), lanFrom, lanTo).split("BBaaBB"); | |
} | |
public static void main(String[] args) { | |
// testTranslateString(); | |
// testTranslateTextFile(args); | |
testTranslateAndroidStringXml(args); | |
} | |
private static void testTranslateAndroidStringXml(String[] args) { | |
// TEST | |
args = new String[] { "f:\\strings.xml", "f:\\test" }; | |
// 源语言 | |
String originalLaunguage = null; | |
// 目标语言 | |
// String destLaunguage = null; | |
String destLaunguage = "en"; | |
// 源文件路径 | |
String originalPath = args[0]; | |
// 目标文件路径 | |
String destPath = args[1]; | |
AndroidStringXmlParser.translateToMultipleLaunguage(originalLaunguage, | |
originalPath, destPath); | |
// TEST | |
// String rawText = FileUtils.readStringFromFile(originalPath); | |
// String trimmedText = AndroidStringXmlParser | |
// .parseTextFromStringTag(rawText); | |
// AndroidStringXmlParser.translateStringXml(originalLaunguage, | |
// destLaunguage, destPath, rawText, trimmedText); | |
} | |
private static void testTranslateTextFile(String[] args) { | |
// 源语言 | |
String originalLaunguage = args[0]; | |
// 目标语言 | |
String destLaunguage = args[1]; | |
// 源文件路径 | |
String originalPath = args[2]; | |
// 目标文件路径 | |
String destPath = args[3]; | |
// 原始文本 | |
String originalText = FileUtils.readStringFromFile(originalPath); | |
// 目标文本 | |
String destText = translate(originalText, originalLaunguage, | |
destLaunguage); | |
FileUtils.writeStringToFile(destText, destPath); | |
} | |
private static void testTranslateString() { | |
String text = "乐盟软件管家☆这是一张图片☆应用推荐"; | |
String tranText = GoogleTranslate.translate(text, "zh-CN", "ja"); | |
System.out.println("tranText=" + tranText); | |
// String[] arrayText = { "我来自广东梅州。", "我爱你,梅州!" }; | |
// String[] tranArray = GoogleTranslate | |
// .translate(arrayText, "zh-CN", "en"); | |
// for (int i = 0; i < tranArray.length; i++) { | |
// System.out.println("tranArray[" + i + "]=" + tranArray[i]); | |
// } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package translate; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class Language { | |
/** | |
* Key -> Value 语言 -> 单词表示 | |
*/ | |
public static final Map<String, String> LANGUAGE = new HashMap<String, String>(); | |
static { | |
LANGUAGE.put("阿尔巴尼亚语", "sq"); | |
LANGUAGE.put("阿拉伯语", "ar"); | |
LANGUAGE.put("阿塞拜疆语", "az"); | |
LANGUAGE.put("爱尔兰语", "ga"); | |
LANGUAGE.put("爱沙尼亚语", "et"); | |
LANGUAGE.put("巴斯克语", "eu"); | |
LANGUAGE.put("白俄罗斯语", "be"); | |
LANGUAGE.put("保加利亚语", "bg"); | |
LANGUAGE.put("冰岛语", "is"); | |
LANGUAGE.put("波兰语", "pl"); | |
LANGUAGE.put("波斯语", "fa"); | |
LANGUAGE.put("布尔语", "af"); | |
LANGUAGE.put("南非荷兰语", "af"); | |
LANGUAGE.put("丹麦语", "da"); | |
LANGUAGE.put("德语", "de"); | |
LANGUAGE.put("俄语", "ru"); | |
LANGUAGE.put("法语", "fr"); | |
LANGUAGE.put("菲律宾语", "tl"); | |
LANGUAGE.put("芬兰语", "fi"); | |
LANGUAGE.put("格鲁吉亚语", "ka"); | |
LANGUAGE.put("古吉拉特语", "gu"); | |
LANGUAGE.put("海地克里奥尔语", "ht"); | |
LANGUAGE.put("韩语", "ko"); | |
LANGUAGE.put("荷兰语", "nl"); | |
LANGUAGE.put("加利西亚语", "gl"); | |
LANGUAGE.put("加泰罗尼亚语", "ca"); | |
LANGUAGE.put("捷克语", "cs"); | |
LANGUAGE.put("卡纳达语", "kn"); | |
LANGUAGE.put("克罗地亚语", "hr"); | |
LANGUAGE.put("拉丁语", "la"); | |
LANGUAGE.put("拉脱维亚语", "lv"); | |
LANGUAGE.put("老挝语", "lo"); | |
LANGUAGE.put("立陶宛语", "lt"); | |
LANGUAGE.put("罗马尼亚语", "ro"); | |
LANGUAGE.put("马耳他语", "mt"); | |
LANGUAGE.put("马来语", "ms"); | |
LANGUAGE.put("马其顿语", "mk"); | |
LANGUAGE.put("孟加拉语", "bn"); | |
LANGUAGE.put("挪威语", "no"); | |
LANGUAGE.put("葡萄牙语", "pt"); | |
LANGUAGE.put("日语", "ja"); | |
LANGUAGE.put("瑞典语", "sv"); | |
LANGUAGE.put("塞尔维亚语", "sr"); | |
LANGUAGE.put("世界语", "eo"); | |
LANGUAGE.put("斯洛伐克语", "sk"); | |
LANGUAGE.put("斯洛文尼亚语", "sl"); | |
LANGUAGE.put("斯瓦希里语", "sw"); | |
LANGUAGE.put("泰卢固语", "te"); | |
LANGUAGE.put("泰米尔语", "ta"); | |
LANGUAGE.put("泰语", "th"); | |
LANGUAGE.put("土耳其语", "tr"); | |
LANGUAGE.put("威尔士语", "cy"); | |
LANGUAGE.put("乌尔都语", "ur"); | |
LANGUAGE.put("乌克兰语", "uk"); | |
LANGUAGE.put("希伯来语", "iw"); | |
LANGUAGE.put("希腊语", "el"); | |
LANGUAGE.put("西班牙语", "es"); | |
LANGUAGE.put("匈牙利语", "hu"); | |
LANGUAGE.put("亚美尼亚语", "hy"); | |
LANGUAGE.put("意大利语", "it"); | |
LANGUAGE.put("意第绪语", "yi"); | |
LANGUAGE.put("印地语", "hi"); | |
LANGUAGE.put("印尼语", "id"); | |
LANGUAGE.put("英语", "en"); | |
LANGUAGE.put("越南语", "vi"); | |
LANGUAGE.put("中文繁体", "zh-TW"); | |
LANGUAGE.put("中文简体", "zh-CN"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment