fix: 自定义翻译简体中文统一使用 zh-hans
This commit is contained in:
@@ -81,6 +81,7 @@ server/data/spanish_diary.db
|
||||
- 共享知识库
|
||||
- 每用户翻译配置(本地 / MyMemory / 自定义接口)
|
||||
- 实时翻译、选中翻译、保存和中译西统一调用每用户翻译配置
|
||||
- LibreTranslate 等自定义接口默认使用 `zh-hans` 表示简体中文,可通过扩展配置 `zh_code` 覆盖
|
||||
- 每用户日记统计
|
||||
- 每用户单词收藏
|
||||
- 手机、平板、桌面响应式适配
|
||||
|
||||
@@ -116,7 +116,7 @@ function translateEditorToEnZh(text) {
|
||||
}
|
||||
return Promise.all([
|
||||
apiTranslate({ text: text, source: 'es', target: 'en' }),
|
||||
apiTranslate({ text: text, source: 'es', target: 'zh' })
|
||||
apiTranslate({ text: text, source: 'es', target: 'zh-hans' })
|
||||
]).then(function(results) {
|
||||
if (!results[0].translatedText || !results[1].translatedText) {
|
||||
throw new Error('翻译接口未返回结果');
|
||||
@@ -397,7 +397,7 @@ function translateZhOnline(zh) {
|
||||
var seq = ++helperRequestSeq;
|
||||
var request;
|
||||
if (shouldUseApiTranslation()) {
|
||||
request = apiTranslate({ text: zh, source: 'zh', target: 'es' }).then(function(data) {
|
||||
request = apiTranslate({ text: zh, source: 'zh-hans', target: 'es' }).then(function(data) {
|
||||
return { translatedText: data.translatedText };
|
||||
});
|
||||
} else if (!BACKEND_MODE) {
|
||||
|
||||
@@ -85,7 +85,7 @@ def translate_text(user):
|
||||
text = (data.get("text") or "").strip()
|
||||
if not text:
|
||||
return jsonify({"error": "翻译内容不能为空"}), 400
|
||||
source = (data.get("source") or "zh").strip()
|
||||
source = (data.get("source") or "zh-hans").strip()
|
||||
target = (data.get("target") or "es").strip()
|
||||
settings = get_user_settings(user["id"])
|
||||
result = perform_translate(text, settings, source, target)
|
||||
|
||||
@@ -19,7 +19,7 @@ def translate_mymemory(text, source="zh-CN", target="es"):
|
||||
return {"ok": False, "error": str(exc)}
|
||||
|
||||
|
||||
def translate_custom(text, settings, source="zh", target="es"):
|
||||
def translate_custom(text, settings, source="zh-hans", target="es"):
|
||||
api_url = (settings.get("api_url") or "").strip()
|
||||
if not api_url:
|
||||
return {"ok": False, "error": "未配置自定义翻译接口地址"}
|
||||
@@ -30,6 +30,12 @@ def translate_custom(text, settings, source="zh", target="es"):
|
||||
except Exception:
|
||||
extra = {}
|
||||
|
||||
zh_code = extra.get("zh_code", "zh-hans")
|
||||
if source.lower() in ("zh", "zh-cn", "zh-hans"):
|
||||
source = zh_code
|
||||
if target.lower() in ("zh", "zh-cn", "zh-hans"):
|
||||
target = zh_code
|
||||
|
||||
payload = extra.get("payload", {})
|
||||
payload["q"] = text
|
||||
payload["source"] = source
|
||||
@@ -65,14 +71,18 @@ def translate_custom(text, settings, source="zh", target="es"):
|
||||
return {"ok": False, "error": str(exc)}
|
||||
|
||||
|
||||
def perform_translate(text, settings, source="zh", target="es"):
|
||||
def perform_translate(text, settings, source="zh-hans", target="es"):
|
||||
provider = (settings.get("translator") or "local").lower()
|
||||
if provider == "mymemory":
|
||||
if source.lower() in ("zh", "zh-cn", "zh-hans"):
|
||||
source = "zh-hans"
|
||||
if target.lower() in ("zh", "zh-cn", "zh-hans"):
|
||||
target = "zh-hans"
|
||||
lang_map = {
|
||||
("zh", "es"): ("zh-CN", "es"),
|
||||
("es", "zh"): ("es", "zh-CN"),
|
||||
("zh", "en"): ("zh-CN", "en"),
|
||||
("en", "zh"): ("en", "zh-CN"),
|
||||
("zh-hans", "es"): ("zh-CN", "es"),
|
||||
("es", "zh-hans"): ("es", "zh-CN"),
|
||||
("zh-hans", "en"): ("zh-CN", "en"),
|
||||
("en", "zh-hans"): ("en", "zh-CN"),
|
||||
("es", "en"): ("es", "en"),
|
||||
("en", "es"): ("en", "es"),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user