chore (backend): add some more comments on the translate function

This commit is contained in:
naskya 2024-04-06 07:42:32 +09:00
parent 0a080159d4
commit 54c5192a06
No known key found for this signature in database
GPG Key ID: 712D413B3A9FED5C
1 changed files with 5 additions and 0 deletions

View File

@ -5,6 +5,9 @@ import { fetchMeta } from "@/misc/fetch-meta.js";
import type { PostLanguage } from "@/misc/langmap";
import * as deepl from "deepl-node";
// DeepL translate and LibreTranslate don't provide
// zh-Hant-TW translations, so we convert zh-Hans-CN
// translations into zh-Hant-TW using opencc-js.
function convertChinese(convert: boolean, src: string) {
if (!convert) return src;
const converter = Converter({ from: "cn", to: "twp" });
@ -78,6 +81,8 @@ export async function translate(
const result = await deeplTranslator.translateText(
text,
source as deepl.SourceLanguageCode | null,
// DeepL API requires us to specify "en-US" or "en-GB" for English
// translations ("en" does not work), so we need to address it
(target === "en" ? to : target) as deepl.TargetLanguageCode,
);