Skip to content

Commit

Permalink
fix baidu translate language error
Browse files Browse the repository at this point in the history
  • Loading branch information
SIPC committed Feb 24, 2024
1 parent 62a005e commit 8aa0528
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
8 changes: 3 additions & 5 deletions src/lib/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
];
8 changes: 8 additions & 0 deletions src/pages/api/lib/baidu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import MD5 from "md5";
import axios from "axios";
import { getErrorMessage } from "@/pages/api/lib/utils";

const languageMap: Record<string, string> = {
"ja": "jp",
"fr": "fra",
"es": "spa",
};

export class Baidu {
public APPID: string;
public APPKEY: string;
Expand All @@ -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 {
Expand Down
8 changes: 2 additions & 6 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@ export default function Home() {
const [to, setTo] = useState<string>("zh");

const [isExpanded, setIsExpanded] = useState<boolean>(false);
const [loader, setLoader] = useState<Translateloader>(
initializeTranslateloader,
);
const [result, setResult] = useState<TranslateResult>(
initializeTranslateState,
);
const [loader, setLoader] = useState<Translateloader>(initializeTranslateloader);
const [result, setResult] = useState<TranslateResult>(initializeTranslateState);

const handleClick = () => {
setIsExpanded(!isExpanded);
Expand Down

0 comments on commit 8aa0528

Please sign in to comment.