Skip to content

Commit

Permalink
1. Fix the problem that can only translate between Chinese and English
Browse files Browse the repository at this point in the history
2. Enable omegat's own cache to reduce network requests for the same
text, faster and more cost-effective
  • Loading branch information
xflcx1991 committed Sep 8, 2020
1 parent d15bc9f commit 41f12d3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ buildscript {
}
}

version = '0.1'
version = '0.2'

configurations {
provided
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/xyz/xffish/machinetranslators/YoudaoTranslate.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class YoudaoTranslate extends BaseTranslate {
* 在软件启动时会自动调用该函数来注册插件
*/
public static void loadPlugins() {
logger.info("加载 YoudaoTranslate Plugin");
logger.debug("加载 YoudaoTranslate Plugin");

Core.registerMachineTranslationClass(YoudaoTranslate.class);
}
Expand All @@ -76,7 +76,7 @@ protected String getPreferenceName() {
}

/**
* 在软件里显示该翻译插件的名字
* 在软件里显示该翻译插件的名字.
*
* @return 本翻译插件显示的名字
*/
Expand Down Expand Up @@ -130,7 +130,7 @@ public static String truncate(String q) {
*/
@Override
protected String translate(Language sLang, Language tLang, String text) throws Exception {
logger.info("translate,sLang={},tLang={},text={}", sLang, tLang, text);
logger.debug("translate,sLang={},tLang={},text={}", sLang, tLang, text);
/*
* 请求用 post+表单形式
* 调用API需要向接口发送以下字段来访问服务。
Expand Down Expand Up @@ -166,7 +166,7 @@ protected String translate(Language sLang, Language tLang, String text) throws E
String prev = getFromCache(sLang, tLang, lvShortText);
if (prev != null) {
// 啊,有缓存,那就直接返回不用请求了
logger.info("啊,有缓存,太美妙了:{}", prev);
logger.debug("啊,有缓存,太美妙了:{}", prev);
return prev;
}

Expand All @@ -193,7 +193,7 @@ protected String translate(Language sLang, Language tLang, String text) throws E
String secretKey = getCredential(PROPERTY_APP_KEY);


logger.info("secretKey = {}", secretKey);
logger.debug("secretKey = {}", secretKey);
String curtime = String.valueOf(System.currentTimeMillis() / 1000);
// 开始计算
String originSign = appKey + truncate(lvShortText) + uuid + curtime + secretKey;
Expand All @@ -215,12 +215,13 @@ protected String translate(Language sLang, Language tLang, String text) throws E


String paramMapStr = JSONUtil.toJsonStr(paramMap);
logger.info("paramMap = {}", paramMapStr);
logger.debug("paramMap = {}", paramMapStr);
// -----------------发 POST 请求-----------------
String responseBody = HttpUtil.post(URL, paramMap);


logger.info("response body = {}", responseBody);
logger.debug("response body = {}", responseBody);


JSONObject jsonObject = JSONUtil.parseObj(responseBody);

Expand All @@ -230,6 +231,8 @@ protected String translate(Language sLang, Language tLang, String text) throws E

if (errorCode.equals("0")) {
translation = jsonObject.getStr("translation");
// 把这次结果添加进缓存
putToCache(sLang, tLang, lvShortText, translation);
} else {
// 出错了,从 errorCode2DescMap 找错误描述信息
String errorCodeDesc = ErrorCode2Desc.translateErrorCode2Desc(errorCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ public final class OLang2YLang {
.put("ig", "ig")
.put("is", "is")
.put("it", "it")
.put("ja", "jw") //有道官网就是坚持这样写 爪哇语
.put("ja", "ja")
.put("jv", "jw") //有道官网就是坚持这样写 爪哇语
.put("ka", "ka")
.put("kk", "kk")
.put("km", "km")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,20 @@
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.omegat.core.Core;
import org.omegat.gui.exttrans.MTConfigDialog;
import org.omegat.util.Language;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.xffish.machinetranslators.util.ErrorCode2Desc;
import xyz.xffish.machinetranslators.util.OLang2YLang;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

/**
* 搞不定脱离 OmegaT 环境的初始化,只能重新搞一个类似功能但不依赖 OmegaT 的功能类.
* BaseTranslate 构造函数用到了已经初始化的 Preferences 类,测试环境搞不定如何初始化。
* 只能手动维护 YoudaoTranslate 和 YoudaoTranslateClone 的同步
*/
public class YoudaoTranslateClone extends BaseTranslateClone{
public class YoudaoTranslateClone extends BaseTranslateClone {
/**
* 设置存储 key 的名字,读取和设置值由 OmegaT 提供 API 来操作
*/
Expand All @@ -48,9 +45,6 @@ public class YoudaoTranslateClone extends BaseTranslateClone{
private static final Logger logger = LoggerFactory.getLogger(YoudaoTranslate.class);





/**
* 在软件启动时会自动调用该函数来注册插件
*/
Expand Down Expand Up @@ -220,6 +214,7 @@ protected String translate(Language sLang, Language tLang, String text) throws E


logger.info("response body = {}", responseBody);
System.out.println("response body = " + responseBody);

JSONObject jsonObject = JSONUtil.parseObj(responseBody);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package xyz.xffish.machinetranslators;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.omegat.util.Language;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Locale;

public class YoudaoTranslateCloneTest {
Expand Down Expand Up @@ -51,9 +55,10 @@ public void setUp() throws Exception {
}

@Test
public void justTest() throws Exception {
@Ignore
public void translate_en2zh_Test() throws Exception {

System.out.println("justTest - 仅供测试");
System.out.println("translate_en2zh_Test - 测试翻译");
Locale l1 = Locale.US;
Locale l2 = Locale.CHINA;
Language sLang = new Language(l1);
Expand All @@ -66,6 +71,25 @@ public void justTest() throws Exception {
System.out.println(translate);


}

@Test
@Ignore
public void translate_zh2en_Test() throws Exception {

System.out.println("translate_zh2en_Test - 测试翻译");
Locale l1 = Locale.CHINA;
Locale l2 = Locale.US;
Language sLang = new Language(l1);
Language tLang = new Language(l2);
String text = "苹果";
String translate = youdaoTranslateCloneInstance.translate(sLang, tLang, text);
System.out.println(translate);
System.out.println("--------测试缓存----------");
translate = youdaoTranslateCloneInstance.translate(sLang, tLang, text);
System.out.println(translate);


}

@Test
Expand All @@ -78,4 +102,32 @@ public void getNameTest() {
logger.info("getNameTest - {}", youdaoTranslateCloneInstance.getName());
}

@Test
public void omegatLang2YoudaoLangTest() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//测试没有参数的echoRequest()方法
Method testMethod = youdaoTranslateCloneInstance.getClass().getDeclaredMethod("omegatLang2YoudaoLang", String.class);
//Method对象继承自java.lang.reflect.AccessibleObject,父类方法setAccessible可调
//将此对象的 accessible 标志设置为指示的布尔值。值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。值为 false 则指示反射的对象应该实施 Java 语言访问检查。
//要访问私有方法必须将accessible设置为true,否则抛java.lang.IllegalAccessException
testMethod.setAccessible(true);
//只检查常用语言和一个auto
Object result = testMethod.invoke(youdaoTranslateCloneInstance, "zh");
Assert.assertEquals("should be zh-CHS", result, "zh-CHS");

result = testMethod.invoke(youdaoTranslateCloneInstance, "en");
Assert.assertEquals("should be en", result, "en");

result = testMethod.invoke(youdaoTranslateCloneInstance, "ja");
Assert.assertEquals("should be en", result, "ja");

result = testMethod.invoke(youdaoTranslateCloneInstance, "ko");
Assert.assertEquals("should be en", result, "ko");

result = testMethod.invoke(youdaoTranslateCloneInstance, "de");
Assert.assertEquals("should be en", result, "de");

}



}

0 comments on commit 41f12d3

Please sign in to comment.