Skip to content

Commit

Permalink
feat: allow user to filter fasttext service from settings too (fastte…
Browse files Browse the repository at this point in the history
…xt will automatically be added to filtered_services to prevent the service being automatically hidden) (#40)
  • Loading branch information
Fevol committed Feb 2, 2023
1 parent 918404c commit 3438d8c
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 39 deletions.
3 changes: 2 additions & 1 deletion src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ others simple objects. The currently used stores are:
- `settings`: Persistent plugin settings, changes made to this store are automatically saved to the `data.json`
- `all_languages`: Key-value object of language locales to their display names (e.g. `en: English`)
- `available_languages`: The languages available for the **global translator**, changes based on filters set
- `available_services`: Services that the user can access, depends on platform and user-set settings
- `available_translator_services`: Translator services that the user can access, depends on platform and user-set settings
- `available_detector_services`: Language detector services that the user can access, depends on platform and user-set settings
- `spellcheck_languages`: Obsidian's set spellchecker languages, used as a filter on all languages
- `fasttext_data`: FastText model data, used for language detection
- `bergamot_data`: Bergamot model data, used for translation
Expand Down
10 changes: 8 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const ICONS = {
"azure_translator_attribution": "<svg width=\"100\" height=\"21.259\" viewBox=\"0 0 100 21.259\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\"><g transform=\"matrix(.21694 0 0 .21694 -5.641 -44.908)\" enable-background=\"new\"><path d=\"m265.15 250.56c4.144-0.521 8.277 1.053 11.536 3.536 0.03-3.214-0.025-6.429 0.03-9.64-0.904-0.529-1.877-0.918-2.882-1.198-4.875-1.375-10.136-1.431-15.013-0.045-4.477 1.288-8.585 4.029-11.212 7.901-3.135 4.538-4.243 10.207-3.997 15.642 0.244 5.229 2.31 10.446 6.116 14.106 3.502 3.394 8.328 5.222 13.166 5.477 4.729 0.232 9.682-0.4 13.814-2.854-0.037-3.043 6e-3 -6.087-0.022-9.13-2.663 1.942-5.773 3.334-9.068 3.713-3.335 0.251-6.907-0.549-9.455-2.809-2.453-2.086-3.758-5.229-4.084-8.377-0.435-4.021 0.149-8.386 2.678-11.673 1.968-2.65 5.104-4.352 8.393-4.649zm-34.141-25.211c-2.807 0.319-5.201 2.864-5.125 5.72-0.118 2.713 2.069 5.146 4.693 5.617 2.529 0.551 5.43-0.516 6.727-2.812 1.173-2.035 0.882-4.808-0.702-6.549-1.35-1.59-3.556-2.284-5.593-1.976zm-27.805 1.282c-5.84 14.501-11.81 28.944-17.695 43.425-1.227-2.876-2.321-5.807-3.491-8.704-4.58-11.589-9.189-23.166-13.763-34.754h-14.698c-6e-3 19.598-3e-3 39.197 0 58.796 3.205-3e-3 6.413 5e-3 9.621-3e-3 -8e-3 -8.774 0-17.549-2e-3 -26.324 0.034-6.336-0.07-12.678 0.053-19.013 0.182 0.287 0.351 0.587 0.508 0.896 5.995 14.813 11.945 29.649 17.97 44.452 2.422-0.039 4.844 0.042 7.264-0.045 5.807-14.816 11.759-29.577 17.6-44.38 0.155-0.324 0.332-0.638 0.531-0.94 0.045 0.649 0.067 1.299 0.062 1.949-0.019 14.464 0.02 28.925-0.016 43.386 3.398 0.048 6.797 0.011 10.199 0.02-3e-3 -19.596-8e-3 -39.192 3e-3 -58.788-4.718 9e-3 -9.434-0.048-14.146 0.027zm96.111 17.268c-2.534 1.432-4.143 3.979-5.223 6.603 9e-3 -2.414-6e-3 -4.824 9e-3 -7.235-3.326-0.016-6.652-0.033-9.979 9e-3 0.02 14.039 0 28.08 9e-3 42.118h9.964c2e-3 -6.717 0-13.434 0-20.151-0.082-2.917 0.159-5.935 1.439-8.603 1.178-2.521 3.451-4.718 6.32-5.043 2.352-0.238 4.838 0.313 6.831 1.602 8e-3 -3.363-6e-3 -6.725 8e-3 -10.088-3.044-1.022-6.589-0.883-9.378 0.788zm-72.513-0.633c9e-3 14.044-2e-3 28.086 6e-3 42.127h10.033c0.014-14.036-0.022-28.073 0.017-42.109-3.351-0.064-6.705-0.031-10.056-0.018zm151.68 19.158c-2.97-1.714-6.388-2.525-9.253-4.438-1.676-1.062-2.149-3.386-1.465-5.158 0.861-1.791 2.938-2.59 4.827-2.662 3.75-0.132 7.524 1.008 10.614 3.128-3e-3 -3.089 3e-3 -6.177-3e-3 -9.268-4.423-1.672-9.314-2.273-13.988-1.453-3.948 0.702-7.824 2.749-10.056 6.162-1.953 2.929-2.205 6.705-1.397 10.057 0.545 2.273 1.937 4.289 3.775 5.72 2.615 2.002 5.745 3.119 8.723 4.455 1.586 0.745 3.267 1.504 4.412 2.88 1.224 1.788 0.853 4.708-1.208 5.729-2.151 1.03-4.664 1.013-6.974 0.639-3.317-0.571-6.452-1.98-9.17-3.948-0.036 3.248-2e-3 6.496-0.016 9.746 3.229 1.434 6.761 2.125 10.274 2.353 4.948 0.185 10.328-0.779 14.063-4.262 3.913-3.449 4.855-9.562 2.63-14.187-1.182-2.47-3.45-4.184-5.788-5.493zm44.361-17.916c-4.603-2.319-9.957-2.657-15-2.033-4.067 0.521-8.063 2.061-11.168 4.779-3.789 3.257-6.012 8.044-6.666 12.941-0.623 4.896-0.412 10.054 1.589 14.637 1.92 4.497 5.708 8.148 10.267 9.937 4.498 1.768 9.5 1.965 14.234 1.201 4.842-0.797 9.428-3.337 12.425-7.246 3.203-4.037 4.58-9.262 4.571-14.352 0.07-3.407-0.355-6.862-1.571-10.063-1.555-4.182-4.667-7.799-8.681-9.801zm-0.435 24.087c-0.388 2.218-1.156 4.429-2.612 6.185-1.469 1.764-3.652 2.839-5.906 3.169-2.419 0.361-4.967 0.188-7.229-0.784-2.441-1.03-4.333-3.138-5.329-5.563-1.199-2.889-1.438-6.09-1.199-9.18 0.23-2.994 1.081-6.065 3.063-8.392 1.808-2.172 4.58-3.373 7.376-3.53 2.854-0.198 5.902 0.46 8.097 2.383 2.023 1.675 3.144 4.183 3.657 6.708 0.576 2.961 0.57 6.026 0.082 9.004zm-80.649-24.404c-4.501-2.044-9.618-2.318-14.468-1.703-5.094 0.642-10.053 3.032-13.286 7.084-2.953 3.617-4.39 8.265-4.664 12.876-0.408 5.436 0.502 11.202 3.701 15.738 2.726 3.914 7.027 6.604 11.676 7.596 3.884 0.814 7.947 0.817 11.832 0.011 4.544-0.932 8.787-3.44 11.603-7.142 2.75-3.485 4.131-7.882 4.442-12.269 0.317-4.494-0.138-9.148-2.01-13.291-1.754-3.892-4.919-7.142-8.826-8.9zm-0.935 28.242c-0.966 2.167-2.733 3.998-4.965 4.866-2.61 1-5.543 1.059-8.237 0.361-2.742-0.719-5.068-2.696-6.334-5.214-1.629-3.241-1.872-6.99-1.536-10.546 0.307-3.234 1.435-6.56 3.904-8.792 1.792-1.668 4.233-2.483 6.649-2.618 3.036-0.198 6.27 0.658 8.452 2.861 2.336 2.291 3.239 5.613 3.489 8.783 0.181 3.47 0.037 7.085-1.422 10.299zm145.17-20.993v-8.188c-3.334 0.012-6.669-0.025-10 0.022-0.076-4.208-9e-3 -8.416-0.037-12.621-3.334 1.002-6.648 2.069-10 3.021-0.08 3.192-5e-3 6.387-0.042 9.578-4.928-2e-3 -9.856 6e-3 -14.785-5e-3 0.063-2.515-0.173-5.048 0.188-7.549 0.3-1.971 1.445-3.941 3.371-4.704 2.428-0.993 5.142-0.333 7.448 0.681-0.014-2.881 0.022-5.762-0.02-8.64-4.854-1.333-10.421-1.072-14.729 1.725-2.778 1.774-4.794 4.614-5.7 7.761-1.071 3.486-0.525 7.168-0.696 10.745-2.338-0.016-4.673-0.039-7.005 0.015 0.024 2.693 0.015 5.387 6e-3 8.077 2.343 0.048 4.688-9e-3 7.032 0.037-0.02 11.333-5e-3 22.664-5e-3 33.998h10.106c0-11.337-3e-3 -22.674 2e-3 -34.01 4.929-8e-3 9.86-6e-3 14.792-3e-3 0.014 7.227-0.015 14.45 0.011 21.674 0.045 3.364 0.62 6.963 2.854 9.619 1.935 2.353 5.004 3.428 7.968 3.646 3.124 0.221 6.352-0.16 9.24-1.411v-8.269c-2.215 1.479-5.273 2.066-7.686 0.72-1.438-0.792-1.998-2.489-2.183-4.021-0.306-2.599-0.112-5.219-0.166-7.826-3e-3 -4.708 9e-3 -9.415-5e-3 -14.122 3.347 0.013 6.695-0.087 10.041 0.05z\" clip-rule=\"evenodd\" fill=currentColor fill-rule=\"evenodd\"/>\t<path d=\"m26.002 207v46.617h46.726c3e-3 -15.539 0-31.078 3e-3 -46.617z\" clip-rule=\"evenodd\" fill=\"#f25022\" fill-rule=\"evenodd\"/>\t<path d=\"m26.002 258.38v46.62h46.729c0-15.539 3e-3 -31.075 0-46.614-15.576-8e-3 -31.152 0-46.729-6e-3z\" clip-rule=\"evenodd\" fill=\"#02a4ef\" fill-rule=\"evenodd\"/>\t<path d=\"m77.506 258.38c-6e-3 15.536 0 31.075-3e-3 46.614h46.729v-46.617c-15.577 0-31.153-3e-3 -46.726 3e-3z\" clip-rule=\"evenodd\" fill=\"#ffb902\" fill-rule=\"evenodd\"/>\t<path d=\"m77.503 207c0 15.539-3e-3 31.078 3e-3 46.617 15.573 0 31.149-3e-3 46.723 0 6e-3 -15.539 3e-3 -31.078 3e-3 -46.617z\" clip-rule=\"evenodd\" fill=\"#80ba01\" fill-rule=\"evenodd\"/></g></svg>\n",
}

export let ALL_SERVICES: string[] = [
export let ALL_TRANSLATOR_SERVICES: string[] = [
"google_translate",
"azure_translator",
"yandex_translate",
Expand All @@ -36,10 +36,16 @@ export let ALL_SERVICES: string[] = [
"fanyi_youdao",
"fanyi_baidu",
"lingva_translate",
// "fasttext",
]

export let ALL_DETECTOR_SERVICES: string[] = [
"fasttext",
]

export let ALL_SERVICES = [...ALL_TRANSLATOR_SERVICES, ...ALL_DETECTOR_SERVICES]

export const DEFAULT_SETTINGS: TranslatorPluginSettings = {
version: "1.4.5",
display_language: 'display',
filter_mode: "0",
translation_service: 'google_translate',
Expand Down
11 changes: 11 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ export default class TranslatorPlugin extends Plugin {
*/
const set_if_exists = Object.keys(SERVICES_INFO).filter(key => translation_service !== key);

// TODO: Version field was introduced in 1.4.5
// (Changed loaded_settings to also filter fasttext, ensure that it is not immediately filtered away even if used,
// this patch will only be here for a few versions)
if (!loaded_settings?.version && loaded_settings.filtered_services) {
loaded_settings.filtered_services = [...loaded_settings.filtered_services, 'fasttext']
}

/** Adds newly introduced settings to the data.json if they're not already there, this ensures that older settings
* are forwards compatible with newer versions of the plugin
*/
Expand All @@ -148,6 +155,10 @@ export default class TranslatorPlugin extends Plugin {
loaded_settings.local_glossary = undefined;
}

// Update the version number in the data.json, only saved if the settings get changed
loaded_settings.version = DEFAULT_SETTINGS.version;



// @ts-ignore (path exists in legacy versions)
if (loaded_settings.storage_path) {
Expand Down
11 changes: 8 additions & 3 deletions src/stores.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {writable, type Writable} from "svelte/store";
import type {TranslatorPluginSettings, ModelFileData} from "./types";
import type TranslatorPlugin from "./main";
import {ALL_SERVICES} from "./constants";
import {ALL_DETECTOR_SERVICES, ALL_TRANSLATOR_SERVICES} from "./constants";

/**
* The store for the plugin settings.
Expand All @@ -19,9 +19,14 @@ export const all_languages = writable<Map<string, string>>(new Map());
export const available_languages = writable<string[]>([]);

/**
* Set of services that can be accessed in settings, commands, etc.
* Set of translator services that can be accessed in settings, commands, etc.
*/
export const available_services = writable<string[]>(ALL_SERVICES);
export const available_translator_services = writable<string[]>(ALL_TRANSLATOR_SERVICES);

/**
* Set of detector services that can be accessed in settings, commands, etc.
*/
export const available_detector_services = writable<string[]>(ALL_DETECTOR_SERVICES);

/**
* Set of languages that are used for spellchecking Obsidian
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import type {ServiceOptions} from "./handlers/types";
* Object containing all plugin settings
*/
export interface TranslatorPluginSettings {
/**
* Version of the settings file
*/
version: string;

/**
* How API keys are stored on the device
* - "none" - API keys are stored in plaintext in data.json
Expand Down
7 changes: 4 additions & 3 deletions src/ui/modals/ViewFunctionalityModalView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import {createEventDispatcher} from "svelte";
import {SERVICES_INFO} from "../../constants";
import {available_services} from "../../stores";
import {available_translator_services} from "../../stores";
const dispatch = createEventDispatcher()
Expand All @@ -25,11 +25,12 @@
>
<Dropdown
slot="control"
options={$available_services
options={$available_translator_services
//.filter(service => SERVICES_INFO[service].type === 'translation')
.map(service => {
return {'value': service, 'text': SERVICES_INFO[service].display_name};
})}
})
}
value={ translation_service }
onChange={(e) => { translation_service = e.target.value; }}
/>
Expand Down
7 changes: 2 additions & 5 deletions src/ui/modals/switch-service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FuzzySuggestModal, App } from "obsidian";
import type TranslatorPlugin from "main";
import {SERVICES_INFO} from "../../constants";
import {available_services} from "../../stores";
import {available_translator_services} from "../../stores";
import {get} from "svelte/store";

export default class SwitchService extends FuzzySuggestModal<string>{
Expand All @@ -13,10 +13,7 @@ export default class SwitchService extends FuzzySuggestModal<string>{
super(app);
this.plugin = plugin;

const loaded_available_services = get(available_services);

this.options = loaded_available_services
//.filter(service => SERVICES_INFO[service].type === 'translation')
this.options = get(available_translator_services)
.map(service => { return {'value': service, 'label': SERVICES_INFO[service].display_name} })
.sort((a, b) => a.label.localeCompare(b.label));

Expand Down
22 changes: 14 additions & 8 deletions src/ui/translator-components/Reactivity.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
spellcheck_languages,
all_languages,
password,
bergamot_data, fasttext_data, available_services, passwords_are_encrypted
bergamot_data,
fasttext_data,
available_translator_services,
passwords_are_encrypted,
available_detector_services
} from "../../stores";
import {SERVICES_INFO, DEFAULT_SETTINGS, ALL_SERVICES} from "../../constants";
import {SERVICES_INFO, DEFAULT_SETTINGS, ALL_TRANSLATOR_SERVICES, ALL_SERVICES} from "../../constants";
import ISO6391 from "iso-639-1";
import t from "../../l10n";
Expand Down Expand Up @@ -190,7 +194,7 @@
*/
export async function getTranslationService(service: string, old_service: string = ''): Promise<DummyTranslate> {
// Do not attempt to create a service if it does not exist
if (!service || !(service in SERVICES_INFO)) {
if (!service || !(service in SERVICES_INFO) || ($settings.filtered_services && !$settings.filtered_services.contains(service))) {
return null;
}
Expand Down Expand Up @@ -323,9 +327,11 @@
new_available_services = new_available_services.filter(service => !SERVICES_INFO[service].desktop_only);
if ($settings.filtered_services.length)
new_available_services = new_available_services.filter(service => $settings.filtered_services.includes(service));
$available_services = new_available_services;
if (!$available_services.includes($settings.translation_service))
$settings.translation_service = $available_services[0];
$available_translator_services = new_available_services.filter(service => SERVICES_INFO[service].type === 'translation');
$available_detector_services = new_available_services.filter(service => SERVICES_INFO[service].type === 'detection');
if (!$available_translator_services.includes($settings.translation_service))
$settings.translation_service = $available_translator_services[0];
}
onMount(async () => {
Expand Down Expand Up @@ -381,8 +387,8 @@
}
filterAvailableServices();
if (!plugin.detector && ($settings.apply_glossary || $settings.service_settings?.fasttext?.default_usage)) {
// TODO: Replace 'fasttext' with settings detector service
if ($available_detector_services.contains('fasttext') && !plugin.detector && ($settings.apply_glossary || $settings.service_settings?.fasttext?.default_usage)) {
plugin.detector = await getTranslationService('fasttext');
if ($settings.service_settings?.fasttext?.default_usage)
plugin.detector.default = true;
Expand Down
26 changes: 14 additions & 12 deletions src/ui/translator-components/SettingsPage.svelte
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<script lang="ts">
import TranslatorPlugin from "../../main";
import {available_services, settings, settings_tab} from "../../stores";
import {available_translator_services, available_detector_services, settings, settings_tab} from "../../stores";
import {slide} from "svelte/transition";
import {horizontalSlide} from "../animations";
import {Icon} from ".././components";
import {SERVICES_INFO, SETTINGS_TABS, ALL_SERVICES} from "../../constants";
import {SERVICES_INFO, SETTINGS_TABS, ALL_SERVICES, ALL_TRANSLATOR_SERVICES} from "../../constants";
import {DummyTranslate} from "../../handlers";
import {
Expand All @@ -27,13 +27,13 @@
let tabs = generateTabs();
let tab_idx = tabs.findIndex(t => t.id === $settings_tab);
$: $available_services, tabs = generateTabs();
$: $available_translator_services, $available_detector_services, tabs = generateTabs();
function generateTabs() {
return [
...SETTINGS_TABS,
...$available_services.map(service => ({id: service, name: SERVICES_INFO[service].display_name, icon: service})),
{id: "fasttext", name: "FastText", icon: "fasttext"}
...$available_translator_services.map(service => ({id: service, name: SERVICES_INFO[service].display_name, icon: service})),
...$available_detector_services.map(service => ({id: service, name: SERVICES_INFO[service].display_name, icon: service})),
];
}
Expand Down Expand Up @@ -89,13 +89,15 @@
on:contextmenu={e => {
if (ALL_SERVICES.includes(id)) {
let menu = new Menu();
menu.addItem((item) => {
item.setTitle("Set as default")
.setIcon("translate")
.onClick((e) => {
$settings.translation_service = id
})
});
if (ALL_TRANSLATOR_SERVICES.contains(id)) {
menu.addItem((item) => {
item.setTitle("Set as default")
.setIcon("translate")
.onClick((e) => {
$settings.translation_service = id
})
});
}
menu.addItem((item) => {
item.setTitle("Hide service")
.setIcon("eye-off")
Expand Down
5 changes: 3 additions & 2 deletions src/ui/translator-components/ViewPage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
glossary,
settings_tab,
spellcheck_languages,
bergamot_data, fasttext_data, available_services, all_languages
bergamot_data, fasttext_data, available_translator_services, all_languages
} from "../../stores";
import {horizontalSlide} from "../animations";
Expand Down Expand Up @@ -322,8 +322,9 @@
}
onMount(() => {
if (!$available_services.includes($translation_service))
if (!$available_translator_services.includes($translation_service))
$translation_service = $settings.translation_service;
// TODO: Make user aware of translation service not existing?
})
onDestroy(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import {
all_languages,
available_services,
available_translator_services,
password,
passwords_are_encrypted,
settings,
Expand All @@ -16,7 +16,11 @@
import {PasswordModal, PasswordRequestModal, TextModal} from "../../modals";
import {SERVICES_INFO, SECURITY_MODES, ALL_SERVICES} from "../../../constants";
import {
SERVICES_INFO,
SECURITY_MODES,
ALL_SERVICES
} from "../../../constants";
import {openGithubIssueLink} from "../../../obsidian-util";
export let plugin: TranslatorPlugin;
Expand Down Expand Up @@ -77,7 +81,7 @@
>
<div slot="control" class="translator-flex-row-element">
<Dropdown
options={$available_services
options={$available_translator_services
//.filter(service => SERVICES_INFO[service].type === 'translation')
.map(service => {
return {'value': service, 'text': SERVICES_INFO[service].display_name};
Expand Down

0 comments on commit 3438d8c

Please sign in to comment.