diff --git a/src/_constants.ts b/src/_constants.ts index 72f8bd8..c0e3def 100644 --- a/src/_constants.ts +++ b/src/_constants.ts @@ -46,7 +46,9 @@ export const DEFAULT_CACHE: DictionaryCache = { } export const DEFAULT_SETTINGS: DictionarySettings = { + getLangFromFile: true, defaultLanguage: "en_US", + normalLang: 'en_US', shouldShowSynonymPopover: true, shouldShowCustomContextMenu: false, apiSettings: { diff --git a/src/main.ts b/src/main.ts index 7986c76..7748cc5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,4 @@ -import { DEFAULT_CACHE } from './_constants'; +import { DEFAULT_CACHE, LANGUAGES } from './_constants'; /* eslint-disable @typescript-eslint/ban-ts-comment */ /* eslint-disable @typescript-eslint/no-explicit-any */ import type { DictionaryCache, DictionarySettings } from 'src/types'; @@ -129,6 +129,22 @@ export default class DictionaryPlugin extends Plugin { // Editor mode // @ts-ignore this.registerEvent(this.app.workspace.on('editor-menu', this.handleContextMenuHelper)); + + this.registerEvent(this.app.workspace.on('file-open', async (file) => { + if (this.settings.getLangFromFile) { + let lang = this.app.metadataCache.getFileCache(file).frontmatter?.lang; + if (!lang) { + lang = this.app.metadataCache.getFileCache(file).frontmatter?.language; + } + if (lang && Object.keys(LANGUAGES).contains(lang)) { + this.settings.defaultLanguage = lang; + await this.saveSettings(); + } else { + this.settings.defaultLanguage = this.settings.normalLang; + await this.saveSettings(); + } + } + })); } onunload(): void { diff --git a/src/types.ts b/src/types.ts index 0d8c00b..f980332 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,6 +2,8 @@ import type { DictionaryWord, Synonym } from "src/integrations/types"; export interface DictionarySettings { defaultLanguage: keyof APISettings; + normalLang: keyof APISettings; + getLangFromFile: boolean; apiSettings: APISettings; partOfSpeechApiName: string; shouldShowSynonymPopover: boolean; diff --git a/src/ui/modals/languageChooser.ts b/src/ui/modals/languageChooser.ts index 2888368..c25afed 100644 --- a/src/ui/modals/languageChooser.ts +++ b/src/ui/modals/languageChooser.ts @@ -31,6 +31,7 @@ export default class LanguageChooser extends FuzzySuggestModal{ async onChooseItem(item: string): Promise { this.plugin.settings.defaultLanguage = item as keyof typeof LANGUAGES; + this.plugin.settings.normalLang = item as keyof typeof LANGUAGES; await this.plugin.saveSettings(); this.close(); } diff --git a/src/ui/settings/settingsTab.ts b/src/ui/settings/settingsTab.ts index 6370aa9..a074b47 100644 --- a/src/ui/settings/settingsTab.ts +++ b/src/ui/settings/settingsTab.ts @@ -30,10 +30,21 @@ export default class SettingsTab extends PluginSettingTab { dropdown.setValue(plugin.settings.defaultLanguage) .onChange(async (value) => { plugin.settings.defaultLanguage = value as keyof typeof LANGUAGES; + plugin.settings.normalLang = value as keyof typeof LANGUAGES; await this.save(); this.display(); }); }); + new Setting(containerEl) + .setName('Get Language from File') + .setDesc('Use the lang or language key in the frontmatter to set the Language dependent on your open File. Example: lang: "en_US" or language: "de".') + .addToggle(toggle => { + toggle.setValue(this.plugin.settings.getLangFromFile).onChange(async value => { + this.plugin.settings.getLangFromFile = value; + await this.plugin.saveSettings(); + }); + }); + new Setting(containerEl) .setName(t('Definition Provider')) .setDesc(t('The API the Plugin will use to search for Definitions.'))