feat: deepl translate
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Kieran 2023-11-02 06:11:00 +09:00
parent f994f8722d
commit 83a085a343
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
4 changed files with 30 additions and 24 deletions

View File

@ -13,11 +13,6 @@ export const Day = Hour * 24;
*/ */
export const ApiHost = "https://api.snort.social"; export const ApiHost = "https://api.snort.social";
/**
* LibreTranslate endpoint
*/
export const TranslateHost = "https://translate.snort.social";
/** /**
* Void.cat file upload service url * Void.cat file upload service url
*/ */

View File

@ -3,7 +3,6 @@ import { FormattedMessage, useIntl } from "react-intl";
import { HexKey, Lists, NostrLink, TaggedNostrEvent } from "@snort/system"; import { HexKey, Lists, NostrLink, TaggedNostrEvent } from "@snort/system";
import { Menu, MenuItem } from "@szhsin/react-menu"; import { Menu, MenuItem } from "@szhsin/react-menu";
import { TranslateHost } from "Const";
import Icon from "Icons/Icon"; import Icon from "Icons/Icon";
import { setPinned, setBookmarked } from "Login"; import { setPinned, setBookmarked } from "Login";
import messages from "Element/messages"; import messages from "Element/messages";
@ -11,6 +10,7 @@ import useLogin from "Hooks/useLogin";
import useModeration from "Hooks/useModeration"; import useModeration from "Hooks/useModeration";
import useEventPublisher from "Hooks/useEventPublisher"; import useEventPublisher from "Hooks/useEventPublisher";
import { ReBroadcaster } from "../ReBroadcaster"; import { ReBroadcaster } from "../ReBroadcaster";
import SnortApi from "External/SnortApi";
export interface NoteTranslation { export interface NoteTranslation {
text: string; text: string;
@ -58,25 +58,18 @@ export function NoteContextMenu({ ev, ...props }: NosteContextMenuProps) {
} }
async function translate() { async function translate() {
const res = await fetch(`${TranslateHost}/translate`, { const api = new SnortApi();
method: "POST", const result = await api.translate({
body: JSON.stringify({ text: [ev.content],
q: ev.content, target_lang: lang.split("-")[0].toUpperCase(),
source: "auto",
target: lang.split("-")[0],
}),
headers: { "Content-Type": "application/json" },
}); });
if (res.ok) { if (typeof props.onTranslated === "function" && result.translations.length > 0) {
const result = await res.json(); props.onTranslated({
if (typeof props.onTranslated === "function" && result) { text: result.translations[0].text,
props.onTranslated({ fromLanguage: langNames.of(result.translations[0].detected_source_language),
text: result.translatedText, confidence: 1,
fromLanguage: langNames.of(result.detectedLanguage.language), } as NoteTranslation);
confidence: result.detectedLanguage.confidence,
} as NoteTranslation);
}
} }
} }

View File

@ -246,7 +246,9 @@ export function NoteInner(props: NoteProps) {
<p className="highlight"> <p className="highlight">
<FormattedMessage {...messages.TranslatedFrom} values={{ lang: translated.fromLanguage }} /> <FormattedMessage {...messages.TranslatedFrom} values={{ lang: translated.fromLanguage }} />
</p> </p>
{translated.text} <div className="card text">
<div className="text-frag">{translated.text}</div>
</div>
</> </>
); );
} else if (translated) { } else if (translated) {

View File

@ -55,6 +55,18 @@ export interface PushNotifications {
scope: string; scope: string;
} }
export interface TranslationRequest {
text: Array<string>;
target_lang: string;
}
export interface TranslationResponse {
translations: Array<{
detected_source_language: string;
text: string;
}>;
}
export default class SnortApi { export default class SnortApi {
#url: string; #url: string;
#publisher?: EventPublisher; #publisher?: EventPublisher;
@ -104,6 +116,10 @@ export default class SnortApi {
return this.#getJsonAuthd<void>("api/v1/notifications/register", "POST", sub); return this.#getJsonAuthd<void>("api/v1/notifications/register", "POST", sub);
} }
translate(tx: TranslationRequest) {
return this.#getJson<TranslationResponse>("api/v1/translate", "POST", tx);
}
async #getJsonAuthd<T>( async #getJsonAuthd<T>(
path: string, path: string,
method?: "GET" | string, method?: "GET" | string,