diff --git a/src/commands.ts b/src/commands.ts index 291bc00..7cbf51e 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -5,6 +5,7 @@ import { state } from './state'; import { showMessageWithTimeout } from './vscode'; import { showApiKeyInput, showSourceLanguageInput, showTargetLanguageInput, showUseProInput } from "./inputs"; import { TranslateCommandParam, TranslateParam } from './types'; +import { getDefaultSourceLanguage, getDefaultTargetLanguage } from './helper'; function translateSelections(selections: vscode.Selection[], translateParam: TranslateParam): Thenable { const { targetLang, sourceLang, below } = translateParam; @@ -73,15 +74,18 @@ function createTranslateCommand(param: TranslateCommandParam) { ? state.sourceLanguage : null; if (askForSourceLang && sourceLang) { - state.sourceLanguage = sourceLang; + state.sourceLanguage = sourceLang ?? getDefaultSourceLanguage(); } if (askForTargetLang || !state.targetLanguage) { - state.targetLanguage = await showTargetLanguageInput(); + const targetLanguage = await showTargetLanguageInput(); - if (!state.targetLanguage) { + if (!targetLanguage) { + state.targetLanguage = getDefaultTargetLanguage(); return; } + + state.targetLanguage = targetLanguage; } const selections = vscode.window.activeTextEditor?.selections?.filter(selection => !selection.isEmpty); diff --git a/src/helper.ts b/src/helper.ts new file mode 100644 index 0000000..0d69141 --- /dev/null +++ b/src/helper.ts @@ -0,0 +1,11 @@ +import * as vscode from 'vscode'; + +export function getDefaultTargetLanguage(config?: vscode.WorkspaceConfiguration): string | null { + config = config ?? vscode.workspace.getConfiguration('deepl'); + return config.get('defaultTargetLanguage', null); +} + +export function getDefaultSourceLanguage(config?: vscode.WorkspaceConfiguration): string | null { + config = config ?? vscode.workspace.getConfiguration('deepl'); + return config.get('defaultSourceLanguage', null); +} \ No newline at end of file diff --git a/src/state.ts b/src/state.ts index 37fb332..8fae245 100644 --- a/src/state.ts +++ b/src/state.ts @@ -2,6 +2,7 @@ import * as vscode from 'vscode'; import * as debug from './debug'; import { ExtensionState } from './types'; import { reactive, watch, ref } from 'vue'; +import { getDefaultSourceLanguage, getDefaultTargetLanguage } from './helper'; const initialized = ref(false); @@ -43,8 +44,8 @@ export function setup(context: vscode.ExtensionContext) { state.nonSplittingTags = config.get('nonSplittingTags') ?? ""; state.preserveFormatting = config.get('preserveFormatting') ?? false; state.glossaryId = config.get('glossaryId') ?? ""; - state.targetLanguage = context.workspaceState.get('deepl:targetLanguage') ?? config.get('defaultTargetLanguage', null); - state.sourceLanguage = context.workspaceState.get('deepl:sourceLanguage') ?? config.get('defaultSourceLanguage', null); + state.targetLanguage = context.workspaceState.get('deepl:targetLanguage') ?? getDefaultTargetLanguage(config); + state.sourceLanguage = context.workspaceState.get('deepl:sourceLanguage') ?? getDefaultSourceLanguage(config); debug.write(`Initialized extension using state:`); debug.write(JSON.stringify(state, null, 2));