Skip to content

Commit

Permalink
Independent API's for different languages, preperation for #49
Browse files Browse the repository at this point in the history
  • Loading branch information
phibr0 committed Sep 16, 2021
1 parent ac85900 commit 68650bd
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 28 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-dictionary-plugin",
"name": "Dictionary",
"version": "2.17.4",
"version": "2.17.5",
"minAppVersion": "0.12.11",
"description": "This is a simple dictionary for the Obsidian Note-Taking Tool.",
"author": "phibr0",
Expand Down
60 changes: 58 additions & 2 deletions src/_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,64 @@ export const DEFAULT_SETTINGS: DictionarySettings = {
defaultLanguage: "en_US",
shouldShowSynonymPopover: true,
shouldShowCustomContextMenu: false,
definitionApiName: "Free Dictionary API",
synonymApiName: "Free Dictionary API",
apiSettings: {
en_US: {
definitionApiName: 'Free Dictionary API',
synonymApiName: 'Free Dictionary API',
},
hi: {
definitionApiName: 'Free Dictionary API',
synonymApiName: null,
},
es: {
definitionApiName: 'Free Dictionary API',
synonymApiName: 'Altervista',
},
fr: {
definitionApiName: 'Free Dictionary API',
synonymApiName: 'Altervista',
},
ja: {
definitionApiName: 'Free Dictionary API',
synonymApiName: null,
},
ru: {
definitionApiName: 'Free Dictionary API',
synonymApiName: null,
},
en_GB: {
definitionApiName: 'Free Dictionary API',
synonymApiName: null,
},
de: {
definitionApiName: 'Free Dictionary API',
synonymApiName: 'Open Thesaurus',
},
it: {
definitionApiName: 'Free Dictionary API',
synonymApiName: 'Altervista',
},
ko: {
definitionApiName: 'Free Dictionary API',
synonymApiName: null,
},
pt_BR: {
definitionApiName: 'Free Dictionary API',
synonymApiName: null,
},
ar: {
definitionApiName: 'Free Dictionary API',
synonymApiName: null,
},
tr: {
definitionApiName: 'Free Dictionary API',
synonymApiName: null,
},
cn: {
definitionApiName: 'Offline Dictionary',
synonymApiName: null,
},
},
partOfSpeechApiName: "Systran API",
advancedSynonymAnalysis: false,
useCaching: false,
Expand Down
6 changes: 4 additions & 2 deletions src/apiManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,19 @@ export default class APIManager {
* @returns Returns the currently selected Definition API
*/
private getDefinitionAPI(): DefinitionProvider {
const lang = this.plugin.settings.defaultLanguage;
return this.definitionProvider.find(
(api) => api.name == this.plugin.settings.definitionApiName
(api) => api.name == this.plugin.settings.apiSettings[lang].definitionApiName
);
}

/**
* @returns Returns the currently selected Synonym API
*/
private getSynonymAPI(): SynonymProvider {
const lang = this.plugin.settings.defaultLanguage;
return this.synonymProvider.find(
(api) => api.name == this.plugin.settings.synonymApiName
(api) => api.name == this.plugin.settings.apiSettings[lang].synonymApiName
);
}

Expand Down
27 changes: 24 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { DictionaryWord, Synonym } from "src/integrations/types";

export interface DictionarySettings {
defaultLanguage: string;
definitionApiName: string;
synonymApiName: string;
defaultLanguage: keyof APISettings;
apiSettings: APISettings;
partOfSpeechApiName: string;
shouldShowSynonymPopover: boolean;
shouldShowCustomContextMenu: boolean;
Expand All @@ -17,6 +16,28 @@ export interface DictionarySettings {
template: string;
}

export interface APISettings {
en_US: APIPair;
hi: APIPair;
es: APIPair;
fr: APIPair;
ja: APIPair;
ru: APIPair;
en_GB: APIPair;
de: APIPair;
it: APIPair;
ko: APIPair;
pt_BR: APIPair;
ar: APIPair;
tr: APIPair;
cn: APIPair;
}

export interface APIPair {
definitionApiName: string | null;
synonymApiName: string | null;
}

export interface DictionaryCache {
cachedDefinitions: CachedDictionaryWord[];
cachedSynonyms: CachedSynonymCollection[];
Expand Down
10 changes: 6 additions & 4 deletions src/ui/modals/definitionProviderChooser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ export default class DefinitionProviderChooser extends FuzzySuggestModal<string>
constructor(app: App, plugin: DictionaryPlugin) {
super(app);
this.plugin = plugin;
this.plugin.manager.definitionProvider.forEach((api) => {
for(let i = 0; i < this.plugin.manager.definitionProvider.length; i++){
const api = this.plugin.manager.definitionProvider[i];
if (api.supportedLanguages.contains(this.plugin.settings.defaultLanguage)) {
this.available.push(api.name);
}
});
}
this.setPlaceholder(t("Choose a Definition Provider Service"));
}

onOpen(): void {
if (this.available.length <= 1) {
this.onChooseItem(this.available.first() ?? "");
this.onChooseItem(this.available.first() ?? null);
}
super.onOpen();
}
Expand All @@ -34,7 +35,8 @@ export default class DefinitionProviderChooser extends FuzzySuggestModal<string>
}

async onChooseItem(item: string): Promise<void> {
this.plugin.settings.definitionApiName = item;
const lang = this.plugin.settings.defaultLanguage;
this.plugin.settings.apiSettings[lang].definitionApiName = item;
await this.plugin.saveSettings();
this.close();
new SynonymProviderChooser(this.app, this.plugin).open();
Expand Down
4 changes: 1 addition & 3 deletions src/ui/modals/languageChooser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type DictionaryPlugin from "src/main";

import { App, FuzzySuggestModal } from "obsidian";
import { LANGUAGES } from "src/_constants";
import DefinitionProviderChooser from "src/ui/modals/definitionProviderChooser";
import t from "src/l10n/helpers";

export default class LanguageChooser extends FuzzySuggestModal<string>{
Expand Down Expand Up @@ -31,10 +30,9 @@ export default class LanguageChooser extends FuzzySuggestModal<string>{
}

async onChooseItem(item: string): Promise<void> {
this.plugin.settings.defaultLanguage = item;
this.plugin.settings.defaultLanguage = item as keyof typeof LANGUAGES;
await this.plugin.saveSettings();
this.close();
new DefinitionProviderChooser(this.app, this.plugin).open();
}

}
10 changes: 6 additions & 4 deletions src/ui/modals/synonymProviderChooser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ export default class SynonymProviderChooser extends FuzzySuggestModal<string>{
constructor(app: App, plugin: DictionaryPlugin) {
super(app);
this.plugin = plugin;
this.plugin.manager.synonymProvider.forEach((api) => {
for(let i = 0; i < this.plugin.manager.synonymProvider.length; i++){
const api = this.plugin.manager.synonymProvider[i];
if (api.supportedLanguages.contains(this.plugin.settings.defaultLanguage)) {
this.available.push(api.name);
}
});
}
this.setPlaceholder(t("Choose a Synonym Provider Service"));
}

onOpen(): void {
if (this.available.length <= 1) {
this.onChooseItem(this.available.first() ?? "");
this.onChooseItem(this.available.first() ?? null);
}
super.onOpen();
}
Expand All @@ -33,7 +34,8 @@ export default class SynonymProviderChooser extends FuzzySuggestModal<string>{
}

async onChooseItem(item: string): Promise<void> {
this.plugin.settings.synonymApiName = item;
const lang = this.plugin.settings.defaultLanguage;
this.plugin.settings.apiSettings[lang].synonymApiName = item;
await this.plugin.saveSettings();
this.close();
}
Expand Down
22 changes: 13 additions & 9 deletions src/ui/settings/settingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class SettingsTab extends PluginSettingTab {
}
dropdown.setValue(plugin.settings.defaultLanguage)
.onChange(async (value) => {
plugin.settings.defaultLanguage = value;
plugin.settings.defaultLanguage = value as keyof typeof LANGUAGES;
await this.save();
this.display();
});
Expand All @@ -38,29 +38,33 @@ export default class SettingsTab extends PluginSettingTab {
.setName(t('Definition Provider'))
.setDesc(t('The API the Plugin will use to search for Definitions.'))
.addDropdown((dropdown) => {
const list: Record<string, string> = {};
for (const api of plugin.manager.definitionProvider) {
if (api.supportedLanguages.contains(plugin.settings.defaultLanguage)) {
dropdown.addOption(api.name, api.name);
list[api.name] = api.name;
}
}
dropdown.setValue(plugin.settings.definitionApiName)
dropdown.addOptions(list)
.setValue(plugin.settings.apiSettings[plugin.settings.defaultLanguage].definitionApiName ?? Object.keys(list).first())
.onChange(async (value) => {
plugin.settings.definitionApiName = value;
plugin.settings.apiSettings[plugin.settings.defaultLanguage].definitionApiName = value;
await this.save();
});
});
new Setting(containerEl)
.setName(t('Synonym Provider'))
.setDesc(t('The API the Plugin will use to search for Synonyms.'))
.addDropdown((dropdown) => {
const list: Record<string, string> = {};
for (const api of plugin.manager.synonymProvider) {
if (api.supportedLanguages.contains(plugin.settings.defaultLanguage)) {
dropdown.addOption(api.name, api.name);
list[api.name] = api.name;
}
}
dropdown.setValue(plugin.settings.synonymApiName)
dropdown.addOptions(list)
.setValue(plugin.settings.apiSettings[plugin.settings.defaultLanguage].synonymApiName ?? Object.keys(list).first())
.onChange(async (value) => {
plugin.settings.synonymApiName = value;
plugin.settings.apiSettings[plugin.settings.defaultLanguage].synonymApiName = value;
await this.save();
});
});
Expand All @@ -80,7 +84,7 @@ export default class SettingsTab extends PluginSettingTab {
})
});

if(plugin.manager.partOfSpeechProvider.length) {
if (plugin.manager.partOfSpeechProvider.length) {
const desc = document.createDocumentFragment();
desc.append(
t('Enabling this will allow the Plugin to analyze full sentences to better suggest synonyms based on the context.'),
Expand All @@ -97,7 +101,7 @@ export default class SettingsTab extends PluginSettingTab {
.setDesc(desc)
.addToggle(toggle => {
toggle.setValue(plugin.settings.advancedSynonymAnalysis)

toggle.onChange(async (value) => {
plugin.settings.advancedSynonymAnalysis = value;
await this.save();
Expand Down

0 comments on commit 68650bd

Please sign in to comment.