Web multilingual translation method and device based on AST analysis

By automatically identifying and translating the source code of web pages through AST analysis, and generating internationalized calling functions, this technology solves the problems of high error rate and difficulty in adapting to rapid changes caused by manual translation in existing technologies, and achieves efficient and accurate multilingual support.

CN120258007BActive Publication Date: 2025-12-09BEIJING QINGWANG TECH CORP
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202510217593.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-02-26
Publication Date
2025-12-09
Estimated Expiration
2045-02-26

AI Technical Summary

Technical Problem

Existing web multilingual translation methods rely on manually extracting and updating translation files, resulting in high error rates and difficulty in adapting to rapidly changing business needs, failing to achieve a complete automated process from code analysis to translation.

Method used

By using AST-based analysis, the system automatically identifies text in the web page source code that needs translation, generates internationalization call functions, and replaces them in the source code. This supports translation of multiple languages ​​and decouples the text from the business logic.

Benefits of technology

It enables automated multilingual translation of web pages, reducing manual operations, improving translation accuracy and efficiency, supporting flexible language switching, and enhancing system maintainability and scalability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120258007B_ABST
    Figure CN120258007B_ABST
Patent Text Reader

Abstract

The present specification relates to a web multilingual translation method and device based on AST analysis. After receiving operation information of a user for a function module of a page, the language type corresponding to the user is determined, and an internationalization calling function in the source code of the function module is called according to the language type to obtain a translation, a page of the function module is generated according to the translation and presented to the user. Wherein, after the source code of the function module is analyzed by abstract syntax tree, the original text of the script and its position information in the source code are determined, the original text is translated into various language types to obtain translations of various language types, an internationalization calling function including the translations of various language types is generated, and the script to be translated is replaced by the internationalization calling function in the source code according to the position information. The method of the embodiment of the present specification avoids large-scale manual search and replacement, and has strong maintainability and expansibility.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] Embodiments of the present specification relate to the technical field of artificial intelligence, and in particular to a Web multilingual translation method and device based on AST analysis. BACKGROUND

[0002] With the development of globalization, more and more Web applications need to support multiple languages to meet the needs of users in different regions. However, the current translation method usually relies on manual extraction of the text to be translated in the Web application, and translation is performed through a translation tool, and finally the Web content is generated according to the translation result and displayed to the user. This makes it necessary to manually extract and update the translation file every time new or modified text is added, which is prone to errors; and as the project size expands, the workload of manually extracting and translating text grows exponentially, making it difficult to adapt to rapidly changing business needs; finally, existing tools can only assist in translation and cannot achieve a complete automated process from code analysis to translation. SUMMARY

[0003] To solve the problems existing in the prior art, the embodiments of the present specification provide a Web multilingual translation method and device based on AST analysis, which determines the target language type corresponding to the user after receiving operation information of the user for a target function module of a target page, and calls an internationalization calling function in the source code of the target function module according to the target language type to obtain a target translation, generates a page of the target function module according to the target translation and presents it to the user. Wherein, after analyzing the abstract syntax tree of the source code of the target function module, the original text of the copy and its position information in the source code are determined, the original text is translated into translation in multiple language types, an internationalization calling function including translation in multiple language types is generated, and the text to be translated is replaced with the internationalization calling function in the source code according to the position information.

[0004] The specific technical solutions of the embodiments of the present specification are as follows:

[0005] On the one hand, the embodiments of the present specification provide a Web multilingual translation method based on AST analysis, which comprises:

[0006] After receiving operation information of the user for a target function module on a target page, determining the target language type corresponding to the user;

[0007] calling an internationalization calling function of a source code of the target function module according to the target language type, to obtain a target translation, wherein, after performing abstract syntax tree analysis on the source code of the target function module, the original text of the text case and position information of the text case in the source code are determined, the original text of the text case is translated into a translation of each language type, the internationalization calling function including the translation of each language type is generated, and the text case is replaced by the internationalization calling function in the source code according to the position information;

[0008] generating a page of the target function module according to the target translation and presenting the page to the user.

[0009] Further, before performing abstract syntax tree analysis on the source code of the target function module, the method further includes:

[0010] previously traversing the source code to extract a plurality of strings in the source code, position information of each string in the source code, syntax characteristics of each string, and a nesting relationship between the strings;

[0011] generating node information of each string corresponding node according to the content, position information and syntax characteristics of each string, respectively;

[0012] constructing the abstract syntax tree including each node according to the node information and the nesting relationship.

[0013] Further, the node information includes text information, node position and node type.

[0014] Generating node information of each string corresponding node according to the content, position information and syntax characteristics of each string, respectively, further includes:

[0015] for one of the strings, taking the content of the string as the text information of the string corresponding node;

[0016] taking the position information of the string as the node position of the string corresponding node;

[0017] determining the node type of the string corresponding node according to the syntax characteristics of the string.

[0018] Further, after performing abstract syntax tree analysis on the source code of the target function module, determining the original text of the text case and the position information of the text case in the source code further includes:

[0019] parsing the abstract syntax tree to obtain node information of each node;

[0020] For a node, determining whether the node is a node requiring translation according to the node information and / or node information of a parent node of the node;

[0021] If yes, it indicates that a string corresponding to the node is the script requiring translation, the text information of the node is used as a source text of the script, and the node position of the node is used as position information of the script.

[0022] Further, determining whether the node is a node requiring translation according to the node information and / or node information of a parent node of the node further includes:

[0023] Executing a predetermined judgment rule according to the node information and / or node information of the parent node of the node to obtain a judgment result of whether the node is a node requiring translation.

[0024] Further, before executing the predetermined judgment rule according to the node information and / or node information of the parent node of the node to obtain the judgment result of whether the node is a node requiring translation, the method further includes:

[0025] Judging whether a node type of the node is a predetermined type, the predetermined type including any one of a literal, a template string and text content;

[0026] If yes, the predetermined judgment rule is executed;

[0027] If no, the node does not require translation.

[0028] Further, executing the predetermined judgment rule according to the node information and / or node information of the parent node of the node to obtain the judgment result of whether the node is a node requiring translation further includes:

[0029] If any one of the following judgment rules is satisfied, the node does not require translation:

[0030] (1) judging whether the text information of the node is a URL address;

[0031] (2) judging whether a node type of the parent node of the node is a pure type;

[0032] (3) judging whether the text information of the node is a color expression;

[0033] (4) judging whether a node type of the parent node of the node is a Switch-Case type;

[0034] (5) judging whether a node type of the parent node of the node is a binary operation expression type and an operator is not a plus sign;

[0035] (6) judging whether the text information of the node is empty;

[0036] (7) judging whether the node type of the parent node of the node is a call expression type and the function name is a specified function name;

[0037] (8) judging whether the node type of the parent node of the node is an attribute type in a JSX element and the attribute name is a specified attribute name and the component name is a specified component name;

[0038] (9) judging whether the text information of the node is a pure Arabic numeral;

[0039] (10) judging whether the text information of the node is a single character and the single character is a specified single character.

[0040] Further, if it is determined according to the node information and / or the node information of the parent node of the node that the node is a node that needs to be translated, the method further comprises:

[0041] judging whether the node type of the node is the template string;

[0042] if yes, traversing all parent nodes of the node upwards until the uppermost parent node whose node type is a template literal is found, taking the text information of the parent node as the original text of the script and taking the node position of the parent node as the position information of the script.

[0043] Further, the multilingual translation of the original text of the script to obtain the translated text of each language type further comprises:

[0044] analyzing the meaning of the string that does not need to be translated in the text information of the parent node, and generating a placeholder corresponding to the meaning, and replacing the string that does not need to be translated with the placeholder in the text information of the parent node;

[0045] according to the meaning of the string that needs to be translated in the text information of the parent node and the meaning of the context thereof, translating the string that needs to be translated into each language type, and splicing the translated text with the placeholder to obtain the translated text of each language type of the text information of the parent node.

[0046] Further, generating the internationalization calling function comprising the translated text of each language type further comprises:

[0047] generating a unique identifier of the script;

[0048] constructing a corresponding relationship among the unique identifier, each language type and the translated text of each language type to obtain the internationalization calling function.

[0049] Further, replacing the script with the internationalization calling function in the source code according to the position information further comprises:

[0050] replace the text in the source code with the internationalization calling function according to the position information.

[0051] Further, when the source code includes multiple texts requiring replacement of the internationalization calling function, the method further includes:

[0052] replacing the texts in the source code in turn according to the order from back to front with the corresponding internationalization calling functions.

[0053] Further, when the source code includes multiple texts requiring replacement of the internationalization calling function, before translating the original texts of the texts into multiple language types, the method further includes:

[0054] de-duplicating the multiple original texts.

[0055] In another aspect, the embodiments of the present specification also provide a Web multilingual translation device based on AST analysis, the device includes:

[0056] a target language type determination unit configured to determine a target language type of a user after receiving operation information of the user on a target function module on a target page;

[0057] a calling unit configured to call an internationalization calling function of a text of source code of the target function module according to the target language type to obtain a target translation text, wherein the source code of the target function module is analyzed by abstract syntax tree in advance, an original text of the text and position information of the text in the source code are determined, the original text of the text is translated into multiple language types to obtain translation texts of the multiple language types, the internationalization calling function including the translation texts of the multiple language types is generated, and the text is replaced with the internationalization calling function in the source code according to the position information.

[0058] a presentation unit configured to generate a page of the target function module according to the target translation text and present the page to the user.

[0059] In another aspect, the embodiments of the present specification also provide a computer device, including a memory, a processor, and a computer program stored in the memory, and the processor implements the above method when executing the computer program.

[0060] In another aspect, the embodiments of the present specification also provide a computer readable storage medium, the computer readable storage medium stores a computer program, and the computer program is executed by a processor to implement the above method.

[0061] With the embodiments of the present specification, the source code of a Web page can be improved and optimized in advance, the text to be translated in the source code of each functional module of the Web page is determined through abstract syntax tree analysis, and the text to be translated is automatically translated, thereby avoiding large-scale manual search and replacement. In addition, the method of the embodiments of the present specification supports translation of language types, the text to be translated in the source code can be translated into translated texts of multiple language types at one time, and an internationalization calling function including the translated texts of each language type is generated. The text to be translated in the source code is replaced with the generated internationalization calling function, thereby decoupling the text in the source code from the business logic, uniformly managing the text through the internationalization calling function, making subsequent updates simple and controllable, and increasing or replacing a translation engine without affecting the overall process, thereby having strong maintainability and expansibility. BRIEF DESCRIPTION OF DRAWINGS

[0062] In order to more clearly illustrate the technical solutions in the embodiments of the present specification or the prior art, the drawings needed to be used in the embodiments or the prior art description will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments of the present specification, and other drawings can be obtained by those skilled in the art without creative labor.

[0063] Figure 1 Fig. 1 shows a flowchart of a Web multilingual translation method based on AST analysis in an embodiment of the present specification;

[0064] Figure 2 Fig. 2 shows a flowchart of generating an internationalization calling function including translated texts of each language type in an embodiment of the present specification;

[0065] Figure 3 Fig. 3 shows a flowchart of constructing an abstract syntax tree of source code in an embodiment of the present specification;

[0066] Figure 4 Fig. 4 shows a flowchart of determining the original text of the text and the position information of the text in the source code after the abstract syntax tree analysis of the source code of the target functional module in an embodiment of the present specification;

[0067] Figure 5 Fig. 5 shows a flowchart of processing text carrying variables in an embodiment of the present specification;

[0068] Figure 6 Fig. 6 shows a flowchart of translating the original text of the text into translated texts of each language type in an embodiment of the present specification;

[0069] Figure 7Fig. 1 shows a structural schematic diagram of a web multilingual translation device based on AST analysis according to an embodiment of the present specification.

[0070] Figure 8 Fig. 2 shows a structural schematic diagram of a computer device according to an embodiment of the present specification.

[0071]

Explanation of reference signs

[0072] 701, target language type determination unit;

[0073] 702, calling unit;

[0074] 703, presentation unit;

[0075] 802, computer device;

[0076] 804, processing device;

[0077] 806, storage resource;

[0078] 808, driving mechanism;

[0079] 810, input / output module;

[0080] 812, input device;

[0081] 814, output device;

[0082] 816, presentation device;

[0083] 818, graphical user interface;

[0084] 820, network interface;

[0085] 822, communication link;

[0086] 824, communication bus. DETAILED DESCRIPTION

[0087] The technical solutions in the embodiments of the present specification will be described clearly and completely below in combination with the drawings in the embodiments of the present specification. Obviously, the described embodiments are only part of the embodiments of the present specification, rather than all the embodiments. Based on the embodiments in the embodiments of the present specification, all other embodiments obtained by those of ordinary skill in the art without creative labor fall within the scope of protection of the embodiments of the present specification.

[0088] It should be noted that the terms "first", "second" and the like in the description and claims of the embodiments of the present specification and the above-described drawings are used to distinguish similar objects, and do not necessarily have to be used to describe a specific order or sequence. It should be understood that the data thus used can be interchanged under appropriate circumstances, so that the embodiments of the present specification described herein can be implemented in an order other than that illustrated or described herein. In addition, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusion, for example, a process, method, device, product or apparatus that includes a list of steps or units does not have to be limited to those steps or units clearly listed, but can include other steps or units not clearly listed or inherent to such processes, methods, products or apparatus.

[0089] It should be noted that the acquisition, storage, use, processing and the like of data in the technical solutions of the embodiments of the present specification comply with the relevant provisions of national laws and regulations.

[0090] It should be noted that in the embodiments of the present specification, some existing industry solutions of software, components, models and the like may be mentioned, which should be considered as exemplary, and the purpose is only to illustrate the feasibility of the technical solution implementation of the present application, but does not mean that the applicant has or will necessarily use the solution.

[0091] In view of the problems in the prior art, the embodiments of the present specification provide a Web multilingual translation method based on AST analysis, Figure 1 As shown in the flowchart of the Web multilingual translation method based on AST analysis in the embodiments of the present specification. The translation process of the page Chinese case is described in the figure. The order of steps listed in the embodiments is only one of the many step execution orders, and does not represent the only execution order. In actual system or device product execution, it can be executed in sequence or in parallel according to the method order shown in the embodiments or the drawings.

[0092] Specifically, as shown in the flowchart of the Web multilingual translation method based on AST analysis in the embodiments of the present specification. The translation process of the page Chinese case is described in the figure. The order of steps listed in the embodiments is only one of the many step execution orders, and does not represent the only execution order. In actual system or device product execution, it can be executed in sequence or in parallel according to the method order shown in the embodiments or the drawings. Figure 1 As shown in the flowchart of the Web multilingual translation method based on AST analysis in the embodiments of the present specification. The translation process of the page Chinese case is described in the figure. The order of steps listed in the embodiments is only one of the many step execution orders, and does not represent the only execution order. In actual system or device product execution, it can be executed in sequence or in parallel according to the method order shown in the embodiments or the drawings.

[0093] Step 101: After receiving the operation information of the user for the target function module on the target page, determining the target language type corresponding to the user;

[0094] Step 102: calling an internationalization calling function of a source code of the target function module according to the target language type to obtain a target translation, wherein, after abstract syntax tree analysis is performed on the source code of the target function module, the original text of the text case and position information of the text case in the source code are determined, the original text of the text case is translated into a translation of each language type, the internationalization calling function including the translation of each language type is generated, and the text case is replaced with the internationalization calling function in the source code according to the position information.

[0095] Step 103: generating a page of the target function module according to the target translation and presenting the page to the user.

[0096] By using the embodiments of the present specification, the source code of a Web page can be improved and optimized in advance, the text case that needs to be translated in the source code of each function module of the Web page is determined through abstract syntax tree analysis, and the text case that needs to be translated is automatically translated, thereby avoiding large-scale manual searching and replacing. In addition, the method of the embodiments of the present specification supports translation of language types, the text case that needs to be translated in the source code can be translated into a translation of multiple language types at one time, the internationalization calling function including the translation of each language type is generated, the text case that needs to be translated in the source code is replaced with the generated internationalization calling function, thereby decoupling the text case and the business logic in the source code, uniformly managing the text case through the internationalization calling function, making subsequent updates simple and controllable, and meanwhile, adding or replacing a translation engine does not affect the overall process, and the method has strong maintainability and expansibility.

[0097] In the embodiments of the present specification, the operation of the user on the target function module of the target page can include operations such as user login to the target page and user change of the language type of the target page. The target page can record the identity information of the user and the target language type selected by the user. If the target page records the identity information of the user, the language type matched with the identity information of the user can be determined as the target language type. Then the page under the target language type needs to be presented to the user. Therefore, the embodiments of the present specification translate the text case in the target page into a translation under the target language type, and generate a page according to the translation.

[0098] In the embodiments of the present specification, abstract syntax tree analysis is performed on the source code of the target page, the text case that needs to be translated in the source code and the position of the text case in the source code are identified, and then the text case that needs to be translated is translated into a translation of each language type. Then an internationalization calling function including the translation of each language type is generated. Optionally, as shown in Figure 2 the generation of the internationalization calling function including the translation of each language type further includes:

[0099] Step 201: generating a unique identifier of the text;

[0100] Step 202: constructing a correspondence among the unique identifier, each language type, and the translation of each language type, to obtain the internationalization calling function.

[0101] In the embodiment of the present specification, the internationalization calling function can include two input parameters, the first input parameter is the unique identifier, and the second input parameter is the language type, and the output result of the internationalization calling function is the translation corresponding to the input unique identifier and language type. Wherein, the unique identifier is the unique identifier of the text in the source code. When it is determined by the abstract syntax tree analysis method that the text in the source code needs to be translated, the original text of the text is automatically translated into a translation of multiple language types, and the internationalization calling function is generated and the text in the source code is replaced by the internationalization calling function.

[0102] The present specification further comprises:

[0103] The unique identifier is used as a fixed parameter of the internationalization calling function of the text, and the language type is used as a dynamic parameter of the internationalization calling function of the text, and the internationalization calling function is used to replace the text in the source code according to the position information.

[0104] Therefore, when the user operates the target function module of the target page, the source code of the target function module will be executed, and when the source code is executed to the position of the internationalization calling function, the internationalization calling function will be called, and the internationalization calling function returns the translation of the text in the source code at this position.

[0105] It can be understood that the function name of all internationalization calling functions in the source code of the embodiment of the present specification can be the same, and the corresponding translation is returned according to the input unique identifier and language type. After the text in the source code that needs to be translated is translated in the embodiment of the present specification, the text is replaced by the internationalization calling function at the position of the text in the source code, so the first input parameter (unique identifier) of the internationalization calling function at the position in the source code is fixed, and the second input parameter (language type) is the target language type (that is, the language type that the target page needs to present to the user) obtained by analysis.

[0106] It should be noted that in addition to the unique identifier and the language type, the internationalization calling function can have more input parameters, and the staff can set more input parameters of the internationalization calling function according to the code structure and the actual scene, and the embodiment of the present specification is not limited.

[0107] In other scenarios of the embodiments of this specification, the text that needs to be translated in the source code can be hard-coded text. For example, when developers write code, they hardcode the text that needs to be displayed to users in a single language. This is hard-coded text. In this case, it is difficult for the web page to support language switching because the source code can only generate and present the page to the user based on the text in that single language, causing great inconvenience to the user. In this situation, the method of the embodiments of this specification can be applied to modify the source code. First, the source code is analyzed using an abstract syntax tree to determine the hard-coded text. Then, the hard-coded text is translated into multiple languages. Next, an internationalization call function for the hard-coded text is generated. Finally, the hard-coded text in the source code is replaced with the internationalization call function. Afterward, when the source code is executed, it will call the internationalization call function, which returns the translation in the corresponding language type, thereby enabling flexible language switching on the web page. This provides a better experience for users, makes the source code more standardized for developers, and allows for direct calling of the internationalization call function for the same hard-coded text during subsequent coding processes. The first input parameter is the unique identifier of the hard-coded text, improving coding efficiency.

[0108] Identifying the text (hard-coded text) that needs translation in the source code is another key focus of this specification's embodiments. Specifically, this specification's embodiments utilize abstract syntax tree analysis to identify the text that needs translation in the source code.

[0109] First, this specification describes the construction of an abstract syntax tree for the source code in the embodiments. For example... Figure 3 As shown, the steps to construct the abstract syntax tree of the source code include:

[0110] Step 301: Traverse the source code in advance to extract multiple strings in the source code, the position information of each string in the source code, the syntactic features of each string, and the nesting relationship between each string;

[0111] Step 302: Generate node information for each string based on its content, position information, and syntactic features;

[0112] Step 303: Construct the abstract syntax tree including each node based on the node information and nesting relationships.

[0113] In the embodiments described in this specification, the source code is composed of multiple strings, for example:

[0114] const text:string = 'Hello'

[0115] const url="https: / / some.where"

[0116] The 'Hello' can be a string, the text:string='Hello' can be a string, and the const text:string='Hello' can also be a string. As can be seen, there is a nesting relationship between multiple strings. In addition, each string in the source code has its own syntax characteristics, which can include keywords, identifiers, operators, separators, comments, data types, functions and methods, classes and objects, literals, expressions and statements, template strings, text content, and the like. Moreover, the position of each string in the source code can also be parsed, such as the line number, the start character number and the character length in the line, or the start character number and the character length in the source code.

[0117] Therefore, the embodiment of the present specification pre-traverses the source code, extracts multiple strings in the source code, position information of each string in the source code, syntax characteristics of each string, and a nesting relationship between each string, then generates node information of each string corresponding node according to the content, position information and syntax characteristics of each string, and finally constructs the abstract syntax tree including each node according to the node information and the nesting relationship. In the abstract syntax tree, each node corresponds to a string in the source code, and the parent node of the node is a string that has a nesting relationship with the string corresponding to the node. For example, 'Hello' is a node A in the abstract syntax tree, the parent node B of the node A corresponds to the string text:string='Hello', and the parent node C of the node B corresponds to the string const text:string='Hello'. The node information of each node includes text information, node position and node type, wherein the text information is the content of the string corresponding to the node, the node position is the position information of the string corresponding to the node in the source code, and the node type is determined according to the syntax characteristics of the string corresponding to the node. Exemplarily, the node type includes but is not limited to literal (Literal), template string (TemplateElement), text content (JSXText), binary operation expression (BinaryExpression), call expression (CallExpression), attribute type in JSX element (JSXAttribute), and template literal (TemplateLiteral).

[0118] It should be noted that the Chinese name of the above-mentioned node type is only an example, and the Chinese name considered by different technicians in the art can be slightly different, and the embodiment of the present specification does not limit it.

[0119] As Figure 4As shown, the method further includes:

[0120] Step 401: parsing the abstract syntax tree to obtain node information of each node;

[0121] Step 402: for a node, determining whether the node is a node that needs to be translated according to the node information and / or node information of a parent node of the node;

[0122] Step 403: if yes, it indicates that a string corresponding to the node is the text that needs to be translated, and the text information of the node is used as the original text of the text, and the node position of the node is used as the position information of the text.

[0123] According to an embodiment of the present specification, in order to reduce the amount of calculation, the node can also be preliminarily screened according to the node type. Specifically, before the step of determining whether the node is a node that needs to be translated according to the node information and / or node information of a parent node of the node, the method further includes:

[0124] determining whether the node type of the node is a predetermined type, the predetermined type including any one of Literal, TemplateElement and JSXText;

[0125] if yes, the predetermined judgment rule is executed;

[0126] if no, the node does not need to be translated.

[0127] In the embodiment of the present specification, it can be determined according to experience which types of node text information may contain hard-coded content and which types of node text information are unlikely to contain hard-coded content. The node type that may contain hard-coded content is used as the predetermined type, so that the node is preliminarily screened by using the predetermined type, and the amount of calculation of subsequent rule judgment is reduced.

[0128] Preferably, in the embodiment of the present specification, only the text information of the nodes of the three types of Literal, TemplateElement and JSXText may contain hard-coded content. Therefore, if the node type is any one of the three types, the node can execute the predetermined judgment rule to further determine whether it needs to be translated, otherwise the node does not need to be translated.

[0129] According to an embodiment of the present specification, the step of determining whether the node is a node that needs to be translated according to the node information and / or node information of a parent node of the node further includes:

[0130] If any of the following judgment rules is met, the node does not need to be translated:

[0131] (1) Determine whether the text information of the node is a URL address; specifically, the text information of the node can be determined to be a URL address by a regular expression method, and if it is a URL address, the text information of the node does not need to be translated.

[0132] (2) Determine whether the node type of the parent node of the node is a pure type; specifically, the pure type refers to a node type that does not contain side effects or additional behaviors, and these nodes only represent syntax structures or data, without involving calculations, side effects or runtime behaviors, such as type declaration (TSTypeAliasDeclaration), syntax structure for defining interface (TSInterfaceDeclaration), literal type declaration (TSLiteralType), type annotation (TSTypeAnnotation) and the like. If the parent node type of the node is a pure type, the node does not need to be translated.

[0133] (3) Determine whether the text information of the node is a color expression; specifically, the text information can be determined to be a color expression by a regular expression method, and if it is, the node does not need to be translated.

[0134] (4) Determine whether the node type of the parent node of the node is a Switch-Case type; specifically, if the parent node type of the node is a Switch-Case type, the node does not need to be translated.

[0135] (5) Determine whether the node type of the parent node of the node is BinaryExpression and the operator is not plus; specifically, if the parent node type of the node is BinaryExpression and the operator is plus, the node does not need to be translated.

[0136] (6) Determine whether the text information of the node is empty; specifically, it can be determined by whether the original text contained by the node is a null string after trimming.

[0137] (7) judging whether the node type of the parent node of the node is CallExpression and the function name is a specified function name; specifically, if the node type of the parent node of the node is CallExpression and the function name is the specified function name, the node does not need to be translated. It should be noted that the specified function name can be set by the staff according to the actual situation, and the embodiments of the present specification are not limited. For example, in the source code after being replaced by the internationalization calling function, the first input parameter of the internationalization calling function is a unique identifier, which is also a string. When the abstract syntax tree is analyzed, the unique identifier is a node, and the type of this node is TemplateElement. It is obviously unreasonable to translate the unique identifier, so the embodiments of the present specification judge whether the node type of the parent node of the unique identifier is CallExpression, that is, whether the node type of the string corresponding to the internationalization calling function is CallExpression. If yes, it is further necessary to judge whether the function name is the function name of the internationalization calling function. If yes, it indicates that it is the unique identifier input by the internationalization calling function and does not need to be translated. Of course, the function names of some other functions can also be specified as function names, such as the function names of the functions for monitoring subscription / publishing events.

[0138] (8) judging whether the node type of the parent node of the node is JSXAttribute and the attribute name is a specified attribute name and the component name is a specified component name; specifically, if the node type of the parent node of the node is JSXAttribute and the attribute name is the specified attribute name and the component name is the specified component name, the node does not need to be translated. For example, the content in the class and style fields of html does not need to be translated, which can be judged by using the node and recursively finding the type of the parent node whether it is the attribute of the dom / component, and then judging whether it needs to be translated according to the two dimensions of the attribute name and the dom / component name.

[0139] (9) judging whether the text information of the node is pure Arabic numerals; specifically, if the text information of the node is pure Arabic numerals, the node does not need to be translated.

[0140] (10) judging whether the text information of the node is a single character and the single character is a specified single character. Specifically, if the text information of the node is a single character and the single character is a specified single character, the node does not need to be translated, for example, mathematical operator symbols, percent signs, and single characters such as curved lines do not need to be translated.

[0141] In some other embodiments of the present specification, the language type of the text information can also be determined by the node containing the text information in combination with regular expressions. If the language type is a specified language type, translation is not required. The specified language type can be set according to actual conditions, and whether this determination rule is enabled or disabled can be determined according to actual project needs.

[0142] It should be noted that the above determination rules are independent and do not interfere with each other, and any of the above rules can be enabled or disabled. In addition, developers are allowed to customize rules, such as skipping when encountering an enumerated member or skipping when encountering an IP, and the embodiments of the present specification do not limit this.

[0143] After determining the nodes that need to be translated, an AI translation tool can be called to translate the text information of these nodes to obtain a translation.

[0144] When replacing, for a string that does not carry a variable, the string can be directly replaced by an internationalization calling function in the source code, that is, the string itself is the original text to be translated, and therefore the string can be replaced according to the position information of the string.

[0145] However, for text carrying a variable, additional processing is required due to cost considerations and the position information required for final replacement.

[0146] Specifically, as shown in Figure 5 If it is determined according to the node information and / or the node information of the parent node of the node that the node is a node that needs to be translated, the method further includes:

[0147] Step 501: determining whether the node type of the node is the template string;

[0148] In the embodiments of the present specification, it is considered that the text information of the node type of the TemplateElement node can carry a variable, and the text information of the node type of the Literal and JSXText node does not carry a variable and can be directly replaced.

[0149] Step 502: If yes, traverse all parent nodes of the node upwards until the uppermost parent node of the node type of the TemplateLiteral is found, and the text information of the parent node is used as the original text of the text case, and the node position of the parent node is used as the position information of the text case.

[0150] In the embodiments of this specification, the TemplateElement must be under the TemplateLiteral, and the template string can continue to nest template strings. Therefore, the strategy is to merge the TemplateElement into the topmost TemplateLiteral.

[0151] For example, the source code is const text: string = 'Hello, ${username}'. 'Hello' is a string, corresponding to a node of the TemplateElement type. The text information of this node is 'Hello'. 'Hello, ${username}' is a string, corresponding to a node of the TemplateLiteral type. The text information of this node is 'Hello, ${username}', and this node is the parent node of the node corresponding to 'Hello'. ${username} is a variable. Therefore, 'Hello, ${username}' is the text information carrying the variable. During translation, 'Hello, ${username}' is translated as a whole. After generating the internationalization call function, 'Hello, ${username}' in the source code is replaced with the internationalization call function as a whole.

[0152] Furthermore, as Figure 6 shown, the translation of the original text of the copywriting into multiple language types and obtaining the translations of each language type further includes:

[0153] Step 601: Analyze the meaning of the strings that do not need to be translated in the text information of the parent node, and generate placeholders corresponding to the meaning. Replace the strings that do not need to be translated with the placeholders in the text information of the parent node;

[0154] Step 602: Translate the strings that need to be translated in the text information of the parent node into each language type according to the meaning of the strings that need to be translated and the meaning of their context, and splice them with the placeholders to obtain the translations of each language type of the text information of the parent node.

[0155] It can be understood that through Figure 6 the method shown, the translation is achieved by combining the context meaning, improving the accuracy of translation.

[0156] Meanwhile, if the variable name is included in the string not requiring translation, a mapping relationship between the placeholder and the string not requiring translation is recorded, and the generation of the internationalization calling function including the translation in each language type includes: taking the placeholder as an input parameter of the internationalization calling function. When the text is replaced by the internationalization calling function in the source code according to the position information, the method further includes: adding the mapping relationship in the source code, so that before the internationalization calling function is called, the value of the placeholder is calculated according to the mapping relationship and the variable value of the variable name, and when the internationalization function is called, the value of the placeholder is input into the internationalization calling function, so that the value of the placeholder is used to replace the corresponding placeholder in the translation, to obtain the target translation, and the page of the target function module is generated according to the target translation and presented to the user. Exemplarily, in javascript / typescript, the string template usually uses {} to represent variables, and the text information of the parent node is'some resource growth of { (newCount-oldCount) / oldCount*100} %', wherein the string in {} is not required to be translated, so the embodiment of the specification analyzes the meaning of the string '(newCount-oldCount) / oldCount*100', and obtains that the meaning is percentage, and therefore the placeholder of the string is 'percentage'. The original string is replaced by the placeholder 'percentage' to obtain'some resource growth of {percentage} %', according to the meaning of the string'some resource growth of'requiring translation and the meaning of the context percentage, the content 'percentage' wrapped by {} is not translated, and the content 'percentage' in the curly braces is output as it is. The reason for rewriting to {} instead of keeping the original ${} is that the internal syntax implementation of most internationalization calling functions is inconsistent with the original language. For example, in javascript / typescript, the string template usually uses {} to represent variables, and the internal syntax implementation of the internationalization calling function uses {} to represent variables.

[0157] wherein newCount and oldCount are variable names, and therefore when the internationalization calling function is replaced in the source code, the mapping relationship needs to be added in the source code, for example, percentage = (newCount-oldCount) / oldCount*100.

[0158] According to one embodiment of the present specification, in the replacement stage, because the character length of the replaced internationalization calling function may be different from the length of the original text in the source code, in order to avoid errors in the replacement position, the embodiments of the present specification sequentially replace each text in the source code with the corresponding internationalization calling function from back to front, thereby avoiding affecting the position information of the previous original text.

[0159] In addition, in order to save translation costs, when the source code includes multiple texts that need to be replaced by internationalization calling functions, before the original texts of the texts are translated into multiple language types, the method further includes: removing duplicates from the multiple original texts, thereby avoiding multiple identical original texts being repeatedly translated multiple times, which can reduce the cost of translation to a certain extent.

[0160] It should be noted that before AI automatic translation, the original text may also need to be converted into a format that AI can recognize, so that the AI accurately outputs the translated text. The specific format needs to be determined according to the AI automatic translation tool, and the embodiments of the present specification will not be described in detail.

[0161] In some other embodiments of the present specification, real-time translation can also be performed. Specifically, after receiving operation information of a user on a target function module on a target page, a target language type corresponding to the user is determined; then the source code of the target function module is traversed, and multiple strings in the source code, position information of each string in the source code, syntax features of each string, and nesting relationships between each string are extracted; node information of each node corresponding to each string is generated according to the content, position information and syntax features of each string, respectively; and the abstract syntax tree including each node is constructed according to the node information and the nesting relationships. Then the abstract syntax tree is parsed to obtain node information of each node; for one node, whether the node is a node that needs to be translated is determined according to the node information and / or the node information of the parent node of the node; if yes, it indicates that the string corresponding to the node is the text that needs to be translated, and the text information of the node is used as the original text of the text, and the node position of the node is used as the position information of the text. Then the original text is translated to obtain a translated text, and the original text in the source code is replaced with the translated text according to the position information, and finally a page of the target function module is generated according to the translated text and presented to the user.

[0162] Among them, the process of constructing the abstract syntax tree and the analysis process of the abstract syntax tree are consistent with the process described above, the difference is that in the process of real-time translation, there is no need to construct the internationalization calling function, but directly replace the original text in the source code with the translated text according to the position information of the original text, and the repeated parts will not be described in detail.

[0163] Based on the same inventive concept, the embodiment of the present specification also provides a web multilingual translation device based on AST analysis. As shown in Figure 7 The device comprises:

[0164] A target language type determination unit 701 is configured to determine a target language type of a user after receiving operation information of the user on a target function module on a target page.

[0165] A calling unit 702 is configured to call an internationalization calling function of a text case of source code of the target function module according to the target language type, to obtain a target translation, wherein the original text of the text case and position information of the text case in the source code are determined after the source code of the target function module is analyzed by abstract syntax tree, the original text of the text case is translated into translation of each language type, the internationalization calling function including the translation of each language type is generated, and the text case is replaced by the internationalization calling function in the source code according to the position information.

[0166] A presentation unit 703 is configured to generate a page of the target function module according to the target translation and present the page to the user.

[0167] Since the principle of the above device for solving the problem is similar to the above method, the implementation of the above system can refer to the implementation of the above method, and the repeated parts will not be described.

[0168] As shown in Figure 8 The structure schematic diagram of the computer device of the embodiment of the present specification is shown, the permeability calculation unit or the diversion capacity calculation unit in the present application can be the computer device in the embodiment, and the method of the present application is executed. The computer device 802 can include one or more processing devices 804, such as one or more central processing units (CPUs), each of which can implement one or more hardware threads.

[0169] The computer device 802 can also include any storage resource 806 for storing any kind of information such as code, settings, data, etc.

[0170] Non-limitingly, for example, the storage resource 806 can include any one or a combination of the following: any type of RAM, any type of ROM, a flash memory device, a hard disk, an optical disk, etc.

[0171] More generally, any storage resource can store information using any technology.

[0172] Further, any storage resource can provide volatile or non-volatile retention of information.

[0173] Further, any storage resource can represent a fixed or removable component of the computer device 802.

[0174] In one scenario, the computer device 802 can perform any of the operations of the associated instructions when the processing device 804 executes the associated instructions stored in any storage resource or combination of storage resources. The computer device 802 also includes one or more driver mechanisms 808 for interacting with any storage resource, such as a hard disk drive mechanism, an optical disk drive mechanism, etc.

[0175] The computer device 802 can also include an input / output module 810 (I / O) for receiving various inputs (via input device 812) and for providing various outputs (via output device 814). One particular output mechanism can include a presentation device 816 and associated graphical user interface (GUI) 818. In other embodiments, the input / output module 810 (I / O), input device 812, and output device 814 can also not be included, merely serving as a computer device in a network. The computer device 802 can also include one or more network interfaces 820 for exchanging data with other devices via one or more communication links 822. One or more communication buses 824 couple the above-described components together.

[0176] The communication links 822 can be implemented in any manner, such as through a local area network, a wide area network (e.g., the Internet), a point-to-point connection, etc., or any combination thereof. The communication links 822 can include any combination of hardwired links, wireless links, routers, gateway functionality, name servers, etc., governed by any protocol or combination of protocols.

[0177] The embodiments of the present specification also provide a computer readable storage medium, the computer readable storage medium stores a computer program, the computer program is executed by a processor to implement the above method.

[0178] The embodiments of the present specification also provide a computer readable instruction, wherein when the processor executes the instruction, the program in the processor makes the processor execute the above method.

[0179] It should be understood that the size of the sequence number of each process described above does not mean the order of execution in various embodiments of the embodiments of the present specification, and the execution order of each process should be determined according to its function and inherent logic, and should not constitute any limitation on the implementation process of the embodiments of the present specification.

[0180] It should also be understood that, in the embodiments of the present specification, the term "and / or" merely describes an association relationship of associated objects, and indicates that there can be three relationships. For example, A and / or B can represent three cases of A existing alone, A and B existing together, and B existing alone. In addition, the character " / " in the embodiments of the present specification generally represents an "or" relationship between the front and rear associated objects.

[0181] Those skilled in the art can realize that the units and algorithm steps of each example described in combination with the embodiments disclosed in the present specification can be realized in electronic hardware, computer software or a combination of both. In order to clearly illustrate the interchangeability of hardware and software, the components and steps of each example have been described in a general manner in the foregoing description. Whether the functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. A person skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of the embodiments of the present specification.

[0182] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the above-described system, device and unit can refer to the corresponding processes in the foregoing method embodiments, which will not be described here.

[0183] In several embodiments provided by the embodiments of the present specification, it should be understood that the disclosed system, device and method can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division, and actual implementation can have another division manner. For example, a plurality of units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the coupling or direct coupling or communication connection between the units shown or discussed can be indirect coupling or communication connection through some interfaces, devices or units, and can also be electrical, mechanical or other forms of connection.

[0184] The units described as separate components can or can not be physically separated, and the components shown as units can or can not be physical units, i.e. they can be located in one place, or can be distributed on a plurality of network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the embodiments of the present specification.

[0185] In addition, each functional unit in each embodiment of the present specification can be integrated into a processing unit, or each unit can exist physically, or two or more units can be integrated into one unit. The integrated unit can be realized in the form of hardware or software functional unit.

[0186] The integrated unit, if implemented in the form of a software function unit and sold or used as an independent product, can be stored in a computer readable storage medium. Based on such understanding, the technical solutions of the embodiments of the present specification or the part of the prior art that contributes essentially, or the whole or part of the technical solutions can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes a plurality of instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present specification. The aforementioned storage medium includes: a U disk, a mobile hard disk, a read-only memory (ROM, Read-Only Memory), a random access memory (RAM, Random Access Memory), a magnetic disk or an optical disk, and various media that can store program codes.

[0187] The principles and implementation manners of the embodiments of the present specification are described in the embodiments of the present specification. The above embodiment description is only used to help understand the method and its core idea of the embodiments of the present specification; meanwhile, for those skilled in the art, according to the idea of the embodiments of the present specification, the specific implementation manners and application ranges will be changed; in view of the above, the content of the present specification should not be understood as a limitation of the embodiments of the present specification.

Claims

1. A method for web multilingual translation based on AST analysis, characterized in that, The method comprises: after receiving operation information of a user on a target function module on a target page, determining a target language type corresponding to the user; calling an internationalization calling function of a source code of the target function module according to the target language type to obtain a target translation, wherein, after performing abstract syntax tree analysis on the source code of the target function module, the original text of the text case and the position information of the text case in the source code are determined, the original text of the text case is translated into a translation in each language type, the internationalization calling function including the translation in each language type is generated, and the text case is replaced with the internationalization calling function in the source code according to the position information; generating a page of the target function module according to the target translation and presenting the page to the user; before performing abstract syntax tree analysis on the source code of the target function module, the method further comprises: previously traversing the source code to extract a plurality of strings in the source code, position information of each string in the source code, syntax characteristics of each string, and a nesting relationship between the strings; generating node information of each string corresponding node according to the content, position information and syntax characteristics of each string, respectively; constructing the abstract syntax tree including each node according to the node information and the nesting relationship.

2. The method of claim 1, wherein, The node information includes text information, node position and node type. Generating node information of each string corresponding node according to the content, position information and syntax characteristics of each string, respectively, further comprises: for one string, taking the content of the string as the text information of the string corresponding node; taking the position information of the string as the node position of the string corresponding node; determining the node type of the string corresponding node according to the syntax characteristics of the string.

3. The method of claim 2, wherein, After previously performing abstract syntax tree analysis on the source code of the target function module, determining the original text of the text case and the position information of the text case in the source code further comprises: parsing the abstract syntax tree to obtain node information of each node; for one node, determining whether the node is a node that needs to be translated according to the node information and / or the node information of the parent node of the node; if yes, it means that the string corresponding to the node is the text case that needs to be translated, and the text information of the node is taken as the original text of the text case, and the node position of the node is taken as the position information of the text case.

4. The method of claim 3, wherein, Determining whether the node is a node that needs to be translated according to the node information and / or the node information of the parent node of the node further comprises: executing a predetermined judgment rule according to the node information and / or the node information of the parent node of the node to obtain a judgment result of whether the node is a node that needs to be translated.

5. The method of claim 4, wherein, Before executing a predetermined judgment rule according to the node information and / or the node information of the parent node of the node to obtain a judgment result of whether the node is a node that needs to be translated, the method further comprises: judging whether the node type of the node is a predetermined type, the predetermined type including any one of literal, template string and text content; If yes, the predetermined judging rule is executed; If no, the node does not need to be translated.

6. The method of claim 4, wherein, The predetermined judging rule is executed according to the node information and / or the node information of the parent node of the node, and a judgment result of whether the node is a node needing to be translated is obtained, and the judgment result further comprises: If any one of the following judgment rules is met, the node does not need to be translated: (1) judging whether the text information of the node is a URL address; (2) judging whether the node type of the parent node of the node is a pure type; (3) judging whether the text information of the node is a color expression; (4) judging whether the node type of the parent node of the node is a Switch-Case type; (5) judging whether the node type of the parent node of the node is a binary operation expression type and the operator is not a plus sign; (6) judging whether the text information of the node is empty; (7) judging whether the node type of the parent node of the node is a call expression type and the function name is a specified function name; (8) judging whether the node type of the parent node of the node is an attribute type in a JSX element and the attribute name is a specified attribute name and the component name is a specified component name; (9) judging whether the text information of the node is pure Arabic numerals; (10) judging whether the text information of the node is a single character and the single character is a specified single character.

7. The method of claim 5, wherein, If it is determined according to the node information and / or the node information of the parent node of the node that the node is a node needing to be translated, the method further comprises: judging whether the node type of the node is the template string; If yes, all parent nodes of the node are traversed upwards until the uppermost parent node with the node type of the template literal is found, the text information of the parent node is taken as the original text of the script, and the node position of the parent node is taken as the position information of the script.

8. The method of claim 7, wherein, The original text of the script is translated into multiple language types to obtain translated texts of each language type, and the method further comprises: analyzing the meaning of a string in the text information of the parent node that does not need to be translated, generating a placeholder corresponding to the meaning, and replacing the string that does not need to be translated with the placeholder in the text information of the parent node; translating a string in the text information of the parent node that needs to be translated into each language type according to the meaning of the string and the meaning of the context of the string, and splicing the string with the placeholder to obtain translated texts of each language type of the text information of the parent node.

9. The method of claim 1, wherein, The internationalization calling function including translated texts of each language type is generated, and the method further comprises: generating a unique identifier of the script; building a correspondence relationship among the unique identifier, each language type, and the translated texts of each language type to obtain the internationalization calling function.

10. The method of claim 9, wherein, The script is replaced with the internationalization calling function in the source code according to the position information, and the method further comprises: taking the unique identifier as a fixed parameter of the internationalization calling function of the script, taking the language type as a dynamic parameter of the internationalization calling function of the script, and replacing the script with the internationalization calling function in the source code according to the position information.

11. The method of claim 10, wherein, When the source code includes multiple text cases that need to be replaced by the internationalization calling function, the method further includes: replacing each text case in the source code with the corresponding internationalization calling function in the order from back to front.

12. The method of claim 1, wherein, When the source code includes multiple text cases that need to be replaced by the internationalization calling function, before the original text of the text case is translated into multiple language types, the method further includes: de-duplicating multiple original texts.

13. An AST analysis based web multilingual translation apparatus, characterized by, The apparatus includes: a target language type determination unit configured to determine a target language type of a user after receiving operation information of the user on a target function module on a target page; a calling unit configured to call an internationalization calling function of a text case of source code of the target function module according to the target language type to obtain a target translation, wherein the original text of the text case and position information of the text case in the source code are determined after the source code of the target function module is analyzed by an abstract syntax tree, the original text of the text case is translated into multiple language types to obtain translations of the language types, the internationalization calling function including the translations of the language types is generated, and the text case is replaced by the internationalization calling function in the source code according to the position information; a presentation unit configured to generate a page of the target function module according to the target translation and present the page to the user; Before the source code of the target function module is analyzed by the abstract syntax tree, the apparatus is further configured to: previously traverse the source code to extract multiple strings in the source code, position information of each string in the source code, syntax characteristics of each string, and a nesting relationship between the strings; generate node information of each node corresponding to each string according to the content, position information, and syntax characteristics of each string; construct the abstract syntax tree including the nodes according to the node information and the nesting relationship.

14. A computer device comprising a memory, a processor, and a computer program stored on the memory, wherein, The processor executes the computer program to implement the method of any one of claims 1 to 12.

15. A computer-readable storage medium, characterized in that, The computer readable storage medium stores a computer program, and the computer program is executed by the processor to implement the method of any one of claims 1 to 12.

Citation Information

Patent Citations

  • Code processing method and device, storage medium and program product

    CN116521169A