From 8aa0528474398543664752c4f10bbf3bf2bc6c49 Mon Sep 17 00:00:00 2001 From: "sipc.ink" <92251518+SIPC@users.noreply.github.com> Date: Sat, 24 Feb 2024 19:53:57 +0800 Subject: [PATCH] fix baidu translate language error --- src/lib/constant.ts | 8 +++----- src/pages/api/lib/baidu.ts | 8 ++++++++ src/pages/index.tsx | 8 ++------ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/lib/constant.ts b/src/lib/constant.ts index 05f59d3..00b0411 100644 --- a/src/lib/constant.ts +++ b/src/lib/constant.ts @@ -10,13 +10,11 @@ export type Language = { export const languages: Language[] = [ { key: "zh", value: "Chinese" }, { key: "en", value: "English" }, - { key: "nl", value: "Dutch" }, + { key: "ja", value: "Japanese" }, + { key: "ru", value: "Russian" }, { key: "fr", value: "French" }, { key: "de", value: "Deutsch" }, { key: "it", value: "Italian" }, - { key: "ja", value: "Japanese" }, - { key: "pl", value: "Polish" }, - { key: "pt", value: "Portuguese" }, - { key: "ru", value: "Russian" }, { key: "es", value: "Spanish" }, + { key: "pt", value: "Portuguese" }, ]; diff --git a/src/pages/api/lib/baidu.ts b/src/pages/api/lib/baidu.ts index 43aaa46..a55a35d 100644 --- a/src/pages/api/lib/baidu.ts +++ b/src/pages/api/lib/baidu.ts @@ -4,6 +4,12 @@ import MD5 from "md5"; import axios from "axios"; import { getErrorMessage } from "@/pages/api/lib/utils"; +const languageMap: Record = { + "ja": "jp", + "fr": "fra", + "es": "spa", +}; + export class Baidu { public APPID: string; public APPKEY: string; @@ -13,6 +19,8 @@ export class Baidu { this.APPKEY = APPKEY; } async translate(text: string, target: string, source: string = "auto") { + target = languageMap[target] || target + source = languageMap[source] || source const salt = String(Math.random()).slice(2) const sign = MD5(this.APPID+text+salt+this.APPKEY) try { diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 217b7ed..d784e7a 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -37,12 +37,8 @@ export default function Home() { const [to, setTo] = useState("zh"); const [isExpanded, setIsExpanded] = useState(false); - const [loader, setLoader] = useState( - initializeTranslateloader, - ); - const [result, setResult] = useState( - initializeTranslateState, - ); + const [loader, setLoader] = useState(initializeTranslateloader); + const [result, setResult] = useState(initializeTranslateState); const handleClick = () => { setIsExpanded(!isExpanded);