This commit is contained in:
parent
f994f8722d
commit
83a085a343
@ -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
|
||||||
*/
|
*/
|
||||||
|
@ -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,27 +58,20 @@ 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();
|
|
||||||
if (typeof props.onTranslated === "function" && result) {
|
|
||||||
props.onTranslated({
|
props.onTranslated({
|
||||||
text: result.translatedText,
|
text: result.translations[0].text,
|
||||||
fromLanguage: langNames.of(result.detectedLanguage.language),
|
fromLanguage: langNames.of(result.translations[0].detected_source_language),
|
||||||
confidence: result.detectedLanguage.confidence,
|
confidence: 1,
|
||||||
} as NoteTranslation);
|
} as NoteTranslation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
async function copyId() {
|
async function copyId() {
|
||||||
const link = NostrLink.fromEvent(ev).encode(CONFIG.eventLinkPrefix);
|
const link = NostrLink.fromEvent(ev).encode(CONFIG.eventLinkPrefix);
|
||||||
|
@ -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) {
|
||||||
|
16
packages/app/src/External/SnortApi.ts
vendored
16
packages/app/src/External/SnortApi.ts
vendored
@ -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,
|
||||||
|
Loading…
Reference in New Issue
Block a user