Skip to content

Commit

Permalink
feat: add ability to add service-specific options (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fevol committed Jan 27, 2023
1 parent 6ded151 commit 918404c
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 13 deletions.
15 changes: 13 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,10 @@ export let SERVICES_INFO: { [key: string]: any } = {
{value: 'westus', text: 'West US'},
{value: 'westus2', text: 'West US 2'},
{value: 'westus3', text: 'West US 3'}
]
],
options: {
profanity_filter: true,
}
},
yandex_translate: {
display_name: "Yandex Translate",
Expand All @@ -864,6 +867,11 @@ export let SERVICES_INFO: { [key: string]: any } = {
url: "https://www.deepl.com/",
type: 'translation',
online_glossary: true,
options: {
split_sentences: true,
preserve_formatting: true,
formality: true,
}
},
libre_translate: {
display_name: "Libre Translate",
Expand Down Expand Up @@ -953,7 +961,10 @@ export let SERVICES_INFO: { [key: string]: any } = {
{value: "us-west-1", text: ""},
{value: "us-west-2", text: ""},
{value: "us-west-2-fips", text: ""}
]
],
options: {
formality: true,
}
},*/
lingva_translate: {
display_name: "Lingva Translate",
Expand Down
17 changes: 16 additions & 1 deletion src/handlers/azure-translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ export class AzureTranslator extends DummyTranslate {
if (this.#region)
headers["Ocp-Apim-Subscription-Region"] = this.#region;

let profanity_action = "NoAction";
if (options.profanity_filter.action === "mark")
profanity_action = "Marked";
else if (options.profanity_filter.action === "delete")
profanity_action = "Deleted";
let profanity_mark = undefined;
if (options.profanity_filter.marker === "mask")
profanity_mark = "Asterisk";
else if (options.profanity_filter.marker === "html-tag")
profanity_mark = "Tag";



const response = await requestUrl({
throw: false,
method: "POST",
Expand All @@ -119,7 +132,9 @@ export class AzureTranslator extends DummyTranslate {
from: from === "auto" ? "" : from,
to: to,
textType: "plain",
allowFallback: "true"
allowFallback: "true",
profanityAction: profanity_action,
profanityMarker: profanity_mark
}),
headers: headers,
body: JSON.stringify([{Text: text}]),
Expand Down
22 changes: 19 additions & 3 deletions src/handlers/deepl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,31 @@ export class Deepl extends DummyTranslate {
}

async service_translate(text: string, from: string, to: string, options: ServiceOptions = {}): Promise<TranslationResult> {

let split_sentences = "1";
if (options.split_sentences === "punctuation")
split_sentences = "nonewlines";
else if (options.split_sentences === "newline" || options.split_sentences === "both")
split_sentences = "0";

let preserve_formatting = options.preserve_formatting ? "1" : "0";

let formality = "default";
if (options.formality === "formal")
formality = "prefer_more";
else if (options.formality === "informal")
formality = "prefer_less";

const response = await requestUrl({
throw: false,
url: `${this.#host}/translate?` + new URLSearchParams({
text: text,
source_lang: from === "auto" ? "" : from,
target_lang: to,
split_sentences: "1",
preserve_formatting: "0",
glossary_id: options.glossary || ""
glossary_id: options.glossary || "",
split_sentences: split_sentences,
preserve_formatting: preserve_formatting,
formality: formality
}),
method: "POST",
headers: {
Expand Down
10 changes: 9 additions & 1 deletion src/handlers/dummy-translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export class DummyTranslate {
*/
available_languages: string[];

/**
* Default options/actions for the service
*/
options: ServiceOptions = {};

/**
* Internal validity status of the service, service functionality cannot be used if this is false
* @remark Service becomes invalid when it fails too many times, or when the user changes the settings
Expand Down Expand Up @@ -249,8 +254,11 @@ export class DummyTranslate {
}
}

// Merges provided options with default service options, provided options take precedence
options = {...options, ...this.options};

if (text.length < this.character_limit) {
output = await this.service_translate(text, from, to,);
output = await this.service_translate(text, from, to, options);
if (output.status_code !== 200)
return this.detected_error("Translation failed", output);
if (detecting_language && temp_detected_language)
Expand Down
8 changes: 4 additions & 4 deletions src/handlers/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ interface ServiceOptions {
* @remark Supported by DeepL
* @property { "punctuation" | "newline" | "both" } [split_sentences] - Split sentences based on punctuation or newlines
*/
split_sentences?: "punctuation" | "newline" | "both";
split_sentences?: "none" | "punctuation" | "newline" | "both";

/**
* Preserve formatting of the input text (i.e.: punctuation or upper/lowercase at beginning/end of sentences)
Expand All @@ -60,7 +60,7 @@ interface ServiceOptions {
* @remark Supported by DeepL (pro), Amazon Translate
* @property { "more" | "less" } [formality] - Lean towards a more formal/informal translation
*/
formality?: "formal" | "informal";
formality?: "default" | "formal" | "informal";


/**
Expand All @@ -78,8 +78,8 @@ interface ServiceOptions {
* @property { string } [profanity_filter.marker] - Marker to use when profanity is detected (only used when action is "Marked")
*/
profanity_filter?: {
action: "mark" | "delete";
marker?: string;
action: "none" | "mark" | "delete";
marker?: "mask" | "html-tag";
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {Modifier} from "obsidian";
import type {ServiceOptions} from "./handlers/types";

/**
* Object containing all plugin settings
Expand Down Expand Up @@ -162,7 +163,7 @@ export interface APIServiceProviders {
/**
* Object containing the settings for a single API service
*/
export interface APIServiceSettings {
export interface APIServiceSettings extends ServiceOptions {
/**
* List of user-selected languages (locales) that will be available with the 'manually_selected' <i>(2)</i> filter mode
*/
Expand Down
8 changes: 8 additions & 0 deletions src/ui/translator-components/Reactivity.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@
translation_service.available_languages = $settings.service_settings[service].available_languages;
}
// Provide default options to the service
translation_service.options = {
split_sentences: service_settings.split_sentences,
preserve_formatting: service_settings.preserve_formatting,
formality: service_settings.formality,
profanity_filter: service_settings.profanity_filter,
}
active_services[service] = translation_service;
translator = translation_service;
service_uses[service] = 1;
Expand Down
129 changes: 128 additions & 1 deletion src/ui/translator-components/settings-tabs/TranslatorSettings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,12 @@
{/if}



{#if service === 'bergamot'}
<SettingItem
name="Service set-up"
type="heading"
/>

<SettingItem
name="Setup local translation"
description="Install Bergamot translation engine (size: 5.05MiB)"
Expand Down Expand Up @@ -361,6 +365,11 @@

{/if}

<SettingItem
name="General settings"
type="heading"
/>

{#if info.request_key !== undefined}
<SettingItem
name="API Key"
Expand Down Expand Up @@ -610,3 +619,121 @@
/>
</SettingItem>
{/if}

{#if info.options}
<SettingItem
name="Service-specific options"
type="heading"
/>

{#if info.options.split_sentences}
<SettingItem
name="Split sentences"
description="Determine if sentences should be split into separate lines"
type="dropdown"
>
<Dropdown
slot="control"
value={ $settings.service_settings[service].split_sentences ?? "none" }
options={[
{value: "none", text: "Do not split sentences"},
{value: "punctuation", text: "Split sentences on punctuation"},
{value: "newline", text: "Split sentences on newlines"},
{value: "both", text: "Split sentences on both"}
]}
onChange={(e) => {
$settings.service_settings[service].split_sentences = e.target.value;
translator.options.split_sentences = e.target.value;
}}
/>
</SettingItem>
{/if}

{#if info.options.preserve_formatting}
<SettingItem
name="Preserve formatting"
description="Do not change the formatting of the source text"
type="toggle"
>
<Toggle
slot="control"
value={ $settings.service_settings[service].preserve_formatting }
onChange={(val) => {
$settings.service_settings[service].preserve_formatting = val;
translator.options.preserve_formatting = val;
}}
/>
</SettingItem>
{/if}

{#if info.options.formality}
<SettingItem
name="Formality"
description="How formal should the translation be"
type="dropdown"
notices={[
{type: "info", text: "Not all languages support formality", style: "translator-info-text"}
]}
>
<Dropdown
slot="control"
value={ $settings.service_settings[service].formality ?? "default" }
options={[
{value: "default", text: "Default"},
{value: "formal", text: "More formal"},
{value: "informal", text: "Less formal"}
]}
onChange={(e) => {
$settings.service_settings[service].formality = e.target.value;
translator.options.formality = e.target.value;
}}
/>
</SettingItem>
{/if}

{#if info.options.profanity_filter}
<SettingItem
name="Profanity filter"
description="If profanity should be filtered out of the translation"
type="dropdown"
>
<Dropdown
slot="control"
value={ $settings.service_settings[service].profanity_filter?.action ?? "none" }
options={[
{value: "none", text: "Do not filter profanity"},
{value: "mark", text: "Mask profanity with marker"},
{value: "delete", text: "Remove profanity"}
]}
onChange={(val) => {
let profanity = $settings.service_settings[service].profanity_filter ?? {};
profanity.action = val.target.value;
$settings.service_settings[service].profanity_filter = profanity;
translator.options.profanity_filter = profanity;
}}
/>
</SettingItem>

{#if $settings.service_settings[service].profanity_filter?.action === "mark"}
<SettingItem
name="Profanity marker"
description="The type of marker to use when masking profanity"
type="dropdown"
class="translator-setting-subsetting"
>
<Dropdown
slot="control"
value={ $settings.service_settings[service].profanity_filter?.marker ?? "mask" }
options={[
{value: "mask", text: "Mask"},
{value: "html-tag", text: "HTML tag"},
]}
onChange={(val) => {
$settings.service_settings[service].profanity_filter.marker = val.target.value;
translator.options.profanity_filter.marker = val.target.value;
}}
/>
</SettingItem>
{/if}
{/if}
{/if}

0 comments on commit 918404c

Please sign in to comment.