Skip to content
This repository was archived by the owner on Jan 4, 2025. It is now read-only.

Commit

Permalink
Merge branch 'main' into online
Browse files Browse the repository at this point in the history
  • Loading branch information
SIPC committed Feb 14, 2024
2 parents 628eff0 + ad5195e commit 3d03e80
Show file tree
Hide file tree
Showing 8 changed files with 2,902 additions and 9 deletions.
2,824 changes: 2,824 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@
"google-translate-api-x": "^10.6.8",
"jsonwebtoken": "^9.0.2",
"langdetect": "^0.2.1",
"unicode-9.0.0":"0.7.0",
"lucide-react": "^0.323.0",
"next": "14.1.0",
"prettier": "^3.2.5",
"react": "^18",
"react-dom": "^18",
"tailwind-merge": "^2.2.1",
"tailwindcss-animate": "^1.0.7"
"tailwindcss-animate": "^1.0.7",
"unicode-9.0.0": "0.7.0"
},
"devDependencies": {
"@types/jsonwebtoken": "^9.0.5",
"@types/langdetect": "^0.2.2",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-beautiful-dnd": "^13.1.8",
"@types/react-dom": "^18",
"@types/jsonwebtoken": "^9.0.5",
"@types/langdetect": "^0.2.2",
"autoprefixer": "^10.0.1",
"postcss": "^8",
"tailwindcss": "^3.3.0",
Expand Down
2 changes: 2 additions & 0 deletions src/components/ResultBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ export function ResultContainer({ loading, result }: ResultContainerProps) {
<ResultBox loading={loading} name="DeepLX" content={result.deeplx} />
<ResultBox loading={loading} name="Microsoft" content={result.microsoft} />
<ResultBox loading={loading} name="Google" content={result.google} />
<ResultBox loading={loading} name="Transmart" content={result.transmart} />
<ResultBox loading={loading} name="Niutrans" content={result.niutrans} />
<ResultBox loading={loading} name="M2m100" content={result.m2m100} />
</div>
);
}
4 changes: 4 additions & 0 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export type TranslateResult = {
microsoft: string;
google: string;
niutrans: string;
transmart: string;
m2m100: string;
};

export type TranslateResponse = {
Expand All @@ -23,6 +25,8 @@ export const initializeTranslateState: TranslateResult = {
microsoft: "",
google: "",
niutrans: "",
transmart: "",
m2m100: "",
};

export async function translateContent(
Expand Down
3 changes: 1 addition & 2 deletions src/pages/api/lib/microsoft.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// code from sipc

import { getErrorMessage } from "@/pages/api/lib/utils";

import axios from "axios";
import { getErrorMessage } from "@/pages/api/lib/utils";
import { decode } from "jsonwebtoken";

export class Microsoft {
Expand Down
58 changes: 58 additions & 0 deletions src/pages/api/lib/transmart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import axios from "axios";
import { getErrorMessage } from "@/pages/api/lib/utils";

export class Transmart {
API_TRANSLATE = "https://yi.qq.com/api/imt";

constructor() {
}

async translate(text: string, targetLanguage: string, sourceLanguage: string = "en") {
try {
const source_lang = sourceLanguage ;
const target_lang = targetLanguage;

const post_data = this.initData(source_lang, target_lang, text);
const post_str = JSON.stringify(post_data);

const response = await axios.post(this.API_TRANSLATE, post_str, {
headers: {
'Content-Type': 'application/json',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
'referer': 'https://yi.qq.com/zh-CN/index'
}
});

if (response.data && response.data.auto_translation && response.data.auto_translation.length) {
return response.data.auto_translation[0].split('\n')[0]
} else {
const errMsg = response.data ? JSON.stringify(response.data) : 'Unknown error';
throw new Error(`Translation API Error: ${errMsg}`);
}

} catch (error) {
throw new Error(`Error during translation: ${getErrorMessage(error)}`);
}
}
initData(source_lang: string, target_lang: string, translate_text: string) {
return {
"header": {
"fn": "auto_translation",
"client_key": "browser-chrome-110.0.0-Mac OS-df4bd4c5-a65d-44b2-a40f-42f34f3535f2-1677486696487"
},
"type": "plain",
"model_category": "normal",
"source": {
"lang": source_lang,
"text_list": [
translate_text
]
},
"target": {
"lang": target_lang
}
};
}
}

export const TransmartInstance = new Transmart();
10 changes: 8 additions & 2 deletions src/pages/api/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { GoogleInstance } from "./lib/google";
import { NiutransInstance } from "./lib/niutrans";
import { M2m100Instance } from "./lib/m2m100";
import { GeminiInstance } from "./lib/gemini";
import { TransmartInstance } from "./lib/transmart";
import { autodetect } from "./lib/autodetect";


Expand All @@ -17,6 +18,7 @@ type TranslateResult = {
google: string;
gemini: string;
niutrans: string;
transmart: string;
m2m100: string;
};

Expand Down Expand Up @@ -50,7 +52,7 @@ export default async function handler(
sourceLanguage = await autodetect(text);

// code from sipc
const [chatgpt, gemini, deeplx, microsoft, google, niutrans, m2m100] =
const [chatgpt, gemini, deeplx, microsoft, google, niutrans, transmart, m2m100] =
await Promise.all([
ChatGPTInstance.translate(text, targetLanguage, sourceLanguage).catch(
(e) => e.message,
Expand All @@ -70,6 +72,9 @@ export default async function handler(
NiutransInstance.translate(text, targetLanguage, sourceLanguage).catch(
(e) => e.message,
),
TransmartInstance.translate(text, targetLanguage, sourceLanguage).catch(
(e) => e.message,
),
M2m100Instance.translate(text, targetLanguage, sourceLanguage).catch(
(e) => e.message,
),
Expand All @@ -85,7 +90,8 @@ export default async function handler(
microsoft,
google,
niutrans,
m2m100
transmart,
m2m100,
},
});
} catch (e) {
Expand Down
1 change: 0 additions & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Github from "@/components/icons/Github";
import Link from "next/link";
import ThemeProvider from "@/components/ThemeProvider";
import LanguageSelect from "@/components/LanguageSelect";
import { Head } from "next/document";
import {
initializeTranslateState,
translateContent,
Expand Down

0 comments on commit 3d03e80

Please sign in to comment.