From 8abed1108acf7c7580f656b8c711675bc1251c8f 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 --- README.md | 2 ++ packages/backend/package.json | 1 + .../src/server/api/endpoints/notes/translate.ts | 12 ++++++++++-- pnpm-lock.yaml | 7 +++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e3ecf30c..598eda9a 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ ## 主要な変更点 +- 繁体中文への投稿翻訳を繁体字で表示する + - DeepL 翻訳や LibreTranslate は簡体中文への翻訳しか提供していない……。 - モデレーターでない一般ユーザーにもカスタム絵文字の管理権を与えられるように - カスタム絵文字の管理が大変なサーバー管理者さんがたくさんいらっしゃったのでこの機能を追加するべきではないか他の開発者に訊いたところロール機能の実装を待つべきだと言われてしまったが、Firefish のロール機能は現状では仕様がまだ固まっておらず実装までに時間が掛かると考えられるため - 以下の権限を与えられます diff --git a/packages/backend/package.json b/packages/backend/package.json index b7658c57..bbc3166f 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -98,6 +98,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 d86fc12a..414e761c 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 cd2c0cab..0a0c52b1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -297,6 +297,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 @@ -12143,6 +12146,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