Code text translation method, device and computer program product
By using abstract syntax tree technology to parse code text, generate a list of terms, and call the translation API, the high labor costs and high error rates of existing code translation methods are solved, achieving efficient and accurate code translation.
Patent Information
- Application Number
- CN202511000711.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-21
- Publication Date
- 2025-11-28
AI Technical Summary
Existing code translation methods are labor-intensive and prone to translation errors, which affects software development efficiency.
The code text is parsed using abstract syntax tree technology to generate a list of terms to be translated. After the user confirms the translation through a preset page interaction component, the preset translation API interface is called to translate the text, and finally the code file is updated.
It improves the accuracy and efficiency of code translation, reduces the workload of manual verification and secondary modification, ensures that the translation results are consistent with the semantic context of the code, and enhances the convenience and flexibility of software development.
Smart Images

Figure CN121031618A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer, in particular to a code text translation method, device and computer program product. BACKGROUND
[0002] In today's global software development wave, the demand for multilingual translation of code is increasingly prominent. Whether it is a business application committed to opening up international markets or an open source project with cross-border collaboration, developers face the urgent need to accurately and efficiently convert code text into different languages. Code text translation has gradually evolved from an optional auxiliary work to a key link in the software development cycle.
[0003] However, in the existing manner, when developers handle multilingual translation tasks in the software development process, they usually need to extract the translatable text in the software code one by one for translation through a pre-edited JSON word library or using external tools. This not only greatly increases the labor cost, but also easily leads to translation errors or omissions due to human negligence, thereby affecting software development and maintenance efficiency. SUMMARY
[0004] The main purpose of the present application is to provide a code text translation method, device and computer program product, which aims to solve the technical problems of existing code translation methods that consume labor costs and are prone to translation errors, affecting software development efficiency.
[0005] To achieve the above-mentioned purpose, the present application provides a code text translation method, which comprises:
[0006] Obtaining a current code file and determining a to-be-translated code text in the current code file;
[0007] Using abstract syntax tree technology to parse the to-be-translated code text, generating a to-be-translated term list according to the parsing result, and showing the to-be-translated term list to the user through a preset page interaction component;
[0008] When receiving the confirmation instruction of the user, calling a preset translation API interface to perform text translation on the to-be-translated term list;
[0009] Updating the current code file according to the text translation result to obtain a translated code file.
[0010] In an embodiment, the step of obtaining a current code file and determining a to-be-translated code text in the current code file comprises:
[0011] Importing a current code file in a VSCode editor and determining a corresponding to-be-translated code text in response to a code translation operation triggered by a user in the VSCode editor.
[0012] The code translation operation includes a selected text translation operation or a full text translation operation, and the code text to be translated is part of the code text in the current code file or is the entire code text.
[0013] In an embodiment, the parsing of the code text to be translated using the abstract syntax tree technique and the generation of a list of words to be translated based on the parsing result include:
[0014] The code text to be translated is parsed using the abstract syntax tree (AST) technique to generate a plurality of AST nodes.
[0015] Each of the AST nodes is traversed to filter out AST nodes containing translatable text.
[0016] Each of the AST nodes containing translatable text is subjected to duplicate item filtering to obtain target AST nodes, and a list of words to be translated is generated based on each of the target AST nodes.
[0017] In an embodiment, the preset page interaction component is a WebView interface, and the step of displaying the list of words to be translated to the user through the preset page interaction component includes:
[0018] The WebView interface is popped up in the VSCode editor, and the list of words to be translated and the list of words to be translated are displayed to the user through the WebView interface, so that the user can adjust the code text to be translated and / or the list of words to be translated, and trigger a confirmation instruction based on the adjusted code text to be translated and / or the adjusted list of words to be translated.
[0019] In an embodiment, when the user's confirmation instruction is received, a preset translation API interface is called to perform text translation on the list of words to be translated.
[0020] When the user's confirmation instruction is received, an API request is sent to a preset API translation service provider to call a corresponding preset translation API interface to translate each word in the list of words to be translated.
[0021] According to the word translation result, the text translation result is determined by matching the words in the code text to be translated.
[0022] In an embodiment, after the step of determining the text translation result by matching the words in the code text to be translated according to the word translation result, the method further includes:
[0023] updating the WebView interface, and showing the user the text translation result and the code text to be translated through the updated WebView interface;
[0024] Upon receiving a text translation instruction triggered by the user based on the updated WebView interface, performing the step of updating the current code file according to the text translation result.
[0025] In an embodiment, the step of updating the current code file according to the text translation result to obtain a translated code file comprises:
[0026] determining the word position and the word matching item of each word translation result in the text translation result in the current code file;
[0027] based on each word position, replacing each word matching item with the corresponding word translation result to obtain a translated code file.
[0028] In an embodiment, the step of obtaining a translated code file comprises:
[0029] generating a current code translation record of the current code file according to the code text to be translated, the word list to be translated, the text translation result and the translated code file;
[0030] storing the current code translation record into a preset Pinia state management library, and the preset Pinia state management library also stores a historical code translation record of the current code file.
[0031] In addition, to achieve the above-mentioned purpose, the present application also provides a code text translation device, which comprises a memory, a processor and a code text translation program stored in the memory and executable on the processor, and the code text translation program is configured to implement the steps of the code text translation method as described above.
[0032] In addition, to achieve the above-mentioned purpose, the present application also provides a computer program product, which comprises a code text translation program, and the code text translation program is executed by a processor to implement the steps of the code text translation method as described above.
[0033] This application discloses a code text translation method, comprising: obtaining the current code file and determining the code text to be translated within the current code file; parsing the code text to be translated using abstract syntax tree technology, generating a list of terms to be translated based on the parsing results, and displaying the list of terms to be translated to the user through a preset page interaction component; upon receiving a confirmation instruction from the user, calling a preset translation API interface to translate the text of the list of terms to be translated; and updating the current code file based on the text translation results to obtain a translated code file.
[0034] Because this application can accurately parse the code structure using abstract syntax tree technology, it helps avoid unnecessary text mistranslation and ensures the accuracy and reliability of the translation. By displaying a list of terms to be translated to the user through preset page interaction components, the user can confirm or adjust the translation scope or content, reducing the mechanical adaptation error of machine translation and ensuring that the translation result is consistent with the semantics of the code context. The translation API is only called after receiving the user's confirmation instruction, avoiding the resource consumption of invalid translation requests. Finally, the current code file is updated directly based on the translation result, maintaining the integrity and maintainability of the code file, reducing the workload of manual verification and secondary modification, and improving the accuracy and execution efficiency of code text translation. Attached Figure Description
[0035] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.
[0036] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0037] Figure 1 This is a flowchart illustrating the first embodiment of the code text translation method of this application;
[0038] Figure 2 This is a flowchart illustrating the second embodiment of the code text translation method of this application;
[0039] Figure 3 This is a flowchart illustrating the third embodiment of the code text translation method of this application;
[0040] Figure 4 This is a schematic diagram illustrating the entire process of the code text translation method in this application.
[0041] The purpose, features, and advantages of this application will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation
[0042] It should be understood that the specific embodiments described herein are merely intended to explain the technical solutions of the present application, and are not used to limit the present application.
[0043] In order to better understand the technical solutions of the present application, the following will be described in detail in conjunction with the accompanying drawings and specific embodiments.
[0044] The embodiment of the present application provides a code text translation method, referring to Figure 1 , Figure 1 The flowchart of the first embodiment of the code text translation method of the present application, in this embodiment, the method comprises steps S10-S40:
[0045] Step S10: obtaining a current code file and determining a code text to be translated in the current code file. It should be noted that the execution subject of the present embodiment can be a computing service device with data processing, network communication, program running, code editing functions, such as notebook computer, desktop computer, etc., and can also be other electronic devices installed with code editor, such as VSCode editor. The present embodiment takes a code text translation device (hereinafter referred to as "device") installed with VSCode editor as an example to explain each embodiment of the present application.
[0046] It should be understood that the current code file can be a code file that needs to perform code translation task, and the user can open or import the current code file in the VSCode editor, so as to perform subsequent code translation task in the VSCode editor.
[0047] After the user imports the current code file in the VSCode editor, the device can capture the user operation instruction through the API of the VSCode editor, automatically identify the path, language type (such as JavaScript / TypeScript / Vue, etc.) and encoding format (UTF-8 / GBK, etc.) of the current code file.
[0048] It can be understood that the code text to be translated can be part of the code text or all of the code text in the current code file determined by the user when triggering the code translation operation in the VSCode editor. Therefore, the code translation operation can include selected text translation operation or all text translation operation, selected text translation operation means that the user selects part of the code text and then triggers translation; all text translation operation means that the user directly triggers translation of the entire code text through the right-click menu.
[0049] In a specific implementation, when a user opens a current code file in a VSCode editor, a device can listen to an editor.context event and the like through a plug-in to respond to a code translation operation triggered by the user in the VSCode editor. If the user selects part of the text and right-clicks the "smart translation" option, the plug-in determines that the selected part of the code text is the text to be translated. If the user performs a translation operation on the entire file, such as right-clicking the file label or triggering translation through a specific shortcut key, the plug-in determines that the entire code file content is the text to be translated.
[0050] Step S20: The abstract syntax tree technology is used to parse the code text to be translated, a list of words to be translated is generated according to the parsing result, and the list of words to be translated is displayed to the user through a preset page interaction component.
[0051] It should be understood that the abstract syntax tree (AST) technology is a tree expression method for representing the structure of source code. Based on the AST technology, the source code can be parsed into hierarchical nodes, and each node represents a part or a structure of the code.
[0052] Therefore, the above-mentioned code text to be translated can be parsed into an AST syntax tree through a plug-in tool such as @babel / parser. Then, the AST nodes are traversed, and nodes such as StringLiteral, TemplateLiteral and the like are screened out, and it is detected whether the nodes contain Chinese characters. Finally, the Chinese characters are extracted to generate a list of words to be translated, which is composed of a plurality of words.
[0053] It should be noted that the preset page interaction component can be a WebView interface in the VSCode editor. Based on the WebView, an interactive interface with the user can be provided, and the list of words to be translated is displayed to the user. The user can view the text to be translated and the list of words to be translated in the WebView interface, and edit and adjust them, in addition, the user can also select a translation language.
[0054] Step S30: When the confirmation instruction of the user is received, a preset translation API interface is called to perform text translation on the list of words to be translated.
[0055] It should be noted that after the user completes the editing and adjustment of the text to be translated and the list of words to be translated or the selection of the translation language in the WebView interface, a confirmation instruction can be triggered.
[0056] Then, the VSCode editor can send an API request to a preset translation API interface (such as Baidu translation, Google translation or DeepL API) through the vscode.ExtensionContext to realize interface calling, so as to send the list of words to be translated to the translation server for text translation, and receive the text translation result returned by the translation server.
[0057] Step S40: updating the current code file according to the text translation result to obtain a translated code file.
[0058] It should be understood that after obtaining the text translation result returned by the translation server, the text translation result can be first displayed in the WebView interface.
[0059] Then, the user can click the "one-click replacement" button in the WebView interface, and then calculate the position of the original text corresponding to the text translation result in the current code file, so as to replace all original text matching items in the current code file through the built-in code editing tool of the VSCode editor, to realize translation update of the current code file, and obtain a translated code file.
[0060] In addition, in the above updating process, the VSCode editor can also provide a undo function to ensure operation safety.
[0061] The embodiment can directly complete the translation task in the code editor without switching to an external tool for text copying and pasting, greatly improving the development efficiency and reducing the time cost caused by manual operation. At the same time, the AST technology is used to accurately analyze the code structure, avoid unnecessary text misinterpretation, ensure the accuracy and reliability of the translation, effectively filter duplicates and organize the word list, improve the efficiency and quality of text extraction. In addition, the preset page interaction component is used to display the words to be translated and their translation results, so that the user can intuitively view and edit the translation content, improve the friendliness of human-computer interaction, and make the translation process more intuitive and convenient.
[0062] Based on the first embodiment of the present application, in the second embodiment of the present application, the same or similar contents as the above embodiment one can be referred to the above introduction, and will not be repeated hereinafter. On this basis, please refer to Figure 2 , Figure 2 is a flowchart of the second embodiment of the code text translation method of the present application.
[0063] In the present embodiment, in order to specifically illustrate how to extract the list of words to be translated based on the AST technology, step S20 specifically includes steps S201-S204:
[0064] Step S201: parse the code text to be translated using the abstract syntax tree (AST) technique to generate a plurality of AST nodes.
[0065] It should be noted that AST is a tree-shaped data structure representing the structure of source code, and each node represents a structure or element in the code. JavaScript and TypeScript code can be parsed into AST by using a parser such as @babel / parser, thereby obtaining a series of AST nodes.
[0066] Among them, the variable declaration, function definition, string literal, template string and other syntax structures in the code text to be translated can be parsed into corresponding AST nodes, and each node contains detailed information about the code element, such as type, value, location, etc.
[0067] Step S202: traverse the AST nodes and filter out the AST nodes containing translatable text.
[0068] It can be understood that after the AST nodes are generated, the AST nodes can be traversed: the AST tree is traversed by depth-first or breadth-first, and the type and attribute of each AST node are checked one by one to determine the AST node containing translatable text content.
[0069] Among them, the translatable text can include: string literal (StringLiteral), such as "const msg = "Hello"; template string (TemplateLiteral), such as console.log(ˋWelcome ${user}ˋ); JSX / TSX text node, such as submit .
[0070] In specific implementation, Chinese characters can be detected by regular expression / [\u4e00-\u9fa5] / , and non-translation content (such as Chinese variable name, URL encoding) can be excluded by combining context analysis; the interpolation expression, property value and other special processing in Vue / React template are carried out to ensure that only pure text content is extracted, thereby filtering out the AST nodes containing translatable text and the corresponding translatable text.
[0071] Step S203: repeating item filtering is performed on each AST node containing translatable text to obtain target AST nodes, and a list of translation items to be translated is generated according to each target AST node.
[0072] It should be understood that there may be repeated text content in the AST nodes containing translatable text screened out. For example, the same prompt information string'save success' may appear multiple times in the code, which needs to be filtered for duplicates.
[0073] It should be noted that a hash table (Hash Map) or other data structure can be used to record the text content that has appeared. When a new AST node containing translatable text is encountered, it is checked whether its text content already exists in the hash table. If it exists, it is marked as a duplicate and filtered out; if it does not exist, its text content is added to the hash table, and the AST node is determined as a target AST node.
[0074] Finally, according to the text content in these target AST nodes, a list of to-be-translated terms is generated. Each term corresponds to translatable text content in a target AST node, and the entries in the list are the objects of subsequent translation operations. Thus, the conversion from AST nodes to a list of to-be-translated terms is completed, and preparations are made for the subsequent translation step.
[0075] Step S204: Pop up the WebView interface in the VSCode editor, and show the user the to-be-translated code text and the list of to-be-translated terms through the WebView interface.
[0076] It should be noted that in the VSCode editor, an interactive interface can be built based on the WebView function provided. After generating the list of to-be-translated terms, this WebView interface can be popped up in the VSCode editor to allow the user to adjust the to-be-translated code text and / or the list of to-be-translated terms based on the WebView interface, and trigger a confirmation instruction based on the adjusted to-be-translated code text and / or the adjusted list of to-be-translated terms.
[0077] In this WebView interface, the to-be-translated code text and the generated list of to-be-translated terms can be displayed simultaneously. For example, for the screened-out code content containing Chinese string literals, the corresponding code fragments and their context information are displayed, allowing the user to clearly see the location and role of these texts in the code. At the same time, all to-be-translated terms are clearly presented in the form of a list, facilitating the user to quickly browse and check.
[0078] It should be noted that the displayed code text to be translated can be a whole paragraph of code containing the word to be translated or the code line where it is located, ensuring that the user can understand the meaning of these texts in the code logic, so as to better judge whether translation is needed and how to translate. For the word list to be translated, each word item can include word content, location identification (such as line number, column number, etc.) in the code, and code file name belonging to it, etc. Information is helpful to help users quickly locate.
[0079] In a specific implementation, the user can adjust the displayed code text to be translated in the WebView interface. For example, if some text does not need to be translated, the user can remove it from the list; or edit and modify the text, such as correcting the text content, adjusting the text format, etc., to ensure the accuracy of the translation and meet the actual needs. At the same time, the user can also directly operate the word list. For example, modify the translation language option of the word, adjust the order of the word, merge or split the word, etc.
[0080] Further, when the user completes the adjustment of the code text or the word list, a confirmation instruction can be triggered by clicking the "Confirm" button in the WebView interface or other operations, to trigger subsequent translation processing. Therefore, step S30 specifically includes steps S301-S302:
[0081] Step S301: When the user's confirmation instruction is received, an API request is sent to a preset API translation service to call a corresponding preset translation API interface to translate each word in the word list to be translated.
[0082] It should be understood that when the user triggers the confirmation instruction in the WebView interface, the VSCode editor can send an API request to the corresponding API translation service according to the preset translation API service configuration (such as Baidu translation, Google translation or DeepL, etc.).
[0083] Among them, the API request can include the content of each word in the word list to be translated and related translation parameters, such as the source language (usually Chinese and other languages that need to be translated), the target language (such as English, French, and other user-specified languages), etc. The request data can be encapsulated and formatted according to the requirements of the API translation service, to ensure that it can be correctly parsed and processed by the translation service.
[0084] Next, after receiving the request, the API translation service uses its internal translation model and algorithm to translate each word in the word list: generates corresponding translation results according to context semantics, word usage, etc., and arranges these results into a structured data format (such as JSON) as the calling result of the preset translation API interface.
[0085] Step S302: According to the word translation result, the word matching is performed on the code text to be translated, and the text translation result is determined.
[0086] It should be understood that after obtaining the word translation result, the word translation result can be matched with the original code text to be translated. The matching process can be realized by the position identification, content characteristics and other information of the word in the code text.
[0087] Exemplarily, according to the line number, column number and unique content of each word in the code, it can be determined that each translated word should be replaced in which specific position in the code text to be translated; through the replacement operation, the translated word is accurately inserted into the corresponding position of the code text to be translated, so as to obtain the complete text translation result.
[0088] The embodiment can realize accurate analysis and efficient extraction of code text, clearly present the text to be translated in the form of a word list, facilitate user preview and adjustment, greatly simplify the operation process of front-end developers when handling multilingual translation tasks, and effectively improve the translation efficiency and accuracy. At the same time, the translation operation is completed directly in the VSCode editor, without the need to switch to external tools for text copying and pasting, reducing the time cost and error probability caused by manual operation, improving the development efficiency; and through calling the preset translation API interface for intelligent translation, high-quality translation results are quickly obtained, meeting the urgent needs of multilingual support in the development process, providing strong technical support for internationalization (i18n) or localization (l10n) development, and enhancing the convenience and flexibility of software development.
[0089] Based on the first and second embodiments of the present application, in the third embodiment of the present application, the same or similar contents as the above-mentioned first and second embodiments can be referred to the above introduction, and will not be repeated hereinafter. On this basis, please refer to Figure 3 , Figure 3 is a flowchart of the third embodiment of the code text translation method of the present application.
[0090] In the present embodiment, after obtaining the text translation result, it can also be displayed to the user to finally decide whether to perform code replacement in the current code file of the VSCode editor, after step S302, further comprising steps S303-S304:
[0091] Step S303: updating the WebView interface, and displaying the text translation result and the code text to be translated to the user through the updated WebView interface.
[0092] It should be noted that after obtaining the complete text translation result, the text translation result can be re-rendered to the WebView interface together with the original code text to be translated.
[0093] Exemplarily, when the WebView interface is displayed, the text translation result can also be compared and displayed with the code text to be translated in an intuitive manner, such as different color marks, table forms, etc. Thus, by retaining the original code text to be translated, the user can conveniently view the overall effect before and after translation and perform comparison and verification.
[0094] The user can view whether the text translation result meets the expectation on the updated WebView interface: whether the translation result of each word item is accurately replaced in the corresponding position in the code text to be translated, and whether the logic and structure of the code are affected, etc.
[0095] If the text translation result does not meet the expectation, for example, there are problems such as inaccurate translation, wrong replacement position, etc., the user can stop this code translation task and return to the step of triggering the code translation operation to reselect the code text to be translated in the current code file.
[0096] Step S304: When receiving the text translation instruction triggered by the user based on the updated WebView interface, performing the step of updating the current code file according to the text translation result.
[0097] It should be understood that if the text translation result meets the expectation, the user can trigger the text translation instruction to realize text replacement of the original current code file by clicking the "confirm replacement" or "apply translation" button in the updated WebView interface. Then, when receiving the text translation instruction, the subsequent step of updating the code file is performed.
[0098] Correspondingly, in order to specifically illustrate how to implement the translation update of the current code file, step S40 includes steps S401-S402:
[0099] Step S401: Determine the word item position and word item matching item of each word item translation result in the text translation result in the current code file.
[0100] It should be understood that the original word item matching item and the specific word item position (such as line number and column number) corresponding to each word item translation result in the current code file can be determined according to the aforementioned AST analysis and word item extraction process.
[0101] In a specific implementation, according to the position information, the position of each term to be replaced can be accurately located, and the original term content matched therewith can be determined. For example, in the JavaScript code, if a certain string literal is located at the 10th line and the 5th column with a length of 10 characters, the position and length can be used as the matching basis to determine the replacement position of the corresponding translated term.
[0102] Step S402: replacing each term matching item with the corresponding term translation result based on the term position to obtain a translated code file.
[0103] It should be understood that after the term position and the matching item are determined, the replacement operation can be performed. The device can safely modify the current code file through the code editing API in the VSCode editor. During the replacement process, each term matching item can be replaced with the corresponding term translation result while keeping other parts of the code unchanged.
[0104] It should be noted that after the replacement is completed, the modified code file can be saved to obtain a translated code file. The translated code file contains all the translated and replaced text content, which can meet the needs of multilingual development. In addition, the user can also use the undo function provided by the VSCode editor to conveniently restore to the previous state when an error occurs.
[0105] Further, Pinia can also be selected as a state management library for storing code translation records based on the VSCode editor. Therefore, step S402 includes steps S4021-S4022.
[0106] Step S4021: generating a current code translation record of the current code file according to the to-be-translated code text, the to-be-translated term list, the text translation result, and the translated code file.
[0107] It should be noted that when the code translation task of the current code file is completed, the key information during the task execution process can be recorded in detail to generate the current code translation record of the current code file, including: the original to-be-translated code text (i.e., the code content before the translation operation starts), the to-be-translated term list (which records each term to be translated and its position in the code and other information), the text translation result (the translated content corresponding to each term), and the translated code file (the code content after the translation is completed).
[0108] It should be understood that the current code translation record can be regarded as a complete translation operation snapshot, which completely saves the key data in the conversion process from the original code to the translated code, and provides detailed basis for subsequent operations such as backtracking, modification, review, etc.
[0109] Step S4022: store the current code translation record into a preset Pinia state management library, and the historical code translation records of the current code file are also stored in the preset Pinia state management library.
[0110] It should be noted that Pinia is a state management mode suitable for Vue.js applications, which can conveniently share states between different components and modules of the application, and supports persistent storage of states.
[0111] After generating the current code translation record, the record can be added to the preset Pinia state management library (Pinia library). The Pinia library has already stored the historical code translation records of the current code file. These historical records also contain detailed information of each translation operation, such as the original content of the word, the translated content, the position, etc.
[0112] It should be understood that by storing the current code translation record into the Pinia library, centralized management and maintenance of the code translation history can be realized. Users can conveniently query and browse all translation records in the Pinia library to perform operations such as version comparison, backtracking to previous translation states, etc. In addition, the Pinia library can also ensure that the translation records can be retained after the plug-in is restarted or the VSCode editor is closed through its persistent function (such as storing the state into the localStorage or userData of the browser), so as to facilitate subsequent use again.
[0113] The embodiment can realize instant preview and comparison of translation results in the VSCode editor, so that users can intuitively view the translation effect without switching tools, ensuring the accuracy of translation. By accurately positioning the word position and replacing, manual operation errors are avoided, and the efficiency and quality of code translation are improved. At the same time, with the help of the Pinia state management library, the current and historical translation records are saved, which facilitates users to backtrack and manage the translation process at any time, and ensures the traceability and safety of code translation.
[0114] Further, reference can be made to Figure 4 The whole process of the code text translation method of the present application is described, Figure 4 The whole process of the code text translation method of the present application is described,
[0115] from Figure 4It can be known that first, after the user opens the current code file in the VSCode editor, the user can trigger the "smart translation" option by right-clicking, select part of the code text or directly select the entire code text in the current code file, and obtain the code text to be translated.
[0116] Next, the above-mentioned code text to be translated is parsed by using AST, AST nodes containing translatable text are screened out and duplicate texts are filtered, a list of translation items to be translated is generated, and a WebView interface is used to show the list to the user, allowing the user to manually modify the translation content and select the translation language.
[0117] After the user confirms that the list of translation items to be translated is correct, the user can call a translation API interface to implement smart translation, and update the WebView interface to display the text translation result.
[0118] Finally, after the user determines that the text translation result is correct, the user can also trigger a text translation instruction to replace the original current code file in the VSCode editor, and obtain a translated file.
[0119] At the same time, key information in the entire translation task execution process can be recorded in detail to generate a current code translation record of the current code file, and the current code translation record is stored in a preset Pinia state management library, so that the code translation history can be centrally managed and maintained.
[0120] The code text translation method provided in the application can realize accurate translation extraction through AST analysis and interactive management, integrate multi-translation engine intelligent adaptation and translation memory optimization, combine a safe update and a history tracking mechanism, and significantly improve development efficiency and code maintainability.
[0121] The application provides a computer-readable storage medium having computer-readable program instructions (i.e., a computer program) stored thereon, and the computer-readable program instructions are used to execute the code text translation method in the above-mentioned embodiments.
[0122] The computer readable storage medium provided in the present application may, for example, be a U disk, but is not limited to an electric, magnetic, optical, electromagnetic, infrared, or semiconductor system or device, or any combination thereof. More specific examples of the computer readable storage medium may include, but are not limited to, an electric connection with one or more conductive wires, a portable computer disk, a hard disk, a random access memory (RAM), a read only memory (ROM), an erasable programmable read only memory (EPROM or flash memory), an optical fiber, a portable compact disk read only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof. In the present embodiment, the computer readable storage medium may be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system or device. The program code contained on the computer readable storage medium can be transmitted by any suitable medium, including but not limited to an electric wire, an optical cable, an RF (Radio Frequency), and the like, or any suitable combination thereof.
[0123] The computer readable storage medium described above may be contained in the code text translation device, or may exist separately without being assembled into the code text translation device.
[0124] The computer readable storage medium described above carries one or more programs, which, when executed by the code text translation device, cause the code text translation device to perform the following steps: obtaining a current code file and determining to-be-translated code text in the current code file; parsing the to-be-translated code text using an abstract syntax tree technique, generating a to-be-translated term list according to a parsing result, and displaying the to-be-translated term list to a user through a preset page interaction component; when receiving a confirmation instruction of the user, calling a preset translation API interface to perform text translation on the to-be-translated term list; updating the current code file according to a text translation result, and obtaining a translated code file.
[0125] Computer program code for carrying out operations of the present application can be written in any combination of one or more programming languages, including an object oriented programming language such as Java, Smalltalk, C++ or the like, and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code can execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or server. In the latter scenario, the remote computer can be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or the connection can be made to an external computer (for example, through the Internet using an Internet Service Provider).
[0126] The flow diagrams and the block diagrams in the drawings are illustrations of architectures, functionalities, and operations of possible implementations of systems, methods, and computer program products according to various embodiments of the present application. In this regard, each block in the flow diagrams or block diagrams can represent a module, a segment, or a portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that in some alternative implementations, the functions noted in the blocks can occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently or the blocks may
[0127] The modules involved in the embodiments of the present application can be implemented in the form of software or in the form of hardware. In some cases, the name of the module does not constitute a limitation on the module itself.
[0128] The readable storage medium provided by the present application is a computer readable storage medium, which stores computer readable program instructions (i.e., a computer program) for executing the above-mentioned code text translation method, and can solve the technical problem that the existing code translation method consumes human cost and is prone to translation errors, thereby affecting the software development efficiency. Compared with the prior art, the computer readable storage medium provided by the present application has the same beneficial effects as the code text translation method provided by the above-mentioned embodiments, and will not be described here.
[0129] The application further provides a computer program product comprising a computer program which, when executed by a processor, implements the steps of the code text translation method as described above.
[0130] The computer program product provided by the application can solve the technical problem that the existing code translation method consumes human cost and is prone to translation errors, thereby affecting the software development efficiency. Compared with the prior art, the beneficial effects of the computer program product provided by the application are the same as those of the code text translation method provided by the above-described embodiments, and are not described herein.
[0131] The above-mentioned serial numbers of the embodiments of the application are only for description, and do not represent the advantages and disadvantages of the embodiments, and they are only part of the embodiments of the application, and do not limit the scope of the application. Any equivalent structural transformation made by using the contents of the specification and the drawings, or direct / indirect application in other related technical fields within the technical concept of the application is included in the protection scope of the application.
Claims
1. A code-text translation method, characterized in that, The method includes: Get the current code file, and determine the code text to be translated within the current code file; The abstract syntax tree technique is used to parse the code text to be translated, and a list of terms to be translated is generated based on the parsing results. The list of terms to be translated is then displayed to the user through a preset page interaction component. Upon receiving confirmation from the user, a preset translation API interface is invoked to translate the text of the list of terms to be translated. The current code file is updated based on the text translation results to obtain the translated code file.
2. The method as described in claim 1, characterized in that, The step of obtaining the current code file and determining the code text to be translated within the current code file includes: Import the current code file into the VSCode editor and, in response to a code translation operation triggered by the user in the VSCode editor, determine the corresponding code text to be translated; The code translation operation includes a selected text translation operation or a full text translation operation, wherein the code text to be translated is a portion of the code text in the current code file or the entire code text.
3. The method as described in claim 1, characterized in that, The step of parsing the code text to be translated using abstract syntax tree technology and generating a list of terms to be translated based on the parsing results includes: The text to be translated is parsed using Abstract Syntax Tree (AST) technology to generate several AST nodes. Traverse each of the AST nodes and filter out the AST nodes that contain translatable text; Duplicate items are filtered out from each of the AST nodes containing translatable text to obtain the target AST nodes, and a list of terms to be translated is generated based on each of the target AST nodes.
4. The method as described in claim 1, characterized in that, The preset page interaction component is a WebView interface. The step of displaying the list of terms to be translated to the user through the preset page interaction component includes: The WebView interface pops up in the VSCode editor, and displays the code text to be translated and the list of terms to be translated to the user through the WebView interface, so that the user can adjust the code text to be translated and / or the list of terms to be translated, and trigger a confirmation command based on the adjusted code text to be translated and / or the adjusted list of terms to be translated.
5. The method as described in claim 4, characterized in that, The step of calling a preset translation API interface to translate the list of terms to be translated upon receiving confirmation from the user includes: Upon receiving the user's confirmation instruction, an API request is sent to the preset API translation service provider to call the corresponding preset translation API interface to translate each entry in the list of entries to be translated; Based on the translation results of the terms, term matching is performed on the code text to be translated to determine the text translation result.
6. The method as described in claim 5, characterized in that, After the step of matching terms in the text to be translated based on the term translation results to determine the text translation results, the method further includes: The WebView interface is updated, and the updated WebView interface is used to display the text translation result and the code text to be translated to the user. Upon receiving a text translation instruction triggered by the user based on the updated WebView interface, the step of updating the current code file based on the text translation result is executed.
7. The method as described in claim 1, characterized in that, The step of updating the current code file based on the text translation result to obtain the translated code file includes: Determine the position of each term in the text translation results within the current code file, as well as the term matching items; Based on the position of each term, the matching item of each term is replaced with the corresponding translation result of each term to obtain the translated code file.
8. The method as described in claim 7, characterized in that, The step of obtaining the translated code file includes: Generate the current code translation record of the current code file based on the code text to be translated, the list of terms to be translated, the text translation results, and the translated code file; The current code translation record is stored in a preset Pinia state management library, which also stores the historical code translation records of the current code file.
9. A code-to-text translation device, characterized in that, The device includes: a memory, a processor, and a code-text translation program stored in the memory and executable on the processor, the code-text translation program being configured to implement the steps of the code-text translation method as described in any one of claims 1 to 8.
10. A computer program product, characterized in that, The computer program product includes a code-text translation program, which, when executed by a processor, implements the steps of the code-text translation method as described in any one of claims 1 to 8.
Citation Information
Patent Citations
Code translation method and device, computer equipment and storage medium
CN110134404A
Code file processing method and device
CN113011201A
Vue project code translation method and device and medium
CN114168189A
Method and system for quickly developing front-end page multi-language items
CN116243901A
Visual code processing method and device, storage medium and terminal
CN116522959A