#雜談 #Java腳本 因本人英語不好在使用Obsidian時,一些插件的設置英文多令人頭痛。故有寫一個的翻譯插件介紹和設置腳本的想法。看到有些前人寫的一下翻譯方法,簡直慘目忍睹。竟然要手動。這個應該寫好到只需要一鍵就可以漢化的地步嗎? 好吧。我承認這有些難度。翻譯引擎就用有道的吧。我覺得它對專業名 ...
雜談 #Java腳本
因本人英語不好在使用Obsidian時,一些插件的設置英文多令人頭痛。故有寫一個的翻譯插件介紹和設置腳本的想法。看到有些前人寫的一下翻譯方法,簡直慘目忍睹。竟然要手動。這個應該寫好到只需要一鍵就可以漢化的地步嗎?
好吧。我承認這有些難度。翻譯引擎就用有道的吧。我覺得它對專業名詞的翻譯準確度還是很高的。
- 提取main.js中需要的詞句
- 使用有道API來翻譯並生成對應的文件
- 使用Quicker的插件一鍵替換
這裡不想詳細寫過程了,直接貼代碼吧。以後有空再整合。
main.js處理代碼:(用了FastJson裡面的工具,需要導入)
import com.alibaba.fastjson.JSON;
import java.io.*;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author ShuangLian
* @date 2022/7/4 1:07
*/
public class test {
public static void main(String[] args) throws IOException {
// 需要翻譯的插件對應的main.js文件
File file = new File("C:\\Users\\91324\\Documents\\Projects\\IdeaProjects\\Test-demo\\src\\test\\java\\cn\\lian\\main.js");
System.out.println(file.getAbsolutePath());
FileInputStream stream = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(stream);
BufferedReader bufferedReader = new BufferedReader(reader);
LinkedList<String> list = new LinkedList<>();
String line;
while ((line = bufferedReader.readLine()) != null) {
// 匹配要翻譯的部分
String s_setName = "setName\\([^\\)]+\\)";
String s_addOption = "addOption\\([^\\)]+\\)";
String s_setDesc = "setDesc\\([^\\)]+\\)*\"\\)";
String s_name = "name: \".*\",";
// 使用正則查找匹配
List<String> linkedList;
if ((linkedList = find(s_setName, line)).size() != 0) {
for (String s : linkedList) {
list.add(s.substring(9, s.length() - 2));
}
}
if ((linkedList = find(s_setDesc, line)).size() != 0) {
for (String s : linkedList) {
list.add(s.substring(9, s.length() - 2));
}
}
if ((linkedList = find(s_addOption, line)).size() != 0) {
for (String ss : linkedList) {
String substring = ss.substring(10, ss.length() - 1);
String[] split = substring.split(",");
for (String s : split) {
s = s.strip();
list.add(s.substring(1, s.length() - 1));
}
}
}
if ((linkedList = find(s_name, line)).size() != 0) {
for (String s : linkedList) {
list.add(s.substring(7, s.length() - 2));
}
}
}
System.out.println(list);
bufferedReader.close();
// 輸出漢英對照.txt
File file1 = new File(".\\test2.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file1);
// 指定輸出文件編碼為gbk,不知道為啥做替換插件的那個2b不使用UTF-8
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "gbk");
// OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
BufferedWriter writer = new BufferedWriter(outputStreamWriter);
for (String s : list) {
writer.append(s).append("\n");
String query = FanyiV3Demo.query(s);
String translation = JSON.parseObject(query).getJSONArray("translation").get(0).toString();
writer.append(translation).append("\n");
}
writer.flush();
}
public static List<String> find(String regex, String str) {
List<String> strings = new LinkedList<>();
Pattern p = Pattern.compile(regex);
Matcher matcher = p.matcher(str);
while (matcher.find()) {
String fundStr = str.substring(matcher.start(), matcher.end());
System.out.println(fundStr);
strings.add(fundStr);
}
return strings;
}
}
有道翻譯Java SDK改裝
/**
* @author ShuangLian
* @date 2022/7/4 3:49
*/
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
/**
* 由有道提供,直接調用query方法即可
* 返回示例:
* {
* "tSpeakUrl": "https://openapi.youdao.com/ttsapi?",
* "requestId": "afc14ed2-e6ca-49fe-8f8f-b36e6ff5bbaa",
* "query": "Preview on Hover for File Links",
* "translation": [
* "預覽懸停文件鏈接"
* ],
* "errorCode": "0",
* "dict": {
* "url": "yddict://m.youdao.com/dict?le=eng&q=Preview+on+Hover+for+File+Links"
* },
* "webdict": {
* "url": "http://mobile.youdao.com/dict?le=eng&q=Preview+on+Hover+for+File+Links"
* },
* "l": "en2zh-CHS",
* "isWord": false,
* "speakUrl": "https://openapi.youdao.com/ttsapi?"
* }
*/
public class FanyiV3Demo {
private static Logger logger = LoggerFactory.getLogger(FanyiV3Demo.class);
private static final String YOUDAO_URL = "https://openapi.youdao.com/api";
// 去有道查看,這裡不能直接用
private static final String APP_KEY = "1fcc4";
//
private static final String APP_SECRET = "LgA5Wxpm60KP7nyuGp";
public static void main(String[] args) throws IOException {
Map<String, String> params = new HashMap<String, String>();
String q = "Decrease body font size";
String salt = String.valueOf(System.currentTimeMillis());
params.put("from", "en");
params.put("to", "zh-CHS");
params.put("signType", "v3");
String curtime = String.valueOf(System.currentTimeMillis() / 1000);
params.put("curtime", curtime);
String signStr = APP_KEY + truncate(q) + salt + curtime + APP_SECRET;
String sign = getDigest(signStr);
params.put("appKey", APP_KEY);
params.put("q", q);
params.put("salt", salt);
params.put("sign", sign);
params.put("vocabId", "您的用戶詞表ID");
/** 處理結果 */
requestForHttp(YOUDAO_URL, params);
}
public static String query(String Str) throws IOException {
Map<String, String> params = new HashMap<String, String>();
String q = Str;
String salt = String.valueOf(System.currentTimeMillis());
params.put("from", "en");
params.put("to", "zh-CHS");
params.put("signType", "v3");
String curtime = String.valueOf(System.currentTimeMillis() / 1000);
params.put("curtime", curtime);
String signStr = APP_KEY + truncate(q) + salt + curtime + APP_SECRET;
String sign = getDigest(signStr);
params.put("appKey", APP_KEY);
params.put("q", q);
params.put("salt", salt);
params.put("sign", sign);
params.put("vocabId", "您的用戶詞表ID");
/** 處理結果 */
return requestForHttp(YOUDAO_URL, params);
}
public static String requestForHttp(String url, Map<String, String> params) throws IOException {
String json = null;
/** 創建HttpClient */
CloseableHttpClient httpClient = HttpClients.createDefault();
/** httpPost */
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
Iterator<Map.Entry<String, String>> it = params.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> en = it.next();
String key = en.getKey();
String value = en.getValue();
paramsList.add(new BasicNameValuePair(key, value));
}
httpPost.setEntity(new UrlEncodedFormEntity(paramsList, "UTF-8"));
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
try {
Header[] contentType = httpResponse.getHeaders("Content-Type");
logger.info("Content-Type:" + contentType[0].getValue());
if ("audio/mp3".equals(contentType[0].getValue())) {
//如果響應是wav
HttpEntity httpEntity = httpResponse.getEntity();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
httpResponse.getEntity().writeTo(baos);
byte[] result = baos.toByteArray();
EntityUtils.consume(httpEntity);
if (result != null) {//合成成功
String file = "合成的音頻存儲路徑" + System.currentTimeMillis() + ".mp3";
byte2File(result, file);
}
} else {
/** 響應不是音頻流,直接顯示結果 */
HttpEntity httpEntity = httpResponse.getEntity();
json = EntityUtils.toString(httpEntity, "UTF-8");
EntityUtils.consume(httpEntity);
logger.info(json);
System.out.println(json);
}
} finally {
try {
if (httpResponse != null) {
httpResponse.close();
}
} catch (IOException e) {
logger.info("## release resouce error ##" + e);
}
}
return json;
}
/**
* 生成加密欄位
*/
public static String getDigest(String string) {
if (string == null) {
return null;
}
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
byte[] btInput = string.getBytes(StandardCharsets.UTF_8);
try {
MessageDigest mdInst = MessageDigest.getInstance("SHA-256");
mdInst.update(btInput);
byte[] md = mdInst.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (byte byte0 : md) {
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (NoSuchAlgorithmException e) {
return null;
}
}
/**
* @param result 音頻位元組流
* @param file 存儲路徑
*/
private static void byte2File(byte[] result, String file) {
File audioFile = new File(file);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(audioFile);
fos.write(result);
} catch (Exception e) {
logger.info(e.toString());
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static String truncate(String q) {
if (q == null) {
return null;
}
int len = q.length();
String result;
return len <= 20 ? q : (q.substring(0, 10) + len + q.substring(len - 10, len));
}
}
還有一個問題:
Quicker插件只是替換功能,有可能會替換到正文的單詞。這裡應該是需要再使用正則匹配一遍。乾脆改成只用java來處理吧。