From 82e511457689322bd81542096b8f709102e94faa Mon Sep 17 00:00:00 2001 From: naskya Date: Tue, 29 Aug 2023 14:20:54 +0900 Subject: [PATCH] feat: post translation in Traditional Chinese --- packages/backend/package.json | 1 + .../src/server/api/endpoints/notes/translate.ts | 12 ++++++++++-- pnpm-lock.yaml | 7 +++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/backend/package.json b/packages/backend/package.json index 3305504898..db8050df71 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -100,6 +100,7 @@ "nodemailer": "6.9.4", "nsfwjs": "2.4.2", "oauth": "^0.10.0", + "opencc-js": "^1.0.5", "os-utils": "0.0.14", "otpauth": "^9.1.4", "parse5": "7.1.2", diff --git a/packages/backend/src/server/api/endpoints/notes/translate.ts b/packages/backend/src/server/api/endpoints/notes/translate.ts index d86fc12a2e..414e761c1c 100644 --- a/packages/backend/src/server/api/endpoints/notes/translate.ts +++ b/packages/backend/src/server/api/endpoints/notes/translate.ts @@ -1,6 +1,7 @@ import { URLSearchParams } from "node:url"; import fetch from "node-fetch"; import config from "@/config/index.js"; +import { Converter } from "opencc-js"; import { getAgentByUrl } from "@/misc/fetch.js"; import { fetchMeta } from "@/misc/fetch-meta.js"; import { Notes } from "@/models/index.js"; @@ -38,6 +39,13 @@ export const paramDef = { required: ["noteId", "targetLang"], } as const; +function convertChinese(convert: boolean, src: string) { + if (!convert) return src; + + const converter = Converter({ from: "cn", to: "twp" }); + return converter(src); +} + export default define(meta, paramDef, async (ps, user) => { const note = await getNote(ps.noteId, user).catch((err) => { if (err.id === "9725d0ce-ba28-4dde-95a7-2cbb2c15de24") @@ -93,7 +101,7 @@ export default define(meta, paramDef, async (ps, user) => { return { sourceLang: json.detectedLanguage?.language, - text: json.translatedText, + text: convertChinese(ps.targetLang === "zh-TW", json.translatedText), }; } @@ -128,6 +136,6 @@ export default define(meta, paramDef, async (ps, user) => { return { sourceLang: json.translations[0].detected_source_language, - text: json.translations[0].text, + text: convertChinese(ps.targetLang === "zh-TW", json.translations[0].text), }; }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 73c2b678b7..18b16333d9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -303,6 +303,9 @@ importers: oauth: specifier: ^0.10.0 version: 0.10.0 + opencc-js: + specifier: ^1.0.5 + version: 1.0.5 os-utils: specifier: 0.0.14 version: 0.0.14 @@ -14555,6 +14558,10 @@ packages: is-wsl: 2.2.0 dev: true + /opencc-js@1.0.5: + resolution: {integrity: sha512-LD+1SoNnZdlRwtYTjnQdFrSVCAaYpuDqL5CkmOaHOkKoKh7mFxUicLTRVNLU5C+Jmi1vXQ3QL4jWdgSaa4sKjg==} + dev: false + /opencollective-postinstall@2.0.3: resolution: {integrity: sha512-8AV/sCtuzUeTo8gQK5qDZzARrulB3egtLzFgteqB2tcT4Mw7B8Kt7JcDHmltjz6FOAHsvTevk70gZEbhM4ZS9Q==} hasBin: true