From 3ae347e4c5ad47a6447c43fedf61786a0283e438 Mon Sep 17 00:00:00 2001 From: olegase123 Date: Fri, 15 Sep 2023 21:36:39 +0500 Subject: [PATCH] Added Sugor Translator support (master branch) --- src/components/TranslatedMessage.svelte | 21 ++++++++++++++- .../MessageTranslationSettings.svelte | 6 ++++- src/ts/storage.ts | 3 +++ src/ts/sugoi-translator-offline.ts | 27 +++++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/ts/sugoi-translator-offline.ts diff --git a/src/components/TranslatedMessage.svelte b/src/components/TranslatedMessage.svelte index 193789af..a467ebf5 100644 --- a/src/components/TranslatedMessage.svelte +++ b/src/components/TranslatedMessage.svelte @@ -1,5 +1,5 @@ + + + + {#if $enabled} diff --git a/src/ts/storage.ts b/src/ts/storage.ts index 1578e43d..8941bae4 100644 --- a/src/ts/storage.ts +++ b/src/ts/storage.ts @@ -4,11 +4,14 @@ import type { Writable } from 'svelte/store'; import { getClient, AvailableLanguages } from 'iframe-translator'; import type { IframeTranslatorClient, AvailableLanguageCodes } from 'iframe-translator'; import { ChatReportUserOptions, Theme, YoutubeEmojiRenderMode } from './chat-constants'; +import { SugoiTranslatorOffline } from './sugoi-translator-offline'; export const stores = webExtStores(); export const hcEnabled = stores.addSyncStore('hc.enabled', true); export const translateTargetLanguage = stores.addSyncStore('hc.translateTargetLanguage', '' as '' | AvailableLanguageCodes); +export const sugoiTranslatorOfflineClient = readable(new SugoiTranslatorOffline()); +export const sugoiTranslatorOfflineEnabled = stores.addSyncStore('sugoiTranslatorOffline.enabled', false); export const translatorClient = readable(null as (null | IframeTranslatorClient), (set) => { let client: IframeTranslatorClient | null = null; const destroyIf = (): void => { diff --git a/src/ts/sugoi-translator-offline.ts b/src/ts/sugoi-translator-offline.ts new file mode 100644 index 00000000..baaa65c8 --- /dev/null +++ b/src/ts/sugoi-translator-offline.ts @@ -0,0 +1,27 @@ +/** + * Only translates from JP to EN. For it to work, you need to install and run Sugoi Translator server on your local machine + */ +export class SugoiTranslatorOffline { + translate(text: string) : Promise { + const SugoiHost = '127.0.0.1' + const SugoiPort = 14366 + const SugoiPath = '' + + return fetch(`http://${SugoiHost}:${SugoiPort}/${SugoiPath}`, { + method: 'POST', + headers: { + 'content-type': 'application/json' + }, + body: JSON.stringify({ + message: 'translate sentences', + content: text + }) + }).then(response => { + if (!response.ok) { + throw new Error(`Error: ${response.status}`); + } + + return response.json() as Promise; + }); + } +}