Code internationalization processing method and device, computer equipment, readable storage medium and program product
By constructing an abstract syntax tree and generating semantic translation keys, and combining this with a translation engine to handle code internationalization, the problems of low text recognition accuracy and syntax errors in traditional code internationalization processing are solved, achieving efficient multilingual adaptation and maintenance.
Patent Information
- Application Number
- CN202512056333.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-31
- Publication Date
- 2026-02-17
AI Technical Summary
In traditional code internationalization, relying on regular expressions for string matching results in low text recognition accuracy and a high risk of syntax errors when filtering content that does not require translation.
An abstract syntax tree is constructed for the files to be processed. Content that does not need to be translated is filtered out through regular expressions, debugging context exclusion mechanisms, and intelligent string processing technology. Semantic translation keys are generated, and multilingual translation is performed in conjunction with a translation engine. The translated content is then stored in the corresponding language pack, thus decoupling the code from the translated text.
It improves the accuracy of text recognition, avoids grammatical errors, enables efficient maintenance and adaptation of multi-language packs, supports the syntax specifications of different front-end frameworks, and only requires updating the language pack to complete multi-language adaptation without modifying the business code.
Smart Images

Figure CN121541860A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of software development technology, and in particular to a code internationalization processing method, apparatus, computer equipment, computer-readable storage medium, and computer program product. Background Technology
[0002] With the development of front-end development technologies, a trend has emerged of multi-framework integrated development and deep implementation of large-scale front-end engineering. Code internationalization (multi-language adaptation), as a core capability supporting the globalization of products, is seeing its application scenarios and technical requirements continuously upgraded. In traditional technical solutions, code internationalization mainly relies on regular expressions for string matching.
[0003] In the current technological environment, this traditional method is weak in filtering content that does not require translation, resulting in low text recognition accuracy and easy syntax errors when modifying code. Summary of the Invention
[0004] Therefore, it is necessary to provide a method, apparatus, computer device, computer-readable storage medium, and computer program product that can effectively filter out code internationalization processing content that does not require translation, in order to address the above-mentioned technical problems.
[0005] Firstly, this application provides a code internationalization processing method, including:
[0006] Construct an abstract syntax tree corresponding to the file to be processed, extract the set of target string nodes that need to be translated from the file based on the abstract syntax tree, and filter the content that does not need to be translated in the set of target string nodes through regular expressions, debugging context exclusion mechanism and intelligent string processing technology to obtain the set of strings to be translated.
[0007] Obtain the pre-configured translation key generation mode. If the translation key generation mode is semantic mode, generate semantic translation keys based on the path fragment of the file to be processed, the unique identifier, and the sequence number of the string to be translated in the set of strings to be translated. Associate the string to be translated with the corresponding semantic translation key to form a set of key-value pairs to be translated.
[0008] Based on the file extension of the file to be processed, the target parser is determined; the target parser replaces the semantic translation keys in the set of translation key-value pairs with general internationalization function calls to obtain the internationalization code; the set of key-value pairs to be translated is stored in the base language pack in the preset directory;
[0009] The base language pack is translated into multiple languages using a translation engine, and the translated content is stored in the corresponding language packs.
[0010] When code internationalization is required, in response to the user's language switching operation, the target language to be translated is determined, and the translation content in the target language package corresponding to the target language is read through the internationalization modification code.
[0011] In one embodiment, a target parser is determined based on the file extension of the file to be processed; the target parser replaces the semantic translation keys in the translation key-value pair set with general internationalization function calls to obtain internationalization modification code, including:
[0012] If the file to be processed has the extension of type 1, then the first parser processes the file to obtain a template object, a script object, and a style object; a type 1 abstract syntax tree is constructed, and based on the type 1 abstract syntax tree, the first text node and the first attribute node in the template object that need internationalization are determined, and the semantic translation keys of the text node and attribute node are replaced with general internationalization function calls to obtain the internationalization modification code corresponding to the template object; the script object is processed through the first preset internationalization processing logic to obtain the internationalization modification code corresponding to the script object; the style object is processed through the second preset internationalization processing logic to obtain the internationalization modification code corresponding to the style object; if the file to be processed has the extension of type 2, then a type 2 abstract syntax tree is constructed. Tree; Identify the second text node and second attribute node in the second type of abstract syntax tree; Replace the semantic translation keys of the second text node and second attribute node with general internationalization function calls to obtain the internationalization modification code corresponding to the second type; If the file extension to be processed is of the third type, construct the third type abstract syntax tree; Based on the third type abstract syntax tree, determine the third node; If the third node is a third text node, replace the semantic translation key of the third text node with general internationalization function calls to obtain the internationalization modification code corresponding to the third type; If the third node is both a third text node and a third attribute node, replace the semantic translation keys of the third text node and the third attribute node with general internationalization function calls to obtain the internationalization modification code corresponding to the third type.
[0013] In one embodiment, a translation engine performs multilingual translation on a base language pack, and the translated content is stored in multiple corresponding language packs, including:
[0014] For any target language in a multilingual dataset, the target language package is determined based on the base language package and the target language. The base language package and the target language package are compared to identify the missing key-value pairs to be translated in the target language package, forming an incremental translation list. The incremental translation list is translated into the target language by the translation engine to obtain the translated content. The translated content is then mapped to semantic translation keys and stored in the target language package.
[0015] In one embodiment, the method further includes:
[0016] If the translation key generation mode is content deduplication mode, then the strings to be translated in the set of strings to be translated are processed to remove trailing whitespace, resulting in whitespace-removed strings; the whitespace-removed strings are processed by a hash algorithm to obtain hash values; the hash values are used as the content deduplication translation key of the strings to be translated, and the strings to be translated are associated with the corresponding content deduplication translation key to form a set of key-value pairs to be translated.
[0017] In one embodiment, a semantic translation key is generated based on the path fragment of the file to be processed, a unique identifier, and the sequence number of the string to be translated in the set of strings to be translated, including:
[0018] Obtain the complete storage path of the file to be processed, and extract path fragments from the complete storage path according to the pre-configured path truncation rules; generate a unique identifier for the file to be processed; assign a sequence number to each string to be translated according to the order of the strings to be translated in the file; generate a semantic translation key based on the path fragment, unique identifier, and sequence number; associate the strings to be translated with the corresponding semantic translation key to form a set of key-value pairs to be translated.
[0019] In one embodiment, the content that does not need to be translated in the target string node set is filtered out using regular expressions, debugging context exclusion mechanisms, and intelligent string processing techniques to obtain a set of strings to be translated, including:
[0020] The first set of strings to be translated is obtained by using regular expressions to detect characters in the target string nodes. Based on resource extension detection rules and a configurable exclusion list, the content that does not need to be translated in the first set of strings to be translated is further filtered to obtain the second set of strings to be translated. The debugging information in the second set of strings to be translated is removed based on the context exclusion mechanism to obtain the third set of strings to be translated. The static tags in the third set of strings to be translated are removed based on string intelligent processing technology to obtain the final set of strings to be translated.
[0021] Secondly, this application also provides a code internationalization processing device, including:
[0022] The extraction module is used to construct the abstract syntax tree corresponding to the file to be processed. Based on the abstract syntax tree, it extracts the set of target string nodes that need to be translated from the file to be processed. It filters the content that does not need to be translated from the set of target string nodes through regular expressions, debugging context exclusion mechanism and intelligent string processing technology to obtain the set of strings to be translated.
[0023] The generation module is used to obtain the pre-configured translation key generation mode. If the translation key generation mode is semantic mode, it generates semantic translation keys based on the path fragment of the file to be processed, the unique identifier, and the sequence number of the string to be translated in the set of strings to be translated. It then associates the string to be translated with the corresponding semantic translation key to form a set of key-value pairs to be translated.
[0024] The processing module is used to determine the target parser based on the file extension of the file to be processed; the target parser replaces the semantic translation keys in the set of translation key-value pairs with general internationalization function calls to obtain the internationalization code; and stores the set of key-value pairs to be translated into the base language pack in a preset directory.
[0025] The translation module is used to perform multilingual translation of the base language pack through the translation engine, and to store the translated content into the corresponding language packs.
[0026] The reading module is used to respond to the user's language switching operation, determine the target language that needs to be translated, and read the translation content from the target language package corresponding to the target language through internationalization modification code.
[0027] Thirdly, this application also provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to perform the following steps:
[0028] Construct an abstract syntax tree corresponding to the file to be processed, extract the set of target string nodes that need to be translated from the file based on the abstract syntax tree, and filter the content that does not need to be translated in the set of target string nodes through regular expressions, debugging context exclusion mechanism and intelligent string processing technology to obtain the set of strings to be translated.
[0029] Obtain the pre-configured translation key generation mode. If the translation key generation mode is semantic mode, generate semantic translation keys based on the path fragment of the file to be processed, the unique identifier, and the sequence number of the string to be translated in the set of strings to be translated. Associate the string to be translated with the corresponding semantic translation key to form a set of key-value pairs to be translated.
[0030] Based on the file extension of the file to be processed, the target parser is determined; the target parser replaces the semantic translation keys in the set of translation key-value pairs with general internationalization function calls to obtain the internationalization code; the set of key-value pairs to be translated is stored in the base language pack in the preset directory;
[0031] The base language pack is translated into multiple languages using a translation engine, and the translated content is stored in the corresponding language packs.
[0032] When code internationalization is required, in response to the user's language switching operation, the target language to be translated is determined, and the translation content in the target language package corresponding to the target language is read through the internationalization modification code.
[0033] Fourthly, this application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, performs the following steps:
[0034] Construct an abstract syntax tree corresponding to the file to be processed, extract the set of target string nodes that need to be translated from the file based on the abstract syntax tree, and filter the content that does not need to be translated in the set of target string nodes through regular expressions, debugging context exclusion mechanism and intelligent string processing technology to obtain the set of strings to be translated.
[0035] Obtain the pre-configured translation key generation mode. If the translation key generation mode is semantic mode, generate semantic translation keys based on the path fragment of the file to be processed, the unique identifier, and the sequence number of the string to be translated in the set of strings to be translated. Associate the string to be translated with the corresponding semantic translation key to form a set of key-value pairs to be translated.
[0036] Based on the file extension of the file to be processed, the target parser is determined; the target parser replaces the semantic translation keys in the set of translation key-value pairs with general internationalization function calls to obtain the internationalization code; the set of key-value pairs to be translated is stored in the base language pack in the preset directory;
[0037] The base language pack is translated into multiple languages using a translation engine, and the translated content is stored in the corresponding language packs.
[0038] When code internationalization is required, in response to the user's language switching operation, the target language to be translated is determined, and the translation content in the target language package corresponding to the target language is read through the internationalization modification code.
[0039] Fifthly, this application also provides a computer program product, including a computer program that, when executed by a processor, performs the following steps:
[0040] Construct an abstract syntax tree corresponding to the file to be processed, extract the set of target string nodes that need to be translated from the file based on the abstract syntax tree, and filter the content that does not need to be translated in the set of target string nodes through regular expressions, debugging context exclusion mechanism and intelligent string processing technology to obtain the set of strings to be translated.
[0041] Obtain the pre-configured translation key generation mode. If the translation key generation mode is semantic mode, generate semantic translation keys based on the path fragment of the file to be processed, the unique identifier, and the sequence number of the string to be translated in the set of strings to be translated. Associate the string to be translated with the corresponding semantic translation key to form a set of key-value pairs to be translated.
[0042] Based on the file extension of the file to be processed, the target parser is determined; the target parser replaces the semantic translation keys in the set of translation key-value pairs with general internationalization function calls to obtain the internationalization code; the set of key-value pairs to be translated is stored in the base language pack in the preset directory;
[0043] The base language pack is translated into multiple languages using a translation engine, and the translated content is stored in the corresponding language packs.
[0044] When code internationalization is required, in response to the user's language switching operation, the target language to be translated is determined, and the translation content in the target language package corresponding to the target language is read through the internationalization modification code.
[0045] The aforementioned code internationalization processing method, device, computer equipment, computer-readable storage medium, and computer program product first construct an abstract syntax tree (AST) corresponding to the file to be processed. Based on the AST, a set of target string nodes requiring translation is extracted from the file. Regular expressions, debugging context exclusion mechanisms, and intelligent string processing techniques are used to filter out content in the target string node set that does not need translation, resulting in a set of strings to be translated. By filtering out content that does not need translation, the strings that truly need translation can be effectively obtained. A pre-configured translation key generation mode is then obtained. If the translation key generation mode is semantic mode, a semantic translation key is generated based on the path fragment of the file to be processed, a unique identifier, and the sequence number of the string to be translated in the set of strings to be translated. The strings to be translated are associated with the corresponding semantic translation keys, forming a set of key-value pairs to be translated. By establishing semantic translation keys, each string to be translated can be given a unique identifier with contextual semantics, avoiding translation key conflicts and facilitating... Later, the system quickly traces the file and location of the text using translation keys, improving the maintenance efficiency of multi-language packs. Based on the file extensions of the files to be processed, a target parser is determined. The target parser replaces the semantic translation keys in the translation key-value pair set with general internationalization function calls, resulting in internationalization modification code. By replacing the semantic translation keys with internationalization modification code, it can adapt to the syntax specifications of different front-end frameworks, ensuring that the modified code can be compiled and run normally. It also decouples the code from the translated text, allowing for multi-language adaptation with only language pack updates, without modifying business code. The set of key-value pairs to be translated is stored in a base language pack in a preset directory. The translation engine performs multi-language translation on the base language pack, and the translated content is stored in the corresponding language packs. When code internationalization is required, in response to the user's language switching operation, the target language to be translated is determined, and the translation content in the target language pack corresponding to the target language is read through the internationalization modification code. This application effectively filters out content that does not need translation by constructing an abstract syntax tree corresponding to the files to be processed, combining regular expressions for accurate matching of Chinese characters, a debugging context exclusion mechanism, and intelligent splitting technology, and performing targeted identification and exclusion for special scenarios. Attached Figure Description
[0046] To more clearly illustrate the technical solutions in the embodiments of this application or related technologies, the drawings used in the description of the embodiments of this application or related technologies will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0047] Figure 1 This is a flowchart illustrating a code internationalization process in one embodiment.
[0048] Figure 2 This is a detailed flowchart illustrating the code internationalization process in one embodiment;
[0049] Figure 3 This is a structural block diagram of a code internationalization processing device in one embodiment;
[0050] Figure 4 This is an internal structural diagram of a computer device in one embodiment. Detailed Implementation
[0051] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0052] It should be noted that the terms "first," "second," etc., used in this application can be used to describe various elements, but these elements are not limited by these terms. These terms are only used to distinguish the first element from the second element. The terms "comprising" and "having," and any variations thereof, used in this application, are intended to cover non-exclusive inclusion. The term "multiple" used in this application refers to two or more. The term "and / or" used in this application refers to one of the embodiments, or any combination of multiple embodiments.
[0053] In one embodiment, such as Figure 1 As shown, a code internationalization method is provided. This embodiment illustrates the method's application to a terminal. It is understood that this method can also be applied to a server, and further to a system including both a terminal and a server, and is implemented through interaction between the terminal and the server. In this embodiment, the method includes the following steps:
[0054] Step 102: Construct the abstract syntax tree corresponding to the file to be processed. Based on the abstract syntax tree, extract the set of target string nodes that need to be translated from the file to be processed. Filter the content that does not need to be translated from the target string node set through regular expressions, debugging context exclusion mechanism and intelligent string processing technology to obtain the set of strings to be translated.
[0055] Among them, the Abstract Syntax Tree (AST) is an abstract data model that represents the code syntax structure in a tree structure after the code text of the file to be processed undergoes syntax parsing. This application constructs the abstract syntax tree relying on professional syntax parsing tools such as Babel (Babel compiler) and Vue compiler. When extracting the set of target string nodes based on the abstract syntax tree, it traverses the AST nodes and identifies nodes containing hard-coded Chinese characters such as StringLiteral (string literal node), JSXText (JSX text node), and JSXAttribute (JSX attribute node), and filters out all potential text nodes that need to be translated.
[0056] Among them, JSX is JavaScript XML (JavaScript Extensible Markup Language), Text refers to text, and Attribute refers to attribute. Babel is an open-source JavaScript compiler, which is essentially a set of syntax parsing and transformation toolchains for processing JavaScript / TypeScript code. The Vue compiler specifically refers to tool sets such as @vue / compiler-sfc (single-file component compiler) and @vue / compiler-dom (DOM template compiler) provided by Vue official.
[0057] Exemplarily, taking the file "src / components / Login / LoginForm.tsx" in a React project as an example: First, construct the abstract syntax tree (AST) of this file through the Babel parser that enables the typescript and jsx plugins, traverse the AST to identify three types of target string nodes: StringLiteral (such as "登录" in const btnText = "登录"), JSXText (such as Please enter your username and password. "请输入账号密码" in ), and JSXAttribute (such as placeholder = "手机号 / 邮箱" in <input placeholder = "手机号 / 邮箱" / >), to form an initial set of target string nodes; then filter out the content that does not need to be translated in the set of target string nodes through regular expressions, debugging context exclusion mechanism, and string intelligent processing technology, and obtain the set of strings to be translated.
[0058] Step 104: Obtain the pre-configured translation key generation mode. If the translation key generation mode is semantic mode, then based on the path fragment of the file to be processed, the unique identifier, and the sequence number of the string to be translated in the set of strings to be translated, generate a semantic translation key, associate the string to be translated with the corresponding semantic translation key, and form a set of key-value pairs to be translated.
[0059] Optionally, in addition to semantic markup mode, the translation generation mode can also be content deduplication mode. The sequence number of the string to be translated is assigned sequentially based on its position in the file to be processed.
[0060] The format of semantic translation is: path fragment - unique identifier - sequence number.
[0061] Step 106: Determine the target parser based on the file extension of the file to be processed; replace the semantic translation keys in the translation key-value pair set with general internationalization function calls through the target parser to obtain the internationalization transformation code; store the key-value pair set to be translated into the base language pack in the preset directory.
[0062] Specifically, if the file to be processed has the extension .vue, the Vue compiler (@vue / compiler-sfc+@vue / compiler-dom) is selected as the target parser; if it is .js / .jsx, the Babel parser with the JSX plugin enabled is selected; if it is .ts / .tsx, the Babel parser with both TypeScript and JSX plugins enabled is selected.
[0063] Here, `vue / compiler-sfc+@vue / compiler-dom` refers to the combined use of two core compiler packages provided by Vue. They work together to perform full-dimensional syntax parsing and processing of `.vue` files. TypeScript is an open-source, strongly typed superset of JavaScript developed by Microsoft.
[0064] Step 108: Perform multilingual translation on the basic language pack using a translation engine, and store the translated content into the corresponding language packs.
[0065] Optionally, the translation engine supports multiple integration methods. It integrates machine translation APIs (such as Baidu Translate, Google Translate, DeepL Translate, etc.) by default, and also supports the integration of custom-deployed private translation models. The system supports switching the translation engine type and corresponding authentication parameters (such as API keys and request addresses) through configuration files. Before translation, the basic language package is preprocessed: the validity of key-value pairs is verified, invalid translation content such as null values and pure symbols is filtered out, and translation requests are initiated in batches according to the preset target language list (such as en-US (English-United States), ja-JP (Japanese-Japan), ko-KR (Korean-Korea), etc.). The target language list can be customized through the project configuration file, supporting coverage of major global languages and regional variations. During the translation process, the rule of "translation key remains unchanged, only value is replaced" is adopted: taking "views-Order-Payment-8f7e6d5c4b3a2e1f-3":"Submit" in the basic language pack (such as zh-CN) as an example, after translating to English (en-US), the translation key is still views-Order-Payment-8f7e6d5c4b3a2e1f-3, and the value is replaced with Submit; after translating to Japanese (ja-JP), the value is replaced with Submit, ensuring the consistency of translation keys in different language packs and providing a unified identifier for text matching during subsequent language switching.
[0066] Step 110: When code internationalization is required, in response to the user's language switching operation, determine the target language that needs to be translated, and read the translation content in the target language package corresponding to the target language through the internationalization modification code.
[0067] The user's language switching operation supports multiple trigger scenarios, including language selection dropdown menus on the front-end page (such as "Simplified Chinese / Traditional Chinese / English / Japanese"), automatic browser language preference recognition, and URL parameter specification. The system will prioritize user-initiated switching operations; if no user-initiated operation occurs, it will read the language identifier returned by the browser's navigator.language as the default target language. The core logic of the internationalization code for reading translated content is as follows: the general internationalization function embedded in step 106 has a built-in language pack loading and text matching mechanism. When the function is executed, it first obtains the currently determined target language identifier, loads the corresponding language pack file from the preset language pack directory (such as src / locales / ), and then matches the corresponding translation value in the target language pack using the passed semantic translation key (such as views-Order-Payment-8f7e6d5c4b3a2e1f-3).
[0068] For example, when a user clicks "Switch to British English (en-GB)" on the page: the system responds to this operation, determines the target language as en-GB, the internationalization function $t('src-components-Login-LoginForm-9a8b7c6d5e4f3g2h-1') is triggered, loads the language pack src / locales / en-GB / order.json, matches the translation key src-components-Login-LoginForm-9a8b7c6d5e4f3g2h-1 in the language pack, obtains the corresponding value Login, and renders it to the page. Simultaneously, the language switching operation triggers a partial or global re-render of the page.
[0069] The above code internationalization method first constructs an abstract syntax tree (AST) for the file to be processed. Based on the AST, it extracts the set of target string nodes that need to be translated from the file. Then, it filters out the content in the target string node set that does not need translation using regular expressions, debugging context exclusion mechanisms, and intelligent string processing technology, resulting in a set of strings to be translated. By filtering out the content that does not need translation, the truly translatable strings can be effectively obtained. Next, it obtains a pre-configured translation key generation mode. If the translation key generation mode is semantic mode, it generates a semantic translation key based on the path fragment of the file to be processed, a unique identifier, and the sequence number of the string to be translated in the set of strings to be translated. The strings to be translated are then associated with the corresponding semantic translation keys, forming a set of key-value pairs to be translated. By establishing semantic translation keys, each string to be translated can be given a unique identifier with contextual semantics, avoiding translation key conflicts and facilitating quick text tracing later using the translation keys. The system improves the maintenance efficiency of multilingual packages by identifying the file and its location; it determines the target parser based on the file extension; the target parser replaces the semantic translation keys in the translation key-value pair set with general internationalization function calls to obtain internationalization modification code; by replacing the semantic translation keys with internationalization modification code, it can adapt to the syntax specifications of different front-end frameworks, ensuring that the modified code can be compiled and run normally, while decoupling the code from the translated text. Subsequent updates only require updating the language pack to complete multilingual adaptation without modifying the business code; the set of key-value pairs to be translated is stored in a base language pack under a preset directory; the translation engine performs multilingual translation on the base language pack, and the translated content is stored in the corresponding language packs; when code internationalization is required, in response to the user's language switching operation, the target language to be translated is determined, and the translation content in the target language pack corresponding to the target language is read through the internationalization modification code. This application effectively filters out content that does not need to be translated by constructing an abstract syntax tree corresponding to the file to be processed, combining regular expressions for accurate matching of Chinese characters, a debugging context exclusion mechanism, and intelligent splitting technology, and performing targeted identification and exclusion for special scenarios.
[0070] In an exemplary embodiment, a target parser is determined based on the file extension of the file to be processed; the target parser replaces the semantic translation keys in the translation key-value pair set with general internationalization function calls to obtain internationalization modification code, including:
[0071] If the file to be processed has the extension of type 1, then the first parser processes the file to obtain a template object, a script object, and a style object; a type 1 abstract syntax tree is constructed, and based on the type 1 abstract syntax tree, the first text node and the first attribute node in the template object that need internationalization are determined, and the semantic translation keys of the text node and attribute node are replaced with general internationalization function calls to obtain the internationalization modification code corresponding to the template object; the script object is processed through the first preset internationalization processing logic to obtain the internationalization modification code corresponding to the script object; the style object is processed through the second preset internationalization processing logic to obtain the internationalization modification code corresponding to the style object; if the file to be processed has the extension of type 2, then a type 2 abstract syntax tree is constructed. Tree; Identify the second text node and second attribute node in the second type of abstract syntax tree; Replace the semantic translation keys of the second text node and second attribute node with general internationalization function calls to obtain the internationalization modification code corresponding to the second type; If the file extension to be processed is of the third type, construct the third type abstract syntax tree; Based on the third type abstract syntax tree, determine the third node; If the third node is a third text node, replace the semantic translation key of the third text node with general internationalization function calls to obtain the internationalization modification code corresponding to the third type; If the third node is both a third text node and a third attribute node, replace the semantic translation keys of the third text node and the third attribute node with general internationalization function calls to obtain the internationalization modification code corresponding to the third type.
[0072] Optionally, the first type can be a file with the .vue extension; the second type can be .jsx; and the third type can be .ts / .tsx.
[0073] For example, when the file to be processed is src / components / Pay / Payment.vue (with the .vue extension, i.e., the first type), it is first split into parts by the Vue compiler (@vue / compiler-sfc + @vue / compiler-dom, i.e., the first parser). <template>Corresponding template object, <scriptsetup>The corresponding script object, <stylescoped>The corresponding style object. After constructing the first type of abstract syntax tree adapted to Vue syntax, traverse the template object AST to identify the first text nodes (such as Payment methods "Payment method" in Payment methods and the first attribute nodes (such as label="Pay immediately" in <button label="Pay immediately">), replace their semantic translation keys with general internationalization function calls respectively, and the original template code {{$t('views-Pay-Payment-7d8e9f0a1b2c3d4e-1')}} is transformed into <scriptsetup>The code `constsubmitText="Payment successful";` is modified using the first preset internationalization processing logic (reusing Babel to parse the Vue script's AST, processing only StringLiteral nodes). This transforms `constsubmitText="Payment successful";` into `constsubmitText=$t('views-Pay-Payment-7d8e9f0a1b2c3d4e-3')`, resulting in the internationalization code for the script object. For style objects (such as...), the internationalization code is then applied. <stylescoped>.pay-btn{color:#333; / *Payment button style* / }, through the second preset internationalization processing logic (only retaining the style structure and filtering out content with no translation value such as comments / class names), directly retain the original style code as the internationalization transformation code for the style object. When the file to be processed is src / pages / Login / LoginForm.jsx (file extension.jsx, i.e., the second type), enable the Babel parser of the jsx plugin to build the second type of abstract syntax tree (JSX syntax AST); identify the second text nodes in the AST (such as Please enter your account. "Please enter your account number" in {$t('src-Login-LoginForm-8c7b6a5d4e3f2g1h-1')} ) and the second attribute nodes (such as placeholder="Mobile number / Email" in <input placeholder="Mobile number / Email" / >); replace the second text nodes with {$t('src-Login-LoginForm-8c7b6a5d4e3f2g1h-1')} , and replace the second attribute nodes with <input placeholder={$t('src-Login-LoginForm-8c7b6a5d4e3f2g1h-2')} / >, finally obtaining the internationalization transformation code for this.jsx file. For the third type: Scenario 1 (.ts file, only the third text nodes): The file to be processed is src / utils / format.ts (file extension.ts, i.e., the third type). After building the third type of abstract syntax tree (TS syntax AST), only the third text nodes are identified (such as "Format verification failed" in const tip = "Format verification failed";), and replace it with const tip = $t('src-utils-format-9b8a7s6d5f4g3h2j-1'), obtaining the internationalization transformation code for this.ts file; Scenario 2 (.tsx file, including the third text nodes + the third attribute nodes): The file to be processed is src / components / Order / OrderList.tsx (file extension.tsx, i.e., the third type). After building the third type of abstract syntax tree (TSX syntax AST), identify the third text nodes (such as Order status "Order status" in {$t('src-Order-OrderList-0c9d8e7f6g5h4j3k-1')} ) and the third attribute nodes (such as label="Filter time" in <Select label="Filter time" / >), and replace them with {$t('src-Order-OrderList-0c9d8e7f6g5h4j3k-1')} and <Select label={$t('src-Order-OrderList-0c9d8e7f6g5h4j3k-2')} / > respectively, obtaining the internationalization transformation code for this.tsx file.
[0074] In this embodiment, by constructing abstract syntax trees for different file extensions and specifically identifying different types of nodes (text nodes, attribute nodes) and replacing them with general internationalized function calls, it can accurately adapt to the syntax features of mainstream front-end file types such as vue (.vue), jsx (.jsx), and ts / tsx (.ts / .tsx). This ensures that the internationalization code conforms to the specifications of each framework / syntax and achieves the unification of internationalization processing logic for multiple file types, avoiding omissions or syntax errors caused by differences in file types.
[0075] In an exemplary embodiment, a translation engine performs multilingual translation on a base language pack, and stores the translated content into multiple language packs respectively, including:
[0076] For any target language in a multilingual dataset, the target language package is determined based on the base language package and the target language. The base language package and the target language package are compared to identify the missing key-value pairs to be translated in the target language package, forming an incremental translation list. The incremental translation list is translated into the target language by the translation engine to obtain the translated content. The translated content is then mapped to semantic translation keys and stored in the target language package.
[0077] Exemplarily, taking the base language pack as Simplified Chinese (zh-CN), and the target languages as British English (en-GB) and Japanese (ja-JP) respectively, assume that the base language pack is stored in src / locales / zh-CN / order.json and contains three key-value pairs to be translated: "Payment Method", "Pay Immediately", and "Payment Successful". When it needs to be translated into British English (en-GB), the system first locates the language pack path corresponding to the target language, src / locales / en-GB / order.json. If the target language pack already exists and contains the historical translation results of the first two texts ("PaymentMethod", "PayImmediately"), by comparing the base language pack and the target language pack key by key, it is found that the semantic translation key corresponding to "Payment Successful" in the base language pack is missing in the target language pack, thus forming an incremental translation list containing only this content. Subsequently, the system passes this incremental translation list into the configured translation engine, specifying the target language as en-GB, and the engine returns the translation result "PaymentSuccessful". The system then establishes a mapping relationship between this translated content and the corresponding semantic translation key and supplements it to the en-GB target language pack to complete the incremental update. When the target language is Japanese (ja-JP) and src / locales / ja-JP / order.json has not been created yet, the system determines that the target language pack is missing all the key-value pairs of the base language pack. At this time, the incremental translation list is the complete content of the base language pack. After the translation engine translates all three texts into Japanese ("Payment Method", "Pay Immediately", "Payment Successful"), the system automatically creates the language pack file corresponding to ja-JP and writes the complete key-value pairs of the translation into it.
[0078] In this embodiment, through the translation method based on incremental comparison, there is no need to repeat the translation of the completed content, which not only reduces the number of calls to the translation engine and the usage cost, but also avoids overwriting the historically translated results that have been manually verified, greatly improving the efficiency and accuracy of the multi-language pack update.
[0079] In an exemplary embodiment, the method further includes:
[0080] If the translation key generation mode is the content deduplication mode, perform blank trimming on the strings to be translated in the set of strings to be translated to obtain blank-trimmed strings; process the blank-trimmed strings through a hash algorithm to obtain hash values; use the hash values as the content deduplication translation keys for the strings to be translated, and associate the strings to be translated with the corresponding content deduplication translation keys to form a set of key-value pairs to be translated.
[0081] For example, taking the set of strings to be translated generated from the file "src / pages / Order / OrderConfirm.vue" as an example, the initial set of strings to be translated in this file contains ["Submit Order", "Submit Order", "Confirm Payment", "Confirm Payment", "Cancel Operation"]. First, the system will perform trailing whitespace removal on each string to be translated in the set. Then, the system will use the MD5 hash algorithm (or SHA-256 or other algorithms) to calculate the hash value of each of the whitespace-removed strings. Subsequently, the system will directly use these hash values as the deduplication keys for the corresponding strings to be translated, establish a one-to-one mapping relationship, and form a set of key-value pairs to be translated.
[0082] In this embodiment, by removing spaces and performing hash processing, strings with the same content but different leading and trailing spaces will eventually correspond to the same hash key, retaining only one key-value pair. This achieves content-level deduplication of the strings to be translated, while strings with unique content generate their own hash keys. This effectively reduces the workload of translating duplicate text, avoids redundant duplicate key-value pairs in the basic language pack, and lowers the cost of subsequent multilingual translation. At the same time, it ensures that text with the same semantics corresponds to a unique translation key in the multilingual pack, greatly improving the cleanliness and maintenance efficiency of the language pack.
[0083] In an exemplary embodiment, a semantic translation key is generated based on the path fragment of the file to be processed, a unique identifier, and the sequence number of the string to be translated in the set of strings to be translated, including:
[0084] Obtain the complete storage path of the file to be processed, and extract path fragments from the complete storage path according to the pre-configured path truncation rules; generate a unique identifier for the file to be processed; assign a sequence number to each string to be translated according to the order of the strings to be translated in the file; generate a semantic translation key based on the path fragment, unique identifier, and sequence number; associate the strings to be translated with the corresponding semantic translation key to form a set of key-value pairs to be translated.
[0085] For example, taking the file "src / views / Order / Payment.vue" as an example, the system first obtains the complete storage path of the file as " / project / src / views / Order / Payment.vue", and extracts the path fragment "views-Order-Payment" from the complete path according to the pre-configured path truncation rules (such as truncation of the path three levels from the "src" level under the project root directory, with the level separator "-"). Then, it calculates a 32-bit unique identifier "8f7e6d5c4b3a2e1f9h8g7f6e5d4c3b2a" for the complete file path using the SHA-256 hash algorithm to ensure the uniqueness of the file's identifier within the project. Then, according to the order in which the string to be translated appears in the file (based on the AST depth-first traversal result), it assigns the "submitter" and "payer" strings in the set. The three strings to be translated, "Formula" and "Actual Payment Amount", are assigned serial numbers 1, 2, and 3 respectively. The system then concatenates the path fragment, unique identifier, and serial number according to the preset format "Path Fragment-Unique Identifier-Serial Number" to generate corresponding semantic translation keys: "views-Order-Payment-8f7e6d5c4b3a2e1f9h8g7f6e5d4c3b2a-1" (corresponding to "Submit"), "views-Order-Payment-8f7e6d5c4b3a2e1f9h8g7f6e5d4c3b2a-2" (corresponding to "Payment Method"), and "views-Order-Payment-8f7e6d5c4b3a2e1f9h8g7f6e5d4c3b2a-3" (corresponding to "Actual Payment Amount"). Finally, a one-to-one mapping relationship is established between each string to be translated and the generated semantic translation key, forming a set of key-value pairs to be translated.
[0086] In this embodiment, by first extracting path fragments of a specified level from the complete path of the file to be processed, generating a globally unique identifier for the file, assigning serial numbers according to the order of text appearance, and then concatenating the three in a preset format to generate a semantic translation key, each translation key can contain file path association information and have global uniqueness. It can also distinguish the text to be translated in different positions within the same file through serial numbers, effectively avoiding translation key conflicts between different files or text within the same file. At the same time, it provides a clear basis for subsequent translation key tracing (locating the file and position to which the text belongs), greatly improving the accuracy and maintainability of internationalization processing.
[0087] In one exemplary embodiment, the content that does not need to be translated in the target string node set is filtered out using regular expressions, debugging context exclusion mechanisms, and intelligent string processing technology to obtain a string set to be translated, including:
[0088] The first set of strings to be translated is obtained by using regular expressions to detect characters in the target string nodes. Based on resource extension detection rules and a configurable exclusion list, the content that does not need to be translated in the first set of strings to be translated is further filtered to obtain the second set of strings to be translated. The debugging information in the second set of strings to be translated is removed based on the context exclusion mechanism to obtain the third set of strings to be translated. The static tags in the third set of strings to be translated are removed based on string intelligent processing technology to obtain the final set of strings to be translated.
[0089] For example, taking the target string node set generated from the file "src / components / Login / LoginForm.tsx" as an example, the system first performs character detection on the target string nodes using regular expressions, filtering out nodes that only contain Chinese characters, excluding pure English strings such as constapiUrl=" / api / login" and pure numeric strings such as constnum="123456", resulting in the first set of strings to be translated, containing "Login", "Please enter your account and password", "Mobile number / email", "Debug: Account verification failed", and "Click". <button> submit< / button> The first set of strings to be translated includes the " / / login form prompt text" and other related content. Next, based on resource extension detection rules (filtering styles, images, and other non-business text) and a configurable exclusion list (pre-setting exclusions for "comment text, interface paths, number sequences, etc."), the comment text " / / login form prompt text" is filtered out from the first set of strings to be translated, resulting in the second set of strings to be translated. Then, based on a context exclusion mechanism, nodes belonging to the debugging context in the AST (such as the text corresponding to `console.log("Debug: Account Verification Failed")`) are identified, and "Debug: Account Verification Failed" is removed from the second set of strings to be translated, resulting in the third set of strings to be translated. Finally, based on intelligent string processing technology, the static tags "click" in the third set of strings to be translated are processed. <button> submit< / button> The "Complete Registration" message was processed by splitting and removing it. <button>、< / button> Static tags such as "click", "submit", and "complete registration" are retained, and only the core text to be translated, such as "click", "submit", and "complete registration", are integrated with the original "login", "please enter your account password", and "phone number / email" in the set. Finally, a clean set of strings to be translated is obtained: ["login", "please enter your account password", "phone number / email", "click", "submit", "complete registration"].
[0090] In this embodiment, the progressive processing of "character detection - rule exclusion - context removal - tag stripping" can accurately distinguish between the business text to be translated and redundant content that does not need to be translated. This not only avoids invalid text from entering the subsequent translation process, but also ensures the integrity of the core business text, greatly improving the accuracy of the set of strings to be translated and the efficiency of subsequent internationalization processing.
[0091] In one exemplary embodiment, such as Figure 2 As shown, a code internationalization method includes:
[0092] An abstract syntax tree (AST) is constructed for the file to be processed. Based on the AST, the set of target string nodes to be translated is extracted from the file. Regular expressions are used to perform character detection on the target string nodes to obtain the first set of strings to be translated. Based on resource extension detection rules and a configurable exclusion list, the content that does not need to be translated in the first set of strings to be translated is further filtered to obtain the second set of strings to be translated. Context exclusion mechanisms are used to remove debugging information from the second set of strings to be translated to obtain the third set of strings to be translated. String intelligent processing technology is used to remove static tags from the third set of strings to be translated to obtain the final set of strings to be translated. A pre-configured translation key generation mode is obtained. If the translation key generation mode is semantic mode, the complete storage path of the file to be processed is obtained. Path fragments are extracted from the complete storage path according to pre-configured path truncation rules. A unique identifier is generated for the file to be processed. A sequence number is assigned to each string to be translated according to the order of the strings to be translated in the file. A semantic translation key is generated based on the path fragment, unique identifier, and sequence number. The strings to be translated are associated with the corresponding semantic translation keys to form a set of key-value pairs to be translated. If the translation key generation mode is content deduplication mode, then the strings to be translated in the set of strings to be translated are processed to remove trailing whitespace, resulting in whitespace-removed strings; the whitespace-removed strings are processed by a hash algorithm to obtain hash values; the hash values are used as the content deduplication translation key of the strings to be translated, and the strings to be translated are associated with the corresponding content deduplication translation key to form a set of key-value pairs to be translated.If the file to be processed has the extension of type 1, then the first parser processes the file to obtain a template object, a script object, and a style object; a type 1 abstract syntax tree is constructed, and based on the type 1 abstract syntax tree, the first text node and the first attribute node in the template object that need internationalization are determined, and the semantic translation keys of the text node and attribute node are replaced with general internationalization function calls to obtain the internationalization modification code corresponding to the template object; the script object is processed through the first preset internationalization processing logic to obtain the internationalization modification code corresponding to the script object; the style object is processed through the second preset internationalization processing logic to obtain the internationalization modification code corresponding to the style object; if the file to be processed has the extension of type 2, then a type 2 abstract syntax tree is constructed. The process involves: identifying the second text node and second attribute node in the second type of abstract syntax tree; replacing the semantic translation keys of the second text node and second attribute node with general internationalization function calls to obtain the internationalization modification code corresponding to the second type; if the file extension to be processed is of the third type, then constructing the third type abstract syntax tree; based on the third type abstract syntax tree, determining the third node; if the third node is a third text node, then replacing the semantic translation keys of the third text node with general internationalization function calls to obtain the internationalization modification code corresponding to the third type; if the third node is both a third text node and a third attribute node, then replacing the semantic translation keys of both the third text node and the third attribute node with general internationalization function calls to obtain the internationalization modification code corresponding to the third type. The set of key-value pairs to be translated is stored in the base language pack in a preset directory.
[0093] For any target language in a multilingual dataset, a target language package is determined based on the base language package and the target language. By comparing the base and target language packages, missing key-value pairs to be translated in the target language package are identified, forming an incremental translation list. The incremental translation list is then translated into the target language using a translation engine to obtain the translated content. This translated content is then mapped to semantic translation keys and stored in the target language package. When internationalization is required, in response to the user's language switching operation, the target language to be translated is determined, and the translated content is read from the target language package corresponding to the target language through internationalization code modifications.
[0094] It should be understood that although the steps in the flowcharts of the above embodiments are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the above embodiments may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages in other steps. It is understood that the steps in different embodiments can be freely combined as needed, and all non-contradictory solutions formed by such combinations are within the scope of protection of this application.
[0095] In one exemplary embodiment, such as Figure 3 As shown, a code internationalization processing device is provided, including: an extraction module 301, a generation module 302, a processing module 303, a translation module 304, and a reading module 305, wherein:
[0096] The extraction module is used to construct the abstract syntax tree corresponding to the file to be processed. Based on the abstract syntax tree, it extracts the set of target string nodes that need to be translated from the file to be processed. It filters the content that does not need to be translated from the set of target string nodes through regular expressions, debugging context exclusion mechanism and intelligent string processing technology to obtain the set of strings to be translated.
[0097] The generation module is used to obtain the pre-configured translation key generation mode. If the translation key generation mode is semantic mode, it generates semantic translation keys based on the path fragment of the file to be processed, the unique identifier, and the sequence number of the string to be translated in the set of strings to be translated. It then associates the string to be translated with the corresponding semantic translation key to form a set of key-value pairs to be translated.
[0098] The processing module is used to determine the target parser based on the file extension of the file to be processed; the target parser replaces the semantic translation keys in the set of translation key-value pairs with general internationalization function calls to obtain the internationalization code; and stores the set of key-value pairs to be translated into the base language pack in a preset directory.
[0099] The translation module is used to perform multilingual translation of the base language pack through the translation engine, and to store the translated content into the corresponding language packs.
[0100] The reading module is used to respond to the user's language switching operation, determine the target language that needs to be translated, and read the translation content from the target language package corresponding to the target language through internationalization modification code.
[0101] In one exemplary embodiment, the processing module is further configured to:
[0102] If the file to be processed has the extension of type 1, then the first parser processes the file to obtain a template object, a script object, and a style object; a type 1 abstract syntax tree is constructed, and based on the type 1 abstract syntax tree, the first text node and the first attribute node in the template object that need internationalization are determined, and the semantic translation keys of the text node and attribute node are replaced with general internationalization function calls to obtain the internationalization modification code corresponding to the template object; the script object is processed through the first preset internationalization processing logic to obtain the internationalization modification code corresponding to the script object; the style object is processed through the second preset internationalization processing logic to obtain the internationalization modification code corresponding to the style object; if the file to be processed has the extension of type 2, then a type 2 abstract syntax tree is constructed. Tree; Identify the second text node and second attribute node in the second type of abstract syntax tree; Replace the semantic translation keys of the second text node and second attribute node with general internationalization function calls to obtain the internationalization modification code corresponding to the second type; If the file extension to be processed is of the third type, construct the third type abstract syntax tree; Based on the third type abstract syntax tree, determine the third node; If the third node is a third text node, replace the semantic translation key of the third text node with general internationalization function calls to obtain the internationalization modification code corresponding to the third type; If the third node is both a third text node and a third attribute node, replace the semantic translation keys of the third text node and the third attribute node with general internationalization function calls to obtain the internationalization modification code corresponding to the third type.
[0103] In one exemplary embodiment, the translation module is further configured to:
[0104] For any target language in a multilingual dataset, the target language package is determined based on the base language package and the target language. The base language package and the target language package are compared to identify the missing key-value pairs to be translated in the target language package, forming an incremental translation list. The incremental translation list is translated into the target language by the translation engine to obtain the translated content. The translated content is then mapped to semantic translation keys and stored in the target language package.
[0105] In one exemplary embodiment, the generation module is further configured to:
[0106] If the translation key generation mode is content deduplication mode, then the strings to be translated in the set of strings to be translated are processed to remove trailing whitespace, resulting in whitespace-removed strings; the whitespace-removed strings are processed by a hash algorithm to obtain hash values; the hash values are used as the content deduplication translation key of the strings to be translated, and the strings to be translated are associated with the corresponding content deduplication translation key to form a set of key-value pairs to be translated.
[0107] In one exemplary embodiment, the generation module is further configured to:
[0108] Obtain the complete storage path of the file to be processed, and extract path fragments from the complete storage path according to the pre-configured path truncation rules; generate a unique identifier for the file to be processed; assign a sequence number to each string to be translated according to the order of the strings to be translated in the file; generate a semantic translation key based on the path fragment, unique identifier, and sequence number; associate the strings to be translated with the corresponding semantic translation key to form a set of key-value pairs to be translated.
[0109] In one exemplary embodiment, the extraction module is further configured to:
[0110] By using regular expressions to perform character detection on the target string nodes, a first set of strings to be translated is obtained. Based on resource extension detection rules and a configurable exclusion list, the content in the first set of strings to be translated that does not need to be translated is further filtered to obtain a second set of strings to be translated. Debugging information in the second set of strings to be translated is removed based on a context exclusion mechanism to obtain a third set of strings to be translated. Static tags in the third set of strings to be translated are removed based on intelligent string processing technology to obtain the final set of strings to be translated.
[0111] The modules in the aforementioned code internationalization processing device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in the processor of a computer device in hardware form or independent of it, or stored in the memory of the computer device in software form, so that the processor can call and execute the operations corresponding to each module.
[0112] In one exemplary embodiment, a computer device is provided, which may be a terminal, and its internal structure diagram may be as follows: Figure 4 As shown, the computer device includes a processor, memory, input / output interfaces, a communication interface, a display unit, and an input device. The processor, memory, and input / output interfaces are connected via a system bus, and the communication interface, display unit, and input device are also connected to the system bus via the input / output interfaces. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The input / output interfaces are used for exchanging information between the processor and external devices. The communication interface is used for wired or wireless communication with external terminals; wireless communication can be achieved through Wi-Fi, mobile cellular networks, Near Field Communication (NFC), or other technologies. When the computer program is executed by the processor, it implements a code internationalization processing method. The display unit is used to form a visually visible image and can be a display screen, a projection device, or a virtual reality imaging device. The display screen can be an LCD screen or an e-ink screen. The input device of the computer device can be a touch layer covering the display screen, or buttons, trackballs, or touchpads set on the casing of the computer device, or external keyboards, touchpads, or mice, etc.
[0113] Those skilled in the art will understand that Figure 4 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0114] In one exemplary embodiment, a computer device is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps in the above-described method embodiments.
[0115] In one embodiment, a computer-readable storage medium is provided having a computer program stored thereon that, when executed by a processor, implements the steps in the above method embodiments.
[0116] In one embodiment, a computer program product is provided, including a computer program that, when executed by a processor, implements the steps in the above method embodiments.
[0117] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. Any references to memory, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile memory and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can take many forms, such as Static Random Access Memory (SRAM) or Dynamic Random Access Memory (DRAM). The databases involved in the embodiments provided in this application may include at least one type of relational database and non-relational database. Non-relational databases may include, but are not limited to, blockchain-based distributed databases. The processors involved in the embodiments provided in this application may be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, quantum computing-based data processing logic devices, artificial intelligence (AI) processors, etc., and are not limited to these.
[0118] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this application.
[0119] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of this patent application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this application should be determined by the appended claims.< / stylescoped> < / scriptsetup> < / stylescoped> < / scriptsetup> < / template>
Claims
1. A method of code internationalization processing, characterized by, The method comprises: constructing an abstract syntax tree corresponding to a to-be-processed file, extracting a target string node set requiring translation from the to-be-processed file based on the abstract syntax tree, filtering content not requiring translation in the target string node set through a regular expression, a debugging context exclusion mechanism and a string intelligent processing technology, and obtaining a to-be-translated string set; obtaining a preconfigured translation key generation mode, generating a semantic translation key based on a path segment of the to-be-processed file, a unique identifier and a serial number of a to-be-translated string in the to-be-translated string set if the translation key generation mode is a semantic mode, associating the to-be-translated string with the corresponding semantic translation key, and forming a to-be-translated key-value pair set; determining a target parser based on an extension of the to-be-processed file, replacing the semantic translation key in the to-be-translated key-value pair set with a general internationalization function call through the target parser, obtaining internationalization reconstruction code, and storing the to-be-translated key-value pair set in a basic language package under a preset directory; performing multilingual translation on the basic language package through a translation engine, and storing the translated content in a corresponding plurality of language packages; when code internationalization processing is required, determining a target language requiring translation in response to a language switching operation of a user, and reading translation content in a target language package corresponding to the target language through the internationalization reconstruction code.
2. The method of claim 1, wherein, The target parser is determined based on the extension of the to-be-processed file. The semantic translation key in the to-be-translated key-value pair set is replaced with a general internationalization function call through the target parser to obtain internationalization reconstruction code, which comprises: if the extension of the to-be-processed file is of a first type, processing the to-be-processed file through a first parser to obtain a template object, a script object and a style object, constructing a first type abstract syntax tree, determining a first text node and a first attribute node requiring internationalization processing in the template object based on the first type abstract syntax tree, and replacing the semantic translation key of the text node and the attribute node with a general internationalization function call to obtain internationalization reconstruction code corresponding to the template object, processing the script object through a first preset internationalization processing logic to obtain internationalization reconstruction code corresponding to the script object, and processing the style object through a second preset internationalization processing logic to obtain internationalization reconstruction code corresponding to the style object; if the extension of the to-be-processed file is of a second type, constructing a second type abstract syntax tree, identifying a second text node and a second attribute node in the second type abstract syntax tree, and replacing the semantic translation key of the second text node and the second attribute node with a general internationalization function call to obtain internationalization reconstruction code corresponding to the second type; If the extension of the to-be-processed file is of a third type, a third type abstract syntax tree is constructed; based on the third type abstract syntax tree, a third node is determined; if the third node is a third text node, a semantic translation key of the third text node is replaced with a general internationalization function call to obtain internationalization reconstruction code corresponding to the third type; if the third node is a third text node and a third attribute node, semantic translation keys of the third text node and the third attribute node are replaced with a general internationalization function call to obtain internationalization reconstruction code corresponding to the third type.
3. The method of claim 1, wherein, The multilingual translation of the basic language package by the translation engine and the storage of the translated content in the corresponding multiple language packages after multilingual translation comprise: For any target language in the multiple languages, a target language package is determined based on the basic language package and the target language; By comparing the basic language package and the target language package, a missing to-be-translated key-value pair in the target language package is determined to form an incremental translation list; The incremental translation list is translated into the target language by the translation engine to obtain translated content, and the translated content and the semantic translation key are stored in the target language package after correspondence.
4. The method of claim 1, wherein, The method further comprises: If the translation key generation mode is a content deduplication mode, the to-be-translated strings in the to-be-translated string set are processed to remove trailing spaces to obtain deduplicated strings; The deduplicated strings are processed by a hash algorithm to obtain hash values; The hash values are used as content deduplication translation keys of the to-be-translated strings to associate the to-be-translated strings with the corresponding content deduplication translation keys to form a to-be-translated key-value pair set.
5. The method of claim 1, wherein, The semantic translation key is generated based on the path segment of the to-be-processed file, the unique identifier, and the serial number of the to-be-translated string in the to-be-translated string set, comprising: The complete storage path of the to-be-processed file is obtained, and a path segment is extracted from the complete storage path according to a pre-configured path extraction rule; A unique identifier is generated for the to-be-processed file; and a serial number is assigned to each to-be-translated string in the to-be-translated string set according to the order of the to-be-translated strings in the file; The semantic translation key is generated based on the path segment, the unique identifier, and the serial number; The to-be-translated string is associated with the corresponding semantic translation key to form a to-be-translated key-value pair set.
6. The method of claim 1, wherein, The to-be-translated string set is obtained by filtering the content that does not need to be translated in the target string node set by using a regular expression, a debugging context exclusion mechanism, and a string intelligent processing technology, comprising: The first to-be-translated string set is obtained by character detection on the target string node by using a regular expression; The second to-be-translated string set is obtained by further filtering the content that does not need to be translated in the first to-be-translated string set based on a resource extension name detection rule and a configured exclusion list; The third to-be-translated string set is obtained by removing the debugging information in the second to-be-translated string set based on a context exclusion mechanism; The to-be-translated string set is obtained by removing the static labels in the third to-be-translated string set based on a string intelligent processing technology.
7. A code internationalization processing apparatus characterized by comprising: The device comprises: The extraction module is configured to construct an abstract syntax tree corresponding to a to-be-processed file, extract a set of target string nodes that need to be translated from the to-be-processed file based on the abstract syntax tree, filter out content that does not need to be translated in the set of target string nodes by using a regular expression, a debugging context exclusion mechanism, and a string intelligent processing technology, and obtain a set of to-be-translated strings. The generation module is configured to obtain a preconfigured translation key generation mode, generate a semantic translation key based on a path segment, a unique identifier, and a serial number of a to-be-translated string in the set of to-be-translated strings if the translation key generation mode is a semantic mode, associate the to-be-translated string with the corresponding semantic translation key, and form a set of to-be-translated key-value pairs. The processing module is configured to determine a target parser based on an extension of the to-be-processed file, replace the semantic translation key in the set of to-be-translated key-value pairs with a universal internationalization function call by using the target parser, obtain internationalization reconstruction code, and store the set of to-be-translated key-value pairs in a base language package in a preset directory. The translation module is configured to perform multilingual translation on the base language package by using a translation engine, and store translated content in a plurality of language packages. The reading module is configured to determine a target language that needs to be translated in response to a language switching operation of a user, and read translated content in a target language package corresponding to the target language by using the internationalization reconstruction code.
8. A computer device comprising a memory and a processor, the memory storing a computer program, characterized in that, The computer program is executed by the processor to implement the steps of the method in any one of claims 1 to 6.
9. A computer readable storage medium having stored thereon a computer program, characterized in that, The computer program is executed by the processor to implement the steps of the method in any one of claims 1 to 6.
10. A computer program product comprising a computer program, characterized in that, The computer program is executed by the processor to implement the steps of the method in any one of claims 1 to 6.