A code processing method, apparatus and electronic device
Patent Information
- Application Number
- CN202610878003.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-06-16
- Publication Date
- 2026-10-09
AI Technical Summary
[0010]为了使本申请所解决的技术问题、技术方案及有益效果更加清楚明白,以下结合实施例,对本申请进行进一步详细说明。应当理解,此处所描述的具体实施例仅仅用以解释本申请,并不用于限定本申请。
Smart Images

Figure CN122884447A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of code compilation technology, and in particular to a code processing method, apparatus and electronic device. Background Technology
[0002] With the development of front-end development technologies, multilingual adaptation has become a core capability supporting the globalization of products. However, existing multilingual implementations only support automatic replacement of static text and cannot effectively handle complex scenarios involving dynamic expressions. During the replacement process, the original code's syntax structure is easily broken, leading to translation failures. Summary of the Invention
[0003] This application provides a code processing method, apparatus, and electronic device, aiming to improve the problem that the automatic replacement process easily destroys the original code's syntactic structure, leading to translation failure.
[0004] In a first aspect, embodiments of this application provide a code processing method, including: Identify basic language text and dynamic interpolation in the source code to be processed; Obtain the translation identifier key corresponding to the basic language text and the placeholder corresponding to the dynamic interpolation; Construct an internationalized function call expression based on the translation identifier key corresponding to the basic language text and the placeholder corresponding to the dynamic interpolation; Based on the source code to be processed and the internationalized function call expression, target code is generated.
[0005] The embodiments described above in this application can distinguish between static base language text and dynamic interpolation by automatically identifying them in the source code during compilation. By obtaining the translation identifier key corresponding to the base language text and the placeholder for the dynamic interpolation, static text and dynamic interpolation can be decoupled, avoiding the direct concatenation of dynamic interpolation into static text in the code, which would disrupt the original syntactic structure. Furthermore, based on the translation identifier key corresponding to the base language text and the placeholder for the dynamic interpolation, an internationalized function call expression is constructed. Target code is generated based on the source code and the internationalized function call expression. This not only eliminates the need to modify the source code but also avoids translation failures caused by treating code logic as text translation, thus improving translation accuracy.
[0006] Secondly, embodiments of this application provide a code processing apparatus, including: The recognition module is used to identify basic language text and dynamic interpolation in the source code to be processed. The acquisition module is used to acquire the translation identifier key corresponding to the basic language text and the placeholder corresponding to the dynamic interpolation; The construction module is used to construct an internationalized function call expression based on the translation identifier key corresponding to the basic language text and the placeholder corresponding to the dynamic interpolation; The generation module is used to generate target code based on the source code to be processed and the internationalized function call expression.
[0007] Thirdly, embodiments of this application provide an electronic device, including a processor and a memory, wherein... Memory, used to store computer programs; A processor is used to execute a program stored in memory to implement the method described in the first aspect.
[0008] Fourthly, embodiments of this application also provide a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the method described in the first aspect. Attached Figure Description
[0009] Figure 1 This is a flowchart of a code processing method provided in an embodiment of this application; Figure 2 This is a flowchart illustrating a specific code processing method provided in an embodiment of this application; Figure 3 This is a structural diagram of the code processing apparatus provided in the embodiments of this application; Figure 4 This is a structural diagram of the electronic device provided in the embodiments of this application. Detailed Implementation
[0010] To make the technical problems, technical solutions, and beneficial effects solved by this application clearer, the following detailed description is provided in conjunction with embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0011] This application provides a code processing method, including: Identify basic language text and dynamic interpolation in the source code to be processed; Obtain the translation identifier key corresponding to the basic language text and the placeholder corresponding to the dynamic interpolation; Construct an internationalized function call expression based on the translation identifier key corresponding to the basic language text and the placeholder corresponding to the dynamic interpolation; Based on the source code to be processed and the internationalized function call expression, target code is generated.
[0012] Specifically, the front-end build tool uses Vite, Webpack, or RollUp. During the build cycle of this tool, a pre-defined preprocessing plugin intercepts source code files, limiting the interception to "JSX" and "TSX" format files in the source code directory. This plugin executes filtering logic to filter non-business files and dependency directories (node_modules). Specifically, this plugin is configured with a priority execution flag (e.g., `enforce: "pre"`) to trigger it in the early stages of the build process. Through this configuration, the plugin only processes "JSX" and "TSX" format files in the business source code, avoiding unnecessary interference with third-party dependencies and non-business files.
[0013] Regular expressions are used to identify the base language text and dynamic interpolation in the source code to be processed. Dynamic interpolation includes at least one of the following: ${variable}, expressions, numeric constants, function calls, etc., to distinguish between static text that needs translation and dynamic interpolation that does not. Translation identifier keys corresponding to the base language text and placeholders corresponding to the dynamic interpolation are obtained, thus decoupling static text from dynamic interpolation and avoiding the direct concatenation of dynamic interpolation into static text in the code, which would disrupt the original grammatical structure. Based on the translation identifier keys corresponding to the base language text and the placeholders corresponding to the dynamic interpolation, an internationalized function call expression is constructed with the translation identifier keys and placeholders as parameters. Multilingual replacement target code is generated based on the source code to be processed and the internationalized function call expression. The source code does not need to be modified throughout the process; the development environment directly returns the source code without performing any replacements, ensuring fast hot updates. Furthermore, it automatically generates multilingual target code without manual text extraction, translation hook injection, or manual sentence-by-sentence translation, resulting in a non-intrusive development experience and no new dependencies at runtime. This significantly reduces the cost of multilingual transformation and improves engineering efficiency and code stability.
[0014] The above solution automatically identifies static base language text and dynamic interpolation in the source code during compilation, thus distinguishing between them. By obtaining the translation identifier key corresponding to the base language text and the placeholder for the dynamic interpolation, static text and dynamic interpolation can be decoupled, avoiding the direct concatenation of dynamic interpolation into static text in the code, which would disrupt the original syntactic structure. Furthermore, based on the translation identifier key and the placeholder for the dynamic interpolation, an internationalized function call expression is constructed. Target code is generated based on the source code and the internationalized function call expression. This not only eliminates the need to modify the source code but also avoids translation failures caused by treating code logic as text translation, improving translation accuracy.
[0015] Example 1 This application provides a code processing method, please refer to the embodiments therein. Figure 1 This includes the following steps: S101: Identify basic language text and dynamic interpolation in the source code to be processed; S102: Obtain the translation identifier key corresponding to the basic language text and the placeholder corresponding to the dynamic interpolation; S103: Construct an internationalized function call expression based on the translation identifier key corresponding to the basic language text and the placeholder corresponding to the dynamic interpolation; S104: Generate target code based on the source code to be processed and the internationalized function call expression.
[0016] The embodiments described above in this application can distinguish between static base language text and dynamic interpolation by automatically identifying them in the source code during compilation. By obtaining the translation identifier key corresponding to the base language text and the placeholder for the dynamic interpolation, static text and dynamic interpolation can be decoupled, avoiding the direct concatenation of dynamic interpolation into static text in the code, which would disrupt the original syntactic structure. Furthermore, based on the translation identifier key corresponding to the base language text and the placeholder for the dynamic interpolation, an internationalized function call expression is constructed. Target code is generated based on the source code and the internationalized function call expression. This not only eliminates the need to modify the source code but also avoids translation failures caused by treating code logic as text translation, thus improving translation accuracy.
[0017] In an optional specific embodiment, step S101 identifies the basic language text and dynamic interpolation in the source code to be processed, including: The source code to be processed is parsed to generate the corresponding abstract syntax tree; Identify the first target node in the abstract syntax tree, wherein the target node includes at least one of the following: string literal node, JSX attribute node, JSX text node, template literal node; A second target node is selected from the first target node; the second target node is the first target node containing the basic language text, and / or the second target node is the first target node containing the basic language text and the dynamic interpolation; Extract the basic language text and the dynamic interpolation contained in the second target node.
[0018] Specifically, based on front-end build tools (such as Vite), the parsing module (@babel / parser) in the Babel parser is invoked to perform lexical and syntactic analysis on the source code files, generating an Abstract Syntax Tree (AST) compatible with the standard ESTree specification. This approach is suitable for projects developed based on the React framework, whose source code file formats include JSX and TSX. During the parser's syntactic analysis, TypeScript, JSX, and decorator parsing capabilities are enabled. The parsing process preserves the original source code location information, node indices, and context information, ensuring accurate subsequent replacement operations without disrupting the code structure. With this configuration, the parser is compatible with the ESNext specification, TypeScript extended syntax, and JSX extended syntax.
[0019] Based on the front-end build tool, the traversal module (@babel / traverse) provided by the Babel parser is invoked to perform a depth-first traversal of the AST, accurately and automatically identifying the first target node according to the syntax node type. This first target node includes at least one of the following: StringLiteral, JSXAttribute, JSX Text, and TemplateLiteral. StringLiteral represents plain string text enclosed in single or double quotes, such as "xxx" or 'xxx'. JsxAttribute represents attributes within JSX tags, and its form includes attribute names equal to string values or attribute names equal to expressions enclosed in curly braces, such as attr="value" or attr={expr}. JsxText refers to the plain text content between JSX tags. TemplateLiteral represents a template string enclosed in backticks, including static fragment sub-nodes (Quasi), expression sub-nodes (Expression), member expression nodes (MemberExpression), and call expression nodes (CallExpression). The member expression nodes and call expression nodes contain string literals or template literal structures. Specifically, the static fragment sub-nodes correspond to the static text portion of the template string, and the expression sub-nodes correspond to the interpolation expression portion of the template string.
[0020] The above embodiments accurately and automatically identify the first target node by node type, instead of matching text with regular expressions, which can avoid mismatches and rollbacks in regular expressions and improve parsing efficiency.
[0021] During the traversal, nodes with pure dynamic interpolation and no basic language text are filtered out, as are nodes without dynamic interpolation and no basic language text. Additionally, comment nodes, empty text nodes, pure number nodes, and pure symbol nodes are filtered out to avoid wasting time translating pure variables, reduce invalid traversal and misidentification, and retain the first target node containing only basic language text as the second target node, and retain the first target node containing both basic language text and dynamic interpolation as the second target node.
[0022] For a second target node containing only basic language text, extract the basic language text from that target node. For a second target node containing both basic language text and dynamic interpolation, extract both the basic language text and dynamic interpolation from that target node. This allows for the automatic identification of static basic language text and dynamic interpolation in the source code during compilation, thus distinguishing between static text and dynamic interpolation.
[0023] In one optional embodiment, the source code to be processed includes at least one of the following: The source code compiled for the first time; Source code that has not been compiled for the first time and whose content has changed compared to the previous compilation.
[0024] Specifically, for multiple source code files to be compiled, a file hash cache is established, that is, a hash value (similar to a file fingerprint) is generated for each source code file, and file-level incremental caching is enabled. For source code files being compiled for the first time, internationalization processing is required, so these source code files are treated as pending source code files. For source code files not being compiled for the first time, if the hash value indicates that the source code file has undergone content changes compared to the previous compilation, internationalization processing is performed on these source code files, so they are treated as pending source code files. For source code files not being compiled for the first time, if the file content has not changed compared to the previous compilation, no processing is required; parsing of these source code files is skipped during compilation, and the result of the previous compilation cache is used, which can significantly improve the speed of secondary compilation.
[0025] In an optional specific embodiment, step S102, obtaining the translation identifier key corresponding to the basic language text and the placeholder corresponding to the dynamic interpolation, includes: Perform hash calculation on the base language text to generate a translation identifier key corresponding to the base language text, and map the base language text to the corresponding translation identifier key; The variable name of the dynamic interpolation is parsed to generate a placeholder corresponding to the variable name, and the dynamic interpolation is mapped to the corresponding placeholder.
[0026] Specifically, for the second target node containing only basic language text, the basic language text is normalized and then hashed to automatically generate a globally unique and stable translation identifier key, and the basic language text is mapped to its corresponding translation identifier key.
[0027] For the second target node containing basic language text and dynamic interpolation, the basic language text is normalized and then hashed to automatically generate a globally unique and stable translation identifier key. The basic language text is then mapped to its corresponding translation identifier key. Furthermore, the variable names of the dynamic interpolation are automatically parsed to generate placeholders for those variable names, such as {{var}}, and the dynamic interpolation is mapped to its corresponding placeholder. This decouples static text from dynamic interpolation, avoiding the direct concatenation of dynamic interpolation into static text in the code, which would disrupt the original syntactic structure.
[0028] In one embodiment, the translation key is formatted as: {filename prefix}__{normalized base language text}__{hash value}, such as: app__xxx_123456, where app refers to the filename prefix; xxx represents the normalized base language text; and 123456 represents the hash value.
[0029] In an optional specific embodiment, step S104 generates target code based on the source code to be processed and the internationalized function call expression, including: The basic language text and the dynamic interpolation contained in the second target node are replaced with the corresponding internationalized function call expressions to obtain the updated abstract syntax tree; Target code is generated based on the updated abstract syntax tree.
[0030] Specifically, for a second target node containing only the basic language text, an internationalized function call expression is constructed with the translation identifier key corresponding to the basic language text as a parameter, and the basic language text is replaced with the corresponding internationalized function call expression, such as: i18n.t(key), where key is app__xxx_123456. Then the second target node can be updated to: i18n.t('app__xxx_123456'), thereby updating the AST.
[0031] For the second target node containing base language text and dynamic interpolation, an internationalized function call expression is constructed with the translation identifier key corresponding to the base language text and the placeholder corresponding to the dynamic interpolation as parameters, such as: i18n.t(key,{var1, var2}). The base language text and dynamic interpolation are then replaced with the corresponding internationalized function call expression to update the AST. Here, {var1, var2} represents placeholders corresponding to two different variable names.
[0032] After all the second target nodes have been updated, the updated AST is obtained, and the target code is generated based on the updated AST.
[0033] The above embodiments replace the basic language text and the included dynamic interpolation with the corresponding internationalized function call expressions. While keeping the original business code unchanged, without destroying the original expression structure, and without introducing syntax errors, the embodiments solve problems such as undefined internationalized functions, abnormal rendering timing, and cross-module reuse errors.
[0034] In an optional specific embodiment, the method further includes: In response to the language selection input for the current functional module, determine the target language package; Load the sub-target language package corresponding to the current functional module from the target language package; If the translation result corresponding to the translation identifier key is found in the sub-target language package, the translation result is taken as the target language translation result; If no translation result corresponding to the translation identifier key is found in the sub-target language package, the basic language text corresponding to the translation identifier key is translated into multiple languages by calling the machine translation interface to obtain a multilingual translation result; the multilingual translation result includes the target language translation result and other language translation results.
[0035] Specifically, it receives the user's language selection input for the currently running functional module, determines the selected target language, and determines the target language package corresponding to the target language.
[0036] For each language package, namespaces are automatically partitioned according to functional modules, generating segmented language packages (i.e., sub-language packages). When running the current functional module, only the sub-language package corresponding to the current functional module is loaded, instead of loading all language packages, which reduces the initial screen size. Functional modules include: business paths, pages, or components. Therefore, only the sub-target language package corresponding to the current functional module within the target language package is loaded, and the translation result corresponding to the translation identifier key is found within the sub-target language package.
[0037] If a translation result corresponding to the translation identifier key is found in the sub-target language package, that result is used as the target language translation result. If no translation result corresponding to the translation identifier key is found in the sub-target language package, the general machine translation interface is automatically invoked to perform multilingual translation on the base language text corresponding to the translation identifier key, resulting in a multilingual translation result; that is, there is one translation result for each language. For the target language, the language translation result of the base language text is the target language translation result. For languages other than the target language, the language translation result of the base language text is the translation result for that other language.
[0038] In one embodiment, the general machine translation interface can be a cloud-based application programming interface (API) or a local machine translation model.
[0039] In one embodiment, a caching mechanism is added to the machine translation, so that the same language is translated only once, avoiding repeated calls to the translation interface.
[0040] In an optional specific embodiment, the method further includes: If the translation result corresponding to the translation identifier key is found in the sub-target language package, it is prohibited to write the translation identifier key and the corresponding target language translation result into the sub-target language package; If the translation result corresponding to the translation identifier key is not found in the sub-target language package, the translation identifier key and the corresponding translation result for each language are written into the sub-language package corresponding to the current functional module in each language package.
[0041] Specifically, if the translation result corresponding to the translation identifier key is found in the sub-target language package, in order to avoid redundant translation results due to repeated writing, it is prohibited to write the translation identifier key and the corresponding target language translation result into the sub-target language package.
[0042] If the translation result corresponding to the translation identifier key is not found in the sub-target language package, the translation identifier key and the corresponding translation result for each language are written into the sub-language package corresponding to the current functional module in each language package to support incremental updates of the sub-language package.
[0043] In one embodiment, the same translation identifier keys and corresponding translation results of different functional modules are merged to compress the language packages. After the target code is generated, the language packages are persisted in batches to reduce input / output (IO). That is, when generating translation results, all translation results are first stored in memory and then written to the hard disk at once to avoid frequent writes to the hard disk, which would significantly reduce the compilation speed.
[0044] In one embodiment, the global replacement function and language pack update function are disabled in the development environment, and only the basic prompt function is retained. That is, the source code content is not automatically modified or translation results are generated in the development environment; only prompt information is displayed to avoid interfering with normal development and debugging and causing performance loss. Internationalization replacement and language pack updates are only performed when the source code is packaged and released online.
[0045] like Figure 2 As shown, the above code processing procedure will be explained below through a specific embodiment: Taking a certain enterprise-level management backend system as an example, the system uses the React front-end framework and enables TypeScript syntax parsing capabilities.
[0046] S201: Obtain the project's source code. The system's source code is as follows: it contains approximately 800 JSX or TSX format source code files, with a total of approximately 120,000 lines of source code. At the user interface level, the system uses the Ant Design component library to build page components.
[0047] S202: Create a Vite plugin in the project directory. This plugin can be named vite-plugin-auto-i18n and its file format is a JavaScript file (i.e., a .js file).
[0048] Before running the plugin, the following dependency modules need to be installed: Crypto-js is a library for encrypting and decrypting data, such as hash algorithms. The Babel core library (@babel / core) provides the fundamental capabilities for syntax parsing and target code generation. Babel's JSX syntax parsing plugin (@babel / plugin-syntax-jsx) is used to support the parsing and transformation of JSX syntax.
[0049] S203: Based on the Vite plugin, the parsing module (@babel / parser) in the Babel parser is called to perform lexical and syntactic analysis on the source code file. The parsing process enables TypeScript syntactic analysis capabilities, JSX syntactic analysis capabilities, and decorator syntactic analysis capabilities to generate an abstract syntax tree that is compatible with the standard ESTree specification.
[0050] S204: Based on the front-end build tool, it calls the traversal module (@babel / traverse) provided by the Babel parser to perform a depth-first traversal of the abstract syntax tree, automatically identify the first target node according to the syntax node type, and select the second target node from the first target node.
[0051] S205: Extract the basic language text and the dynamic interpolation contained in the second target node, perform hash calculation on the basic language text to generate the corresponding translation identifier key, parse the variable names of the dynamic interpolation to generate the corresponding placeholders, and construct an internationalization function call expression with the translation identifier key and placeholders as parameters.
[0052] S206: Replace the basic language text and the dynamic interpolation contained in the second target node with the corresponding internationalized function call expression to obtain the updated abstract syntax tree.
[0053] S207: Generate target code based on the updated abstract syntax tree.
[0054] S208: After S206, load the sub-target language package corresponding to the current functional module from the target language package.
[0055] S209: If no translation result corresponding to the translation identifier key is found in the sub-target language package, perform multilingual translation on the basic language text corresponding to the translation identifier key to obtain multilingual translation results. Write the translation identifier key and the corresponding translation results in each language package into the sub-language package corresponding to the current functional module.
[0056] S210: When running the target code, the translation results from the multilingual package are invoked and displayed using an internationalized function call expression.
[0057] In the above embodiments, with a code scale of 120,000 lines, if the traditional regular expression replacement method is used, the time to build the target code in one go is about 28-32 seconds; using the solution of this application embodiment, the time to build the complete target code in one go is about 16-19 seconds, and the build time is reduced by an average of about 35%-40%. For traditional multilingual projects, the first screen loading time is about 4.2-4.8 seconds; using the solution of this application embodiment, the first screen loading time is about 2.9-3.3 seconds, and the first screen loading time is reduced by an average of about 25%-30%. The solution of this application embodiment automatically generates standardized keys and language packages, reducing manual intervention costs by more than 80%. Subsequent additions of translation content or adjustments to the translation scheme can be iterated through a unified interface and build process, significantly reducing the maintenance cost of multilingual versions.
[0058] This application also provides a code processing device 300, please refer to... Figure 3 ,include: The recognition module 301 is used to recognize the basic language text and dynamic interpolation in the source code to be processed; The acquisition module 302 is used to acquire the translation identifier key corresponding to the basic language text and the placeholder corresponding to the dynamic interpolation; Module 303 is used to construct an internationalized function call expression based on the translation identifier key corresponding to the basic language text and the placeholder corresponding to the dynamic interpolation; The generation module 304 is used to generate target code based on the source code to be processed and the internationalized function call expression.
[0059] Optionally, when identifying basic language text and dynamic interpolation in the source code to be processed, the recognition module 301 is specifically used for: The source code to be processed is parsed to generate the corresponding abstract syntax tree; Identify the first target node in the abstract syntax tree, wherein the target node includes at least one of the following: string literal node, JSX attribute node, JSX text node, template literal node; A second target node is selected from the first target node; the second target node is the first target node containing the basic language text, and / or the second target node is the first target node containing the basic language text and the dynamic interpolation; Extract the basic language text and the dynamic interpolation contained in the second target node.
[0060] Optionally, the source code to be processed includes at least one of the following: The source code compiled for the first time; Source code that has not been compiled for the first time and whose content has changed compared to the previous compilation.
[0061] Optionally, when acquiring the translation identifier key corresponding to the basic language text and the placeholder corresponding to the dynamic interpolation, the acquisition module 302 is specifically used for: Perform hash calculation on the base language text to generate a translation identifier key corresponding to the base language text, and map the base language text to the corresponding translation identifier key; The variable name of the dynamic interpolation is parsed to generate a placeholder corresponding to the variable name, and the dynamic interpolation is mapped to the corresponding placeholder.
[0062] Optionally, when generating target code based on the source code to be processed and the internationalized function call expression, the generation module 304 is specifically used for: The basic language text and the dynamic interpolation contained in the second target node are replaced with the corresponding internationalized function call expressions to obtain the updated abstract syntax tree; Target code is generated based on the updated abstract syntax tree.
[0063] Optionally, the device further includes a processing module for: In response to the language selection input for the current functional module, determine the target language package; Load the sub-target language package corresponding to the current functional module from the target language package; If the translation result corresponding to the translation identifier key is found in the sub-target language package, the translation result is taken as the target language translation result; If no translation result corresponding to the translation identifier key is found in the sub-target language package, the basic language text corresponding to the translation identifier key is translated into multiple languages by calling the machine translation interface to obtain a multilingual translation result; the multilingual translation result includes the target language translation result and other language translation results.
[0064] Optionally, the processing module is further configured to: If the translation result corresponding to the translation identifier key is found in the sub-target language package, it is prohibited to write the translation identifier key and the corresponding target language translation result into the sub-target language package; If the translation result corresponding to the translation identifier key is not found in the sub-target language package, the translation identifier key and the corresponding translation result for each language are written into the sub-language package corresponding to the current functional module in each language package.
[0065] This application also provides an electronic device 400, please refer to... Figure 4 It includes a memory 410 and a processor 420, wherein the memory 410 is used to store computer programs; and the processor 420 is used to execute the programs stored in the memory 410 to implement the code processing method described in any embodiment of this application.
[0066] This application also provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the code processing method described in any embodiment of this application.
[0067] In this application, "multiple" refers to two or more.
[0068] In this application, unless otherwise expressly defined, the terms "installation," "connection," and "linking" should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral connection; they can refer to a mechanical connection or an electrical connection; they can refer to a direct connection or an indirect connection through an intermediate medium; and they can refer to the internal connection between two components. Those skilled in the art can understand the specific meaning of the above terms in this application based on the specific circumstances.
[0069] The terms “first,” “second,” “third,” “fourth,” etc., in this application (if present) are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence.
[0070] In this application, the term "and / or" is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, or B existing alone. Additionally, in this application, the character " / " generally indicates that the preceding and following related objects have an "or" relationship.
[0071] Unless otherwise specified, all steps in this application may be performed sequentially or randomly. For example, if the method includes steps A and B, it means that the method may include steps A and B performed sequentially, or it may include steps B and A performed sequentially. For example, if the method may also include step C, it means that step C may be added to the method in any order. For example, the method may include steps A, B, and C, or it may include steps A, C, and B, or it may include steps C, A, and B, etc.
[0072] The above description is merely a preferred embodiment of this application and is not intended to limit this application. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this application should be included within the protection scope of this application.
Claims
1. A code processing method, characterized in that, include: Identify basic language text and dynamic interpolation in the source code to be processed; Obtain the translation identifier key corresponding to the basic language text and the placeholder corresponding to the dynamic interpolation; Construct an internationalized function call expression based on the translation identifier key corresponding to the basic language text and the placeholder corresponding to the dynamic interpolation; Based on the source code to be processed and the internationalized function call expression, target code is generated.
2. The method according to claim 1, characterized in that, The identification of basic language text and dynamic interpolation in the source code to be processed includes: The source code to be processed is parsed to generate the corresponding abstract syntax tree; Identify the first target node in the abstract syntax tree, wherein the target node includes at least one of the following: string literal node, JSX attribute node, JSX text node, template literal node; A second target node is selected from the first target node; the second target node is the first target node containing the basic language text, and / or the second target node is the first target node containing the basic language text and the dynamic interpolation; Extract the basic language text and the dynamic interpolation contained in the second target node.
3. The method according to claim 1 or 2, characterized in that, The source code to be processed includes at least one of the following: The source code compiled for the first time; Source code that has not been compiled for the first time and whose content has changed compared to the previous compilation.
4. The method according to claim 1, characterized in that, The step of obtaining the translation identifier key corresponding to the basic language text and the placeholder corresponding to the dynamic interpolation includes: Perform hash calculation on the base language text to generate a translation identifier key corresponding to the base language text, and map the base language text to the corresponding translation identifier key; The variable name of the dynamic interpolation is parsed to generate a placeholder corresponding to the variable name, and the dynamic interpolation is mapped to the corresponding placeholder.
5. The method according to claim 2, characterized in that, The process of generating target code based on the source code to be processed and the internationalized function call expression includes: The basic language text and the dynamic interpolation contained in the second target node are replaced with the corresponding internationalized function call expressions to obtain the updated abstract syntax tree; Target code is generated based on the updated abstract syntax tree.
6. The method according to claim 1, characterized in that, The method further includes: In response to the language selection input for the current functional module, determine the target language package; Load the sub-target language package corresponding to the current functional module from the target language package; If the translation result corresponding to the translation identifier key is found in the sub-target language package, the translation result is taken as the target language translation result; If no translation result corresponding to the translation identifier key is found in the sub-target language package, the basic language text corresponding to the translation identifier key is translated into multiple languages by calling the machine translation interface to obtain a multilingual translation result; the multilingual translation result includes the target language translation result and other language translation results.
7. The method according to claim 6, characterized in that, The method further includes: If the translation result corresponding to the translation identifier key is found in the sub-target language package, it is prohibited to write the translation identifier key and the corresponding target language translation result into the sub-target language package; If the translation result corresponding to the translation identifier key is not found in the sub-target language package, the translation identifier key and the corresponding translation result for each language are written into the sub-language package corresponding to the current functional module in each language package.
8. A code processing device, characterized in that, include: The recognition module is used to identify basic language text and dynamic interpolation in the source code to be processed. The acquisition module is used to acquire the translation identifier key corresponding to the basic language text and the placeholder corresponding to the dynamic interpolation; The construction module is used to construct an internationalized function call expression based on the translation identifier key corresponding to the basic language text and the placeholder corresponding to the dynamic interpolation; The generation module is used to generate target code based on the source code to be processed and the internationalized function call expression.
9. An electronic device, characterized in that, Includes processor and memory, of which: Memory, used to store computer programs; A processor for executing a program stored in memory to implement the method described in any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the method described in any one of claims 1-7.