-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Baidu REST client and GUI frontend
- Loading branch information
Showing
20 changed files
with
624 additions
and
173 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,33 @@ | ||
minecraft_version=1.16.2 | ||
forge_version=1.16.2-33.0.5 | ||
# | ||
# Copyright (C) 2021 Ringosham | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
|
||
minecraft_version=1.16.4 | ||
forge_version=1.16.4-35.1.4 | ||
mappings_channel=snapshot | ||
mappings_version=20200723-1.16.1 | ||
mappings_version=20201028-1.16.3 | ||
mod_version=5.1.1 | ||
# suppress inspection "UnusedProperty" | ||
mod_id=translationmod | ||
mod_name=Real time translation mod | ||
mod_vendor=Ringosham | ||
group_name=com.ringosham.translationmod | ||
# Gradle import fix | ||
# suppress inspection "UnusedProperty" | ||
org.gradle.daemon=false | ||
org.gradle.daemon=false | ||
# suppress inspection "UnusedProperty" | ||
# Gradle needs more memory | ||
org.gradle.jvmargs=-Xmx3G |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,23 @@ | ||
# | ||
# Copyright (C) 2021 Ringosham | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
|
||
#Sat Sep 14 01:19:13 BST 2019 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-all.zip |
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
98 changes: 98 additions & 0 deletions
98
src/main/java/com/ringosham/translationmod/client/BaiduClient.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright (C) 2021 Ringosham | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.ringosham.translationmod.client; | ||
|
||
import com.ringosham.translationmod.client.types.Language; | ||
import com.ringosham.translationmod.client.types.RequestResult; | ||
import com.ringosham.translationmod.common.ConfigManager; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.net.URLEncoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class BaiduClient extends RESTClient { | ||
|
||
private static final char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; | ||
|
||
public BaiduClient() { | ||
super("https://fanyi-api.baidu.com/api/trans/vip/translate"); | ||
} | ||
|
||
@Override | ||
public RequestResult translate(String message, Language from, Language to) { | ||
Map<String, String> queryParam = new HashMap<>(); | ||
String encodedMessage = null; | ||
//Percent encode message | ||
try { | ||
encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8.name()); | ||
} catch (UnsupportedEncodingException ignored) { | ||
} | ||
//Query message | ||
queryParam.put("q", encodedMessage); | ||
//From language | ||
queryParam.put("to", to.getBaiduCode()); | ||
//To language | ||
queryParam.put("from", from.getBaiduCode()); | ||
//App ID | ||
queryParam.put("appid", ConfigManager.config.baiduAppId.get()); | ||
//Salt | ||
String salt = String.valueOf(System.currentTimeMillis()); | ||
queryParam.put("salt", salt); | ||
//Signature | ||
//Must use unencoded message for signature | ||
queryParam.put("sign", sign(ConfigManager.config.baiduAppId.get(), message, salt, ConfigManager.config.baiduKey.get())); | ||
Response response = POST(queryParam, "application/x-www-form-urlencoded"); | ||
if (response.getResponseCode() == 200) { | ||
//Baidu does not follow HTTP codes at all. It's 200 regardless of success or failure | ||
//wrrrrryyyyyyyyyy | ||
} else { | ||
//Most likely internal server error | ||
} | ||
//TODO Process response | ||
return null; | ||
} | ||
|
||
private String sign(String appid, String q, String salt, String key) { | ||
//Credits to Baidu's example | ||
String rawSignString = appid + q + salt + key; | ||
try { | ||
MessageDigest digest = MessageDigest.getInstance("MD5"); | ||
byte[] rawSignBytes = rawSignString.getBytes(StandardCharsets.UTF_8); | ||
byte[] signBytes = digest.digest(rawSignBytes); | ||
//Turn it to a hex string | ||
char[] signChars = new char[signBytes.length * 2]; | ||
int index = 0; | ||
for (byte b : signBytes) { | ||
//Turn MSB to hex | ||
//Bytes in Java are signed. Unsigned shift by 4 to get MSB | ||
//(Need to AND 0xf because the shift converted the byte back to int) | ||
signChars[index++] = hexDigits[b >>> 4 & 0xf]; | ||
//Turn LSB to hex | ||
//Bitwise AND to eliminate MSB | ||
signChars[index++] = hexDigits[b & 0xf]; | ||
} | ||
return new String(signChars); | ||
} catch (NoSuchAlgorithmException ignored) { | ||
} | ||
return null; | ||
} | ||
} |
Oops, something went wrong.