MTrans Project Introduction
MTrans, also known as Multi-source Translation, is a lightweight service that combines various mainstream online translation and text-to-speech (TTS) features. It is an open-source project available on GitHub for developers who can freely modify and further develop the project. This project enables users to send HTTP requests to supported online translation servers to retrieve and parse results conveniently.
Supported Sources and Languages
MTrans currently supports multiple translation sources and languages. Here is an overview:
- Baidu Translate: Mandarin, English, Japanese, Korean, French, Russian, German.
- Youdao Translate: Mandarin, English, Japanese, Korean, French, Russian.
- Google Translate: Mandarin, English, Japanese, Korean, French, Russian, German.
- Tencent Translate: Mandarin, English, Japanese, Korean, French, Russian, German.
- Omi Translate: Mandarin, English.
- TryCan: Mandarin, English.
- Iciba: Mandarin, English, Japanese, Korean, French, German.
- Sogou Translate: Mandarin, English, Japanese, Korean, French, Russian, German.
Additionally, MTrans provides TTS services from providers such as Baidu, Youdao, Google, Tencent, and Sogou, supporting languages like Mandarin, English, Japanese, Korean, French, Russian, German, and Thai.
Quick Start
Environment Setup
MTrans is developed using IDEA and Maven. To set it up, include the following dependencies in your pom.xml
file:
<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>
Sample Code
Here is a minimal example of how to use the translation feature:
public class Test {
public static void main(String[] args) {
Querier<AbstractTranslator> querierTrans = new Querier<>();
querierTrans.setParams(LANG.ZH, LANG.EN, "如果这都不算爱,我有什么好悲哀!");
querierTrans.attach(new GoogleTranslator());
List<String> result = querierTrans.execute();
for (String str : result) {
System.out.println(str);
}
}
}
For TTS, you can follow a similar approach:
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());
List<String> result = querierTTS.execute();
for (String str : result) {
System.out.println(str);
}
}
}
MTrans Structure
Package Overview
MTrans organizes its functionality into several packages, each serving specific purposes:
- com.swjtu.lang: Contains language enumeration.
- com.swjtu.util: Provides utility methods.
- com.swjtu.http: Includes HTTP method interfaces and abstract classes.
- com.swjtu.querier: Generic querier class for handling requests.
- com.swjtu.trans: Abstract translator class.
- com.swjtu.trans.impl: Real implementations for Baidu, Google, Youdao, and others.
- com.swjtu.tts: Abstract TTS class.
- com.swjtu.tts.impl: Real implementations for various TTS providers.
How to Extend?
MTrans supports mainstream online translations and TTS services, which can be easily expanded. Developers can extend functionality by understanding the source code and HTTP protocols.
Extending Languages
To add a new language, follow these steps:
- Determine the server-supported language and its code.
- Add the language code to the
LANG
enumeration. - Map the language in
setLangSupport()
method in the respective translator implementation class.
For instance, to add Spanish to the Youdao translator:
- Identify the code:
es
- Add
SPA
toLANG
:public enum LANG { ZH, EN, JP, KOR, FRA, SPA, ... }
- Modify
YoudaoTranslator
:langMap.put(LANG.SPA, "es");
MTrans provides a versatile and developer-friendly platform for leveraging various online translation and TTS services in a single unified interface.