VScode 插件开发 国际化帮助工具

一、功能实现

// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const fs = require('fs');
const path = require('path')

const axios = require('axios');
const crypto = require('crypto');
const YOUDAO_API_URL = 'https://openapi.youdao.com/api';
const targetLanguage = 'zh'; // 目标语言,例如中文

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed

/**
 * @param {vscode.ExtensionContext} context
 */
function activate(context) {

	// Use the console to output diagnostic information (console.log) and errors (console.error)
	// This line of code will only be executed once when your extension is activated
	// console.log('Congratulations, your extension "gn-i18n-helper" is now active!');

	// The command has been defined in the package.json file
	// Now provide the implementation of the command with  registerCommand
	// The commandId parameter must match the command field in package.json
	const disposable = vscode.commands.registerCommand(
		'extension.getSelectedText',  // 这个名称必须与package.json中的命令名称一致
		async () => {
			const editor = vscode.window.activeTextEditor;
			if (editor) {
				// 选中的文本
				const selection = editor.selection;
				const selectedText = editor.document.getText(selection);

				// 获取当前工作区的文件夹
				const workspaceFolders = vscode.workspace.workspaceFolders;
				// 获取文件内容
				const filePath1 = path.join(workspaceFolders[0].uri.fsPath, 'LANGUAGEI18n/LANGUAGEI18nCommon.md');
				const filePath2 = path.join(workspaceFolders[0].uri.fsPath, 'LANGUAGEI18n/LANGUAGEI18nFrontend.md');
				const filePath3 = path.join(workspaceFolders[0].uri.fsPath, 'LANGUAGEI18n/LANGUAGEI18nPage.md');
				const filePath4 = path.join(workspaceFolders[0].uri.fsPath, 'LANGUAGEI18n/LANGUAGEI18nGeneral.md');
				const fileContent1 = fs.readFileSync(filePath1, 'utf8');
				const fileContent2 = fs.readFileSync(filePath2, 'utf8');
				const fileContent3 = fs.readFileSync(filePath3, 'utf8');
				const fileContent4 = fs.readFileSync(filePath4, 'utf8');
				let fileContent = fileContent1 + fileContent2 + fileContent3 + fileContent4;

				if (editor.document.languageId === 'vue') {
					const document = editor.document;
					const position = editor.selection.active; // 获取光标位置
					const lineText = document.lineAt(position.line).text; // 获取当前行文本
					const currentFileContent = document.getText(); // 获取文件内容
					
					// 默认 template,防止出现多个 template 标签
					// 使用正则表达式匹配  之间的内容
					let section = 'template';
					const javaScriptRegex = /
                    
                    

你可能感兴趣的:(插件,vscode,ide,编辑器)