Multi-source Translation (MTrans) of this project provides a lightweight service that integrates a variety of mainstream online translation and TTS functions. The program sends HTTP requests to the supported online target servers, obtains and parses the returned results, providing convenience to users. At present, this project is free and open source , and developers can conduct secondary development based on this.
Currently the support sources and languages are as follows:
| Translation source | Server address | Support languages | Way |
|---|---|---|---|
| Baidu Translation | http://fanyi.baidu.com/v2transapi | Chinese, English, Japanese, Korean, French, Russian, German | Translation |
| Youdao Translation | http://fanyi.youdao.com/translate_o | Chinese, English, Japanese, Korean, French, Russian | Translation |
| Google Translate | https://translate.google.cn/translate_a/single | Chinese, English, Japanese, Korean, French, Russian, German | Translation |
| Tencent Translator | http://fanyi.qq.com/api/translate | Chinese, English, Japanese, Korean, French, Russian, German | Translation |
| Ome Translation | http://www.omianyi.com/transSents.do | Chinese, English | Translation |
| TryCan | http://fanyi.trycan.com/Transfer.do | Chinese, English | Translation |
| Kingsoft Love Ci Domineer | http://fy.iciba.com/ajax.php?a=fy | Chinese, English, Japanese, Korean, French, German | Translation |
| Sogou Translation | http://fanyi.sogou.com/reventondc/translate | Chinese, English, Japanese, Korean, French, Russian, German | Translation |
| TTS Source | Server address | Support languages |
|---|---|---|
| Baidu TTS | http://fanyi.baidu.com/gettts | Chinese, English, Japanese, Korean, French, Russian, German, Thai |
| Youdao TTS | http://tts.youdao.com/fanyivoice | English, Japanese, Korean, French |
| Google TTS | https://translate.google.cn/translate_tts | Chinese, English, Japanese, Korean, French, Russian, German |
| Tencent TTS | http://audiodetect.browser.qq.com:8080/tts | Chinese, English, Japanese, Korean |
| Sogou TTS | http://fanyi.sogou.com/reventondc/syncthesis | Chinese, English |
This project is developed using IDEA + Maven . Please add the following dependencies in 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 > 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 );
}
}
} 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 );
}
}
}This project mainly defines the following packages, and their naming and functions are as follows:
| Package name | Include classes | illustrate |
|---|---|---|
| com.swjtu.lang | LANG | Enumeration type, list of supported languages |
| com.swjtu.util | Util | Toolkit |
| com.swjtu.http | HttpParams, AbstractHttpAttribute | HTTP method interface and abstract classes |
| com.swjtu.querier | Querier | Generics, queryer |
| com.swjtu.trans | AbstractTranslator | Translator (Abstract) Class |
| com.swjtu.trans.impl | BaiduTranslator, GoogleTranslator, YoudaoTranslator, IcibaTranslator, OmiTranslator, SogouTranslator, TencentTranslator, TrycanTranslator | Translator Entity Class |
| com.swjtu.tts | AbstractTTS | TTS Abstract Class |
| com.swjtu.tts.impl | BaiduTTS, YoudaoTTS, GoogleTTS, TencentTTS, SogouTTS | TTS Entity Class |
Project structure

com.swjtu.http package/class diagram

com.swjtu.querier package/class diagram

com.swjtu.trans package/class diagram

com.swjtu.trans.impl package/class diagram

com.swjtu.tts package/class diagram

com.swjtu.tts.impl package/class diagram

com.swjtu.util package/class diagram

LANG enumeration: defines the supported or supported languages, and uniformly standardizes the language list.
public enum LANG {
ZH , // 中文
EN , // 英语
JP , // 日语
JPKA , // 日语假名
TH , // 泰语
...
} Util class: contains and implements some practical methods.
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 class: defines Querier class and uses the observer pattern. This class contains a collection, the elements in the collection are translator class or TTS class. After setting parameters through setParams() , execute execute() method to send the request and return the result. Elements can be added or removed to the collection through attach() and detach() methods.
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 >();
for ( T element : collection ) {
if ( element . getClass (). getName (). contains ( "Translator" )) {
result . add ( element . run ( from , to , text ));
} else if ( element . getClass (). getName (). contains ( "TTS" )) {
result . add ( element . run ( from , text ));
}
}
return result ;
}
public void attach ( T element );
public void detach ( T element );
...
} HttpParams interface: defines the interface method for setting HTTP data format
public interface HttpParams {
public void setFormData ( LANG source , String text ); // 设置 TTS 参数的接口方法
public void setFormData ( LANG from , LANG to , String text ); // 设置翻译器参数的接口方法
} AbstractHttpAttribute class: request and control process related to HTTP requests
public abstract String query () throws Exception ;
public abstract String run ( LANG source , String text );
public abstract String run ( LANG from , LANG to , String text );
// 资源释放
public void close ( HttpEntity httpEntity , CloseableHttpResponse httpResponse );
public void close (); AbstractTranslator class: inherits from AbstractHttpAttribute class, implements the HttpParams interface, and defines abstract translator classes.
@ Override
public String run ( LANG from , LANG to , String text ) {
String result = "" ;
setFormData ( from , to , text );
try {
result = parses ( query ());
} catch ( Exception e ) {
e . printStackTrace ();
}
close ();
return result ;
}
public abstract void setLangSupport (); // 设置支持的语种
public abstract String parses ( String text ) throws IOException ; // 解析返回结果AbstractTTS class: inherits from AbstractHttpAttribute class, implements the HttpParams interface, and defines the abstract TTS class.
@ Override
public String run ( LANG source , String text ) {
String saveFile = null ;
setFormData ( source , text );
try {
saveFile = query ();
System . out . println ( saveFile );
} catch ( IOException e ) {
e . printStackTrace ();
}
close ();
return saveFile ;
}
public String query () throws IOException {
...
// 将 TTS 结果保存为 mp3 音频文件,以待转换文本的 md5 码作为部分文件名
StringBuilder saveFile = new StringBuilder ();
saveFile . append ( "./tts/" )
. append ( this . getClass (). getName ())
. append ( "-" )
. append ( Util . md5 ( uri ))
. append ( ".mp3" );
...
} This project encapsulates several methods and exposes 5 API methods through Querier generic class. It is very simple and easy to use. See the example for details:
// 设置查询器参数
public void setParams ( LANG source , String text );
public void setParams ( LANG from , LANG to , String text );
public List < String > execute (); // 执行查询并返回结果
public void attach ( T element ); // 向查询器中添加元素
public void detach ( T element ); // 移除查询器中的元素This project supports and provides mainstream online translation and TTS services, and can easily perform related tasks through the provided API interface. At the same time, considering the potential needs of users, we will introduce how to expand based on this project to achieve the purpose of secondary development. Before expanding this project, users need to have a certain understanding of the project source code and HTTP knowledge.
The enumeration of LANG in the project code defines most commonly used languages. If the supported language cannot meet the user's needs, the user can expand it by himself.
setLangSupport() method, add the code map to the langMap variable;For example: For example, if you add Spanish support to Youdao translator:
Step 1: By querying the list of languages supported by Youdao Translation Server, it can be seen that Spanish language is supported and its code name is: es
Step 2: In LANG , add language custom code:
public enum LANG {
ZH , // 中文
EN , // 英语
JP , // 日语
JPKA , // 日语假名
TH , // 泰语
FRA , // 法语
SPA , // 西班牙语 <--- 添加语种(自定义语种代号)
KOR , // 韩语
....
} Step 3: In the YoudaoTranslator class, add the code map:
@ Override
public void setLangSupport () {
langMap . put ( LANG . ZH , "zh-CHS" );
langMap . put ( LANG . EN , "en" );
langMap . put ( LANG . JP , "ja" );
langMap . put ( LANG . KOR , "ko" );
langMap . put ( LANG . FRA , "fr" );
langMap . put ( LANG . RU , "ru" );
langMap . put ( LANG . SPA , "es" ); // 添加代号映射
} Developers define their own translator class by inheriting the AbstractTranslator class and implement the following abstract methods in the class:
// 添加语种支持
public abstract void setLangSupport ();
// 用于设置请求参数
public abstract void setFormData ( LANG from , LANG to , String text );
// 发送 HTTP 请求并接收返回结果(通常为 JSON 或 XML 字符串,根据用户请求结果而定)
public abstract String query () throws Exception ;
// 解析字符串,提取翻译结果
public abstract String parses ( String text ) throws IOException ;Note: For some HTTP requests that require setting cookies, please obtain and set the cookies before making the request. Usually, press F12 in the Chrome browser and enter:
document.cookiein the Console console to view it.
Developers define their own TTS class by inheriting the AbstractTTS class and implement the following abstract methods in the class:
// 添加语种支持
public abstract void setLangSupport ();
// 用于设置请求参数
public abstract void setFormData ( LANG source , String text );
// 发送 HTTP 请求并接收返回结果(通常为 JSON 或 XML 字符串,根据用户请求结果而定)
public abstract String query () throws Exception ;Note: The default TTS save path is: ./tts/class name -md5 (content to be converted).mp3
(如:com.swjtu.tts.impl.GoogleTTS-5757a2c16ce52b5427eb12f961d6362e.mp3)