diff --git a/public/scripts/extensions/translate/index.js b/public/scripts/extensions/translate/index.js index dbc8ab587..20519db5b 100644 --- a/public/scripts/extensions/translate/index.js +++ b/public/scripts/extensions/translate/index.js @@ -12,6 +12,9 @@ import { } from '../../../script.js'; import { extension_settings, getContext } from '../../extensions.js'; import { findSecret, secret_state, writeSecret } from '../../secrets.js'; +import { SlashCommand } from '../../slash-commands/SlashCommand.js'; +import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '../../slash-commands/SlashCommandArgument.js'; +import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js'; import { splitRecursive } from '../../utils.js'; export const autoModeOptions = { @@ -649,4 +652,21 @@ jQuery(() => { eventSource.on(event_types.MESSAGE_UPDATED, handleMessageEdit); document.body.classList.add('translate'); + + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ + name: 'translate', + helpString: 'Translate text to a target language. If target language is not provided, the value from the extension settings will be used.', + namedArgumentList: [ + new SlashCommandNamedArgument('target', 'The target language code to translate to', ARGUMENT_TYPE.STRING, false, false, '', Object.values(languageCodes)), + ], + unnamedArgumentList: [ + new SlashCommandArgument('The text to translate', ARGUMENT_TYPE.STRING, true, false, ''), + ], + callback: async (args, value) => { + const target = args?.target && Object.values(languageCodes).includes(String(args.target)) + ? String(args.target) + : extension_settings.translate.target_language; + return await translate(String(value), target); + }, + })); });