MTrans
Multi-source Translation
Install / Use
/learn @hujingshuang/MTransREADME
MTrans 项目介绍
本项目 多源翻译 (Multi-source Translation, MTrans),提供了集多种主流的 在线翻译 及 TTS 功能于一身的轻量级服务。通过程序向所支持的在线目标服务器发送 HTTP 请求,获取并解析返回的结果,为使用者提供便利。目前,本项目免费开源,开发者可基于此进行二次开发。
目前支持 源 及 语种 如下:
| 翻译源 | 服务器地址 | 支持语种 | 方式 | :-: | :-: | :-: | :-: | | 百度翻译 | http://fanyi.baidu.com/v2transapi | 中文、英语、日语、韩语、法语、俄语、德语 | 互译 | 有道翻译 | http://fanyi.youdao.com/translate_o | 中文、英语、日语、韩语、法语、俄语 | 互译 | 谷歌翻译 | https://translate.google.cn/translate_a/single | 中文、英语、日语、韩语、法语、俄语、德语 | 互译 | 腾讯翻译君 | http://fanyi.qq.com/api/translate | 中文、英语、日语、韩语、法语、俄语、德语 | 互译 | 欧米翻译 | http://www.omifanyi.com/transSents.do | 中文、英语 | 互译 | TryCan | http://fanyi.trycan.com/Transfer.do | 中文、英语 | 互译 | 金山爱词霸 | http://fy.iciba.com/ajax.php?a=fy | 中文、英语、日语、韩语、法语、德语 | 互译 | 搜狗翻译 | http://fanyi.sogou.com/reventondc/translate | 中文、英语、日语、韩语、法语、俄语、德语 | 互译
| TTS 源 | 服务器地址 | 支持语种 | :-: | :-: | :-: | | 百度 TTS | http://fanyi.baidu.com/gettts | 中文、英语、日语、韩语、法语、俄语、德语、泰语 | 有道 TTS | http://tts.youdao.com/fanyivoice | 英语、日语、韩语、法语 | 谷歌 TTS | https://translate.google.cn/translate_tts | 中文、英语、日语、韩语、法语、俄语、德语 | 腾讯 TTS | http://audiodetect.browser.qq.com:8080/tts | 中文、英语、日语、韩语 | 搜狗 TTS | http://fanyi.sogou.com/reventondc/synthesis | 中文、英语
一、快速开始
1、环境配置
本项目使用 IDEA + Maven 进行开发,请在 pom.xml 中添加如下依赖。
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
2、最小实例
- 最小翻译实例
import com.swjtu.lang.LANG; import com.swjtu.querier.Querier; import com.swjtu.trans.AbstractTranslator; import com.swjtu.trans.impl.GoogleTranslator; import java.util.List; public class Test { public static void main(String[] args) { Querier<AbstractTranslator> querierTrans = new Querier<>(); // 获取查询器 querierTrans.setParams(LANG.ZH, LANG.EN, "如果这都不算爱,我有什么好悲哀!"); // 设置参数 querierTrans.attach(new GoogleTranslator()); // 向查询器中添加 Google 翻译器 List<String> result = querierTrans.execute(); // 执行查询并接收查询结果 for (String str : result) { System.out.println(str); } } } - 最小 TTS 实例
import com.swjtu.lang.LANG; import com.swjtu.querier.Querier; import com.swjtu.tts.AbstractTTS; import com.swjtu.tts.impl.BaiduTTS; import java.util.List; public class Test { public static void main(String[] args) { Querier<AbstractTTS> querierTTS = new Querier<>(); // 获取查询器 querierTTS.setParams(LANG.EN, "To be or not to be, that is a question."); // 设置参数 querierTTS.attach(new BaiduTTS()); // 向查询器中添加 Google 翻译器 List<String> result = querierTTS.execute(); // 执行查询并接收查询结果 for (String str : result) { System.out.println(str); } } }
二、MTrans 使用说明
1、包/类 一览表
本项目中主要定义了如下几个包,其命名及作用如下表:
| 包名 | 包含类 | 说明 | :- | :-: | :-: | | com.swjtu.lang | LANG | 枚举类型,支持的语种列表 | com.swjtu.util | Util | 工具包 | com.swjtu.http | HttpParams、AbstractHttpAttribute | HTTP 方法接口及抽象类 | com.swjtu.querier | Querier | 泛型,查询器 | com.swjtu.trans | AbstractTranslator | 翻译器(抽象)类 | com.swjtu.trans.impl | BaiduTranslator、GoogleTranslator、YoudaoTranslator、IcibaTranslator、<br>OmiTranslator、SogouTranslator、TencentTranslator、TrycanTranslator | 翻译器实体类 | com.swjtu.tts | AbstractTTS | TTS 抽象类 | com.swjtu.tts.impl | BaiduTTS、YoudaoTTS、GoogleTTS、TencentTTS、SogouTTS | TTS 实体类
2、类图
-

-
com.swjtu.http 包 / 类图

-
com.swjtu.querier 包 / 类图

-
com.swjtu.trans 包 / 类图

-
com.swjtu.trans.impl 包 / 类图

-
com.swjtu.tts 包 / 类图

-
com.swjtu.tts.impl 包 / 类图

-
com.swjtu.util 包 / 类图

3、类说明
-
LANG枚举:定义所支持或将支持的语种,统一并规范了语种列表。public enum LANG { ZH, // 中文 EN, // 英语 JP, // 日语 JPKA, // 日语假名 TH, // 泰语 ... } -
Util类:包含并实现了一些实用方法。public static List<NameValuePair> map2list(Map<String, String> mapParams); // 将 Map 转换成 List public static String getUrlWithQueryString(String url, Map<String, String> params); // 生成 URL // 各种格式的 MD5 public static String md5(String input); public static String md5(File file); public static String md5(InputStream in); -
Querier类:定义了Querier类,使用了观察者模式。该类包含了一个集合,集合中的元素为翻译器类 或 TTS 类,通过setParams()设定好参数后,执行execute()方法发送请求,同时返回结果。可以通过attach()和detach()方法向集合中添加或移除元素。public final class Querier<T extends AbstractHttpAttribute> { private List<T> collection; // 集合 ... public void setParams(LANG source, String text); // TTS 参数设置, source 源语种,text 待转换为语音的内容 public void setParams(LANG from, LANG to, String text); // 翻译器参数设置,from 源语种,to 目标语种,text 待翻译内容 public List<String> execute() { List<String> result = new ArrayList<String>
