Web multi-language translation method and device based on AST analysis

Through AST analysis, the copy to be translated in the Web page source code is determined, and an international call function is generated, which solves the problems of high error rates and difficulty in adapting to rapid changes caused by manual translation in the existing technology, and realizes automated multilingual translation and flexible language switching.

CN120258007AActive Publication Date: 2025-07-04BEIJING QINGWANG TECH CORP

Patent Information

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

AI Technical Summary

Technical Problem

The existing multilingual translation methods of web applications rely on manual extraction and update of translated files, resulting in high error rates and difficulty in adapting to rapidly changing business needs, and the complete automation process from code analysis to translation cannot be achieved.

Method used

Using AST analysis method, the source code is analyzed through an abstract syntax tree, the location and type of the copy to be translated is determined, and the international call function is generated, and it is replaced with the source code to realize the automation of multilingual translation.

Benefits of technology

It realizes automated multilingual translation of web pages, reduces manual operations, improves translation accuracy and efficiency, supports flexible switching of language types, and has good maintenance and scalability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120258007A_ABST
    Figure CN120258007A_ABST
Patent Text Reader

Abstract

The invention relates to a Web multi-language translation method and device based on AST analysis. After operation information of the user for the function module of the page is received, the language type corresponding to the user is determined, the internationalized calling function in the source code of the function module is called according to the language type, a translation is obtained, and the page of the function module is generated according to the translation and presented to the user. The method comprises the following steps of: analyzing an abstract syntax tree of a source code of a functional module in advance, determining an original text of a copywriting and position information of the original text in the source code, translating the original text in various language types to obtain translated texts of various language types, and generating an internationalized calling function comprising the translated texts of various language types; and replacing the to-be-translated copywriting with the internationalized calling function in the source code according to the position information. According to the method provided by the embodiment of the invention, large-scale manual search and replacement are avoided, and the maintainability and expansibility are very high.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of this specification relate to the field of artificial intelligence technology, and in particular, to a Web multi-language translation method and device based on AST analysis. Background Art

[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 methods usually rely on manually extracting the text to be translated in the Web application and translating it through translation tools, and finally generating and presenting the Web content to the user according to the translation results. This makes it necessary to manually extract and update the translation files every time new text is added or modified, which is prone to errors; and as the project scale expands, the workload of manually extracting and translating text increases exponentially, making it difficult to adapt to the rapidly changing business needs; finally, most of the existing tools can only assist in translation and cannot achieve a complete automated process from code analysis to translation. Summary of the Invention

[0003] To solve the problems existing in the prior art, the embodiments of this specification provide a Web multi-language translation method and device based on AST analysis. After receiving the operation information of the user for the target function module of the target page, the target language type corresponding to the user is determined, and the internationalization call function in the source code of the target function module is called according to the target language type to obtain the target translation. According to the target translation, the page of the target function module is generated and presented to the user. Among them, after pre-analyzing the source code of the target function module by an abstract syntax tree, the original text of the copywriting and its position information in the source code are determined, the original text is translated into multiple language types to obtain the translations of each language type, an internationalization call function including the translations of each language type is generated, and the copywriting to be translated in the source code is replaced with the internationalization call function according to the position information.

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

[0005] On the one hand, the embodiments of this specification provide a Web multi-language translation method based on AST analysis, and the method includes:

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

[0007] Call the internationalization call function of the source code text in the target function module according to the target language type to obtain the target translation. Specifically, after analyzing the abstract syntax tree of the source code of the target function module in advance, determine the original text of the text and the position information of the text in the source code, translate the original text of the text into multiple language types to obtain translations in each language type, generate an internationalization call function including the translations in each language type, and replace the text with the internationalization call function in the source code according to the position information.

[0008] Generate the page of the target function module according to the target translation and present it to the user.

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

[0010] Traverse the source code in advance, 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.

[0011] Generate node information for the nodes corresponding to each string respectively according to the content, position information, and syntactic features of each string.

[0012] Construct 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 for the nodes corresponding to each string respectively according to the content, position information, and syntactic features of each string further includes:

[0015] For a string, use the content of the string as the text information of the node corresponding to the string.

[0016] Use the position information of the string as the node position of the node corresponding to the string.

[0017] Determine the node type of the node corresponding to the string according to the syntactic features of the string.

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

[0019] Parse the abstract syntax tree to obtain the node information of each node.

[0020] For a node, determine whether the node is a node to be translated according to the node information and / or the node information of the parent node of the node;

[0021] If so, it means that the string corresponding to the node is the copywriting to be translated, take the text information of the node as the original text of the copywriting, and take the node position of the node as the position information of the copywriting.

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

[0023] Execute 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 on whether the node is a node to be translated.

[0024] Further, 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 on whether the node is a node to be translated, the method further includes:

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

[0026] If so, execute the predetermined judgment rule;

[0027] If not, the node does not need to be translated.

[0028] Further, 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 on whether the node is a node to be translated further includes:

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

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

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

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

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

[0034] (5) Judge 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;

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

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

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

[0038] (9) Determine whether the text information of this node is pure Arabic numerals;

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

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

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

[0042] If so, traverse all the parent nodes of this node upward until the topmost parent node with the node type of template literal is found. Use the text information of this parent node as the original text of the copywriting and use the node position of this parent node as the position information of the copywriting.

[0043] Further, translating the original text of the copywriting into translations of various language types further includes:

[0044] Analyze the meaning of the strings that do not need to be translated in the text information of this parent node and generate placeholders corresponding to the meaning. Replace the strings that do not need to be translated in the text information of this parent node with the placeholders;

[0045] Translate the strings that need to be translated in the text information of this parent node according to their meanings and the meanings of their contexts into translations of various language types, and splice them with the placeholders to obtain translations of various language types of the text information of this parent node.

[0046] Further, generating an internationalized call function including translations of various language types further includes:

[0047] Generate a unique identifier for this copywriting;

[0048] Construct the correspondence relationship among the unique identifier, various language types, and translations of various language types to obtain the internationalized call function.

[0049] Further, replacing the copywriting with the internationalized call function in the source code according to the position information further includes:

[0050] Take the unique identifier as a fixed parameter of the internationalization call function of the copywriting, take the language type as a dynamic parameter of the internationalization call function of the copywriting, and replace the copywriting in the source code with the internationalization call function according to the position information.

[0051] Further, when there are multiple copywritings in the source code that need to be replaced with internationalization call functions, the method further includes:

[0052] Replace each copywriting in the source code with the corresponding internationalization call function in sequence from the back to the front.

[0053] Further, when there are multiple copywritings in the source code that need to be replaced with internationalization call functions, before translating the original texts of the copywritings into multiple language types, the method further includes:

[0054] Deduplicate multiple original texts.

[0055] On the other hand, an embodiment of this specification also provides a Web multilingual translation device based on AST analysis, and the device includes:

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

[0057] A call unit, configured to call the internationalization call function of the copywriting in the source code of the target function module according to the target language type to obtain a target translation. After performing abstract syntax tree analysis on the source code of the target function module in advance, determine the original text of the copywriting and the position information of the copywriting in the source code, translate the original text of the copywriting into multiple language types to obtain translations in each language type, generate an internationalization call function including the translations in each language type, and replace the copywriting with the internationalization call 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 and present it to the user.

[0059] On the other hand, an embodiment of this specification also provides a computer device, including a memory, a processor, and a computer program stored on the memory. When the processor executes the computer program, the above method is implemented.

[0060] On the other hand, an embodiment of this specification also provides a computer-readable storage medium, where the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the above method is implemented.

[0061] Using the embodiments of this specification, it is possible to pre-improve and optimize the source code of a Web page. Through abstract syntax tree analysis, the text to be translated in the source code of each functional module of the Web page can be determined, and the text to be translated can be automatically translated, avoiding large-scale manual search and replacement. In addition, the method of the embodiments of this specification supports translation of language types, and the text to be translated in the source code can be translated at one time to obtain translations of multiple language types, and an internationalized call function including translations of each language type can be generated. The text to be translated in the source code is replaced with the generated internationalized call function, so as to decouple the text in the source code from the business logic, manage the text uniformly through the internationalized call function, make subsequent updates simple and controllable, and at the same time, adding or replacing the translation engine does not affect the overall process, having strong maintainability and scalability. BRIEF DESCRIPTION OF THE DRAWINGS

[0062] In order to more clearly illustrate the technical solutions in the embodiments of this specification or the prior art, the following will briefly introduce the drawings required to be used in the description of the embodiments or the prior art. Obviously, the drawings in the following description are only some embodiments of the embodiments of this specification. For those of ordinary skill in the art, without creative efforts, other drawings can also be obtained based on these drawings.

[0063] Figure 1 Shown is a schematic flowchart of a Web multi-language translation method based on AST analysis in the embodiments of this specification;

[0064] Figure 2 Shown is a schematic flowchart of generating an internationalized call function including translations of each language type in the embodiments of this specification;

[0065] Figure 3 Shown is a schematic flowchart of constructing an abstract syntax tree of the source code in the embodiments of this specification;

[0066] Figure 4 Shown is a schematic flowchart of determining the original text of the copywriting and the position information of the copywriting in the source code after pre-analyzing the abstract syntax tree of the source code of the target functional module in the embodiments of this specification;

[0067] Figure 5 Shown is a schematic flowchart of processing text with variables in the embodiments of this specification;

[0068] Figure 6 Shown is a schematic flowchart of translating the original text of the copywriting into translations of multiple language types to obtain translations of each language type in the embodiments of this specification;

[0069] Figure 7The following is a schematic structural diagram of a Web multilingual translation device based on AST analysis in the embodiments of this specification;

[0070] Figure 8 The following is a schematic structural diagram of a computer device in the embodiments of this specification.

[0071]

Explanation of the reference numerals in the drawings

[0072] 701. Target language type determination unit;

[0073] 702. Invocation 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 implementation manners

[0087] Next, the technical solutions in the embodiments of this specification will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of this specification. Obviously, the described embodiments are only a part of the embodiments of this specification, rather than all the embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments in this specification without creative efforts shall fall within the protection scope of the embodiments of this specification.

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

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

[0090] It should be noted that in the embodiments of this specification, some industry-existing solutions such as certain software, components, models, etc. may be mentioned. They should be regarded as exemplary. The purpose is only to illustrate the feasibility in the implementation of the technical solutions of this application, but it does not mean that the applicant has already or necessarily used this solution.

[0091] In view of the problems existing in the prior art, the embodiments of this specification provide a Web multilingual translation method based on AST analysis. Figure 1 The following is a schematic flowchart of the Web multilingual translation method based on AST analysis in the embodiments of this specification. The translation process of the text on the page is described in this figure. The order of steps listed in the embodiments is only one way among the execution orders of many steps, and does not represent the only execution order. When the actual system or device product executes, it can be executed in the order of the method shown in the embodiments or the drawings, or executed in parallel.

[0092] Specifically, as Figure 1 shown, the method may include:

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

[0094] Step 102: Invoke the internationalization call function for the copywriting in the source code of the target function module according to the target language type to obtain the target translation. Specifically, after performing abstract syntax tree analysis on the source code of the target function module in advance, determine the original text of the copywriting and its position information in the source code, translate the original text of the copywriting into translations of multiple language types, generate an internationalization call function including the translations of multiple language types, and replace the copywriting in the source code with the internationalization call function according to the position information.

[0095] Step 103: Generate the page of the target function module according to the target translation and present it to the user.

[0096] Using the embodiments of this specification, it is possible to pre-improve and optimize the source code of a Web page. Through abstract syntax tree analysis, determine the copywriting that needs to be translated in the source code of each function module of the Web page, and automatically translate the copywriting that needs to be translated, avoiding large-scale manual search and replacement. In addition, the method of the embodiments of this specification supports translation of language types, can translate the copywriting that needs to be translated in the source code at one time to obtain translations of multiple language types, and generate an internationalization call function including the translations of multiple language types. Replace the copywriting that needs to be translated in the source code with the generated internationalization call function, so as to decouple the copywriting in the source code from the business logic, uniformly manage the copywriting through the internationalization call function, make subsequent updates simple and controllable, and at the same time adding or replacing the translation engine does not affect the overall process, having strong maintainability and scalability.

[0097] In the embodiments of this specification, the operations of the user on the target function module of the target page may include operations such as the user logging in to the target page and the user changing the language type of the target page. The target page can record the user's identity information, the target language type selected by the user, etc. If the target page records the user's identity information, the matching language type can be determined according to the user's identity information as the target language type. Then it is necessary to present the page in the target language type to the user. Therefore, the embodiments of this specification translate the copywriting in the target page into the translation in the target language type and generate a page according to the translation.

[0098] In the embodiments of this specification, perform abstract syntax tree analysis on the source code of the target page in advance, identify the copywriting that needs to be translated in the source code and its position in the source code, and then translate the copywriting that needs to be translated into translations of multiple language types. Then generate an internationalization call function including the translations of multiple language types. Optionally, as Figure 2 shown, generating an internationalization call function including the translations of multiple language types further includes:

[0099] Step 201: Generate a unique identifier for the copywriting.

[0100] Step 202: Construct the correspondence among the unique identifier, each language type, and the translations of each language type to obtain the internationalization call function.

[0101] In the embodiments of this specification, the internationalization call function may include two input parameters. The first input parameter is the unique identifier, and the second input parameter is the language type. The output result of the internationalization call function is the translation corresponding to the input unique identifier and language type. Among them, the unique identifier is the unique identifier of the copywriting in the source code. When it is determined through the abstract syntax tree analysis method that the copywriting in the source code needs to be translated, the original text of the copywriting is automatically translated into translations of multiple language types, and then the internationalization call function is generated and the copywriting in the source code is replaced with the internationalization call function.

[0102] The replacement of the copywriting with the internationalization call function in the source code according to the position information in this specification further includes:

[0103] Use the unique identifier as the fixed parameter of the internationalization call function of the copywriting, use the language type as the dynamic parameter of the internationalization call function of the copywriting, and replace the copywriting in the source code with the internationalization call function 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. When the source code executes to the position of the internationalization call function, the internationalization call function will be called, and the internationalization call function will return the translation of the copywriting at this place in the source code.

[0105] It can be understood that the function names of all internationalization call functions in the source code of the embodiments of this specification can be the same. According to the input unique identifier and language type, the corresponding translation is returned. After translating the copywriting that needs to be translated in the source code in the embodiments of this specification, the copywriting is replaced with the internationalization call function at the position of the copywriting in the source code. Therefore, the first input parameter (unique identifier) of the internationalization call function at this position in the source code is fixed, and the second input parameter (language type) is the analyzed target language type (that is, the language type that the target page needs to present to the user).

[0106] It should be noted that in addition to the unique identifier and the language type, the internationalization call function may also have more input parameters. The staff can set more input parameters of the internationalization call function according to the code structure and actual scenario, and the embodiments of this specification do not make restrictions.

[0107] In other scenarios of the embodiments of this specification, the copywriting that needs to be translated in the source code can be hard-coded copywriting. For example, when a developer writes code, the copywriting that needs to be presented to the user in the source code is hard-coded as a certain language. Such copywriting is hard-coded copywriting. In this case, it is very difficult for the Web page to support language switching because when the source code is executed, only the copywriting in this one language can be used to generate the page and present it to the user, causing great inconvenience to the user. In this case, the method of the embodiments of this specification can be applied to transform the source code. First, perform an analysis of the abstract syntax tree on the source code to determine the hard-coded copywriting in the source code, then translate the hard-coded copywriting into multiple language types, then generate an internationalized call function for the hard-coded copywriting, and finally replace the hard-coded copywriting in the source code with the internationalized call function. After that, when the source code is executed, it will call the internationalized call function, and the internationalized call function will return the translated text of the corresponding language type, thus realizing flexible language switching on the Web page. For users, the experience is better. For developers, the source code is more standardized, and in subsequent coding processes, the internationalized call function can be directly called for the same hard-coded copywriting, and the first parameter input is the unique identifier of the hard-coded copywriting, improving the coding efficiency.

[0108] How to identify the copywriting (hard-coded copywriting) that needs to be translated in the source code is another key point of the embodiments of this specification. Specifically, the embodiments of this specification use the abstract syntax tree analysis method to identify the copywriting that needs to be translated in the source code.

[0109] First, the embodiments of this specification construct an abstract syntax tree of the source code. As Figure 3 shown, the steps of constructing an abstract syntax tree of the source code include:

[0110] Step 301: Traverse the source code in advance, 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 corresponding to each string according to the content, position information, and syntactic features of each string respectively;

[0112] Step 303: Construct the abstract syntax tree including each node according to the node information and the nesting relationship.

[0113] In the embodiments of this specification, the source code is composed of multiple strings. For example:

[0114] const text:string=‘Hello’

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

[0116] Among them, 'Hello' can be a string, text:string = 'Hello' can be a string, const text:string = 'Hello' can also be a string. Thus, there is a nested relationship among multiple strings. In addition, each string in the source code has its own syntactic features, which can include keywords, identifiers, operators, delimiters, comments, data types, functions and methods, classes and objects, literals, expressions and statements, template strings, text content, etc. And the position of each string in the source code can also be parsed, for example, the line where the string is located in the source code, the starting character number and character length in that line, or the starting character number and character length of the string in the source code, etc.

[0117] Therefore, the embodiments of this specification traverse the source code in advance, 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 nested relationship among each string. Then, node information corresponding to each string is generated respectively according to the content, position information, and syntactic features of each string. Finally, an abstract syntax tree including each node is constructed according to the node information and the nested relationship. In the abstract syntax tree, each node corresponds to a string in the source code, and the parent node of the node is the string that has an upper-level nested relationship with the string corresponding to this node. For example, 'Hello' is a node A in an abstract syntax tree, the parent node B of this node A corresponds to the string text:string = 'Hello', and the parent node C of node B corresponds to the string const text:string = 'Hello'. The node information of each node includes text information, node position, and node type. Among them, the text information is the content of the string corresponding to this node, the node position is the position information of the string corresponding to this node in the source code, and the node type is determined according to the syntactic features of the string corresponding to this node. Exemplarily, the node type includes but is not limited to literal (Literal), template element (TemplateElement), text content (JSXText), binary operation expression (BinaryExpression), call expression (CallExpression), attribute type in JSX element (JSXAttribute), template literal (TemplateLiteral).

[0118] It should be noted that the Chinese names of the above node types are only examples, and different technicians in the field may have slightly different Chinese names, which are not restricted by the embodiments of this specification.

[0119] Such as Figure 4As shown, after pre - analyzing the source code of the target function module to obtain the abstract syntax tree, determining the original text of the copywriting and the position information of the copywriting in the source code further includes:

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

[0121] Step 402: For a node, determine whether the node is a node to be translated according to the node information and / or the node information of the parent node of the node;

[0122] If so, it means that the string corresponding to the node is the copywriting to be translated. Take the text information of the node as the original text of the copywriting and the node position of the node as the position information of the copywriting.

[0123] According to an embodiment of the present specification, in order to reduce the calculation amount, nodes can also be preliminarily screened according to the node type. Specifically, before obtaining the judgment result of whether the node is a node to be translated by executing a predetermined judgment rule according to the node information and / or the node information of the parent node of the node, the method further includes:

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

[0125] If so, execute the predetermined judgment rule;

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

[0127] In the embodiments of the present specification, it is possible to determine according to experience which types of nodes may contain hard - coded content in their text information and which types of nodes cannot have hard - coded content in their text information. Take the node types that may contain hard - coded content as the predetermined type, so as to preliminarily screen nodes using the predetermined type and reduce the calculation amount of subsequent rule judgment.

[0128] Preferably, in the embodiments of the present specification, only the text information of nodes of the three types of Literal, TemplateElement, and JSXText may contain hard - coded content. Therefore, if the node type is any one of these 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, obtaining the judgment result of whether the node is a node to be translated by executing a predetermined judgment rule according to the node information and / or the node information of the parent node of the node further includes:

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

[0131] (1) Determine whether the text information of the node is a URL address; specifically, the regular expression method can be used to determine whether the text information of the node is a URL address. 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, a pure type refers to a node type that does not contain side effects or additional behaviors. These nodes only represent syntactic structures or data, without involving calculations, side effects, or runtime behaviors. For example, type declarations (TSTypeAliasDeclaration), syntactic structures for defining interfaces (TSInterfaceDeclaration), literal type declarations (TSLiteralType), type annotations (TSTypeAnnotation), etc. If the type of the parent node 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 regular expression method can be used to determine whether the text information is a color expression. If so, 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 node type of the parent node 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 a plus sign; specifically, if the node type of the parent node of the node is BinaryExpression and the operator is a plus sign, 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 checking whether the trimmed original text contained in the node is an empty string.

[0137] (7) Determine whether the node type of the parent node of this node is CallExpression and whether the function name is the specified function name; specifically, if the type of the parent node of this node is CallExpression and the function name is the specified function name, then this 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 this specification do not make any restrictions. For example, in the source code after being replaced with an internationalized call function, the first input parameter of the internationalized call function is a unique identifier, which is also a string. When analyzing the abstract syntax tree, this unique identifier is a node, and the type of this node is TemplateElement. It is obviously unreasonable to translate the unique identifier as well. Therefore, the embodiments of this specification determine whether the node type of the parent node corresponding to the unique identifier is CallExpression, that is, to determine whether the node type corresponding to the string of the internationalized call function is CallExpression. If so, it is also necessary to further determine whether the function name is the function name of the internationalized call function. If so, it means that this is the unique identifier passed in by the internationalized call function and does not need to be translated. Of course, the function names of some other functions can also be used as the specified function name, such as the function name for listening to subscription / publishing events, etc.

[0138] (8) Determine whether the node type of the parent node of this node is JSXAttribute, and whether the attribute name is the specified attribute name and the component name is the specified component name; specifically, if the type of the parent node of this node is JSXAttribute and the attribute name is the specified attribute name and the component name is the specified component name, then this 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. It can be determined whether it is an attribute of a dom / component by using the node and recursively searching for the type of the parent node, and then it can be seen whether it needs to be translated according to the two dimensions of the attribute name and the dom / component name.

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

[0140] (10) Determine whether the text information of this node is a single character and the single character is the specified single character; specifically, if the text information of this node is a single character and the single character is the specified single character, then this node does not need to be translated. For example, single characters such as mathematical operation symbols, percent signs, and floating lines do not need to be translated.

[0141] In some other embodiments of this specification, it is also possible to determine whether the language type of the text information is the specified language type by combining the text information included in the node with regular expressions. If so, translation is not required. The specified language type can be set according to the actual situation, and this judgment rule can be determined whether to be enabled according to the actual needs of the project.

[0142] It should be noted that the above judgment rules are all independent, do not interfere with each other, and any number of the above can be enabled or disabled. In addition, developers are also allowed to customize rules, such as skipping when encountering members of an enumeration, skipping when encountering an IP, etc., which are not limited in the embodiments of this specification.

[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 the translation. Then an internationalization call function is generated for replacement.

[0144] When replacing, for a string without variables, directly replace the string with the internationalization call function in the source code, that is, the string itself is the original text to be translated, so it can be replaced according to the position information of the string.

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

[0146] Specifically, as Figure 5 shown, if it is determined that 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, the method further includes:

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

[0148] In the embodiments of this specification, only the text information of nodes with the node type of TemplateElement may carry variables, and the text information of nodes with the node types of Literal and JSXText do not carry variables and can be directly replaced.

[0149] Step 502: If so, traverse all the parent nodes of the node upward until the topmost parent node with the node type of TemplateLiteral is found, use the text information of the parent node as the original text of the copywriting, and use the node position of the parent node as the position information of the copywriting.

[0150] In the embodiments of this specification, the TemplateElement must be under the TemplateLiteral, and template strings can continue to nest template strings. Therefore, the strategy is to merge the TemplateElement into the top-level 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, and the text information of this node is 'Hello'. 'Hello, ${username}' is a string, corresponding to a node of the TemplateLiteral type, and 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, so 'Hello, ${username}' is text information carrying a 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, translating 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 the Figure 6 method shown, translation is realized by combining the context meaning, improving the accuracy of translation.

[0156] Meanwhile, if the string that does not need to be translated includes variable names, record the mapping relationship between the placeholder and the string that does not need to be translated. The internationalized call function that generates translations in various language types includes: using the placeholder as the input parameter of the internationalized call function. When replacing the copywriting with the internationalized call 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 calling the internationalized call function, calculate the value of the placeholder according to the mapping relationship and the variable value of the variable name. When calling the internationalized function, input the value of the placeholder into the internationalized call function, so as to use the value of the placeholder to replace the corresponding placeholder in the translation to obtain the target translation, generate the page of the target functional module according to the target translation and present it to the user. Exemplarily, in javascript / typescript, string templates usually use ${} to represent variables. The text information of this parent node is 'The month-on-month growth rate of a certain resource is ${(newCount-oldCount) / oldCount*100}%'. Among them, the string in '${}' does not need to be translated. Therefore, the embodiments of this specification analyze the meaning of the string '(newCount-oldCount) / oldCount*100' and conclude that its meaning is a percentage. Therefore, the placeholder generated for this string is 'percentage'. Replace the original string with the placeholder 'percentage' to get 'The month-on-month growth rate of a certain resource is {percentage}%'. Translate according to the meaning of the string that needs to be translated 'The month-on-month growth rate of a certain resource is' and the meaning of its context percentage, without translating the content 'percentage' wrapped by '{}', and the content 'percentage' in the curly brackets is output as it is. The reason for changing it to {} instead of keeping the original ${} is that the internal grammar implementation of most internationalized call functions is inconsistent with the original language. For example, in javascript / typescript, string templates usually use ${} to represent variables, while the internal grammar implementation of internationalized call functions uses {} to represent variables.

[0157] Among them, both newCount and oldCount are variable names. Therefore, when replacing the internationalized call function into the source code, it is also necessary to add a mapping relationship in the source code. For example, percentage=(newCount-oldCount) / oldCount*100.

[0158] According to an embodiment of this specification, in the replacement stage, since the character length of the replaced internationalized call 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 this specification replace each piece of text in the source code with the corresponding internationalized call function in the order from back to front, so as to avoid affecting the original text position information in the front.

[0159] In addition, in order to save translation costs, when there are multiple pieces of text in the source code that need to be replaced with internationalized call functions, before translating the original text of the pieces of text in multiple language types, the method further includes: removing duplicates from the multiple original texts, so as to avoid multiple identical original texts being translated multiple times, which can reduce the translation cost to a certain extent.

[0160] It should be noted that before performing AI automated translation, the original text may also need to be converted into a format that can be recognized by the AI, so that the AI can accurately output the translation. The specific format to be converted needs to be determined according to the AI automated translation tool, and the embodiments of this specification will not elaborate.

[0161] In some other embodiments of this specification, real-time translation can also be performed. Specifically, after receiving the operation information of the user for the target function module on the target page, determine the target language type corresponding to the user; then traverse the source code of the target function module, 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; generate node information for each node corresponding to each string according to the content, position information, and syntactic features of each string respectively; construct the abstract syntax tree including each node according to the node information and the nesting relationship. Then parse the abstract syntax tree to obtain the node information of each node; for a node, determine whether the node is a node that needs to be translated according to the node information of this node and / or the node information of the parent node of this node; if so, it means that the string corresponding to this node is the piece of text that needs to be translated, use the text information of this node as the original text of the piece of text, and use the node position of this node as the position information of the piece of text. Then translate the original text to obtain the translation, replace the original text in the source code with the translation according to the position information, and finally generate the page of the target function module according to the translation and present it to the user.

[0162] Among them, the process of constructing the abstract syntax tree and the analysis process of the abstract syntax tree are the same as the processes described above. The difference is that in the process of real-time translation, there is no need to construct internationalized call functions, but directly replace the original text in the source code with the translation obtained according to the position information of the original text. The repeated parts will not be elaborated.

[0163] Based on the same inventive concept, an embodiment of this specification also provides a Web multilingual translation device based on AST analysis. As Figure 7 shown, the device includes:

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

[0165] An invocation unit 702, configured to invoke an internationalization invocation function of the text in the source code of the target function module according to the target language type to obtain a target translation. After performing abstract syntax tree analysis on the source code of the target function module in advance, the original text of the text and the 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 translations of each language type, an internationalization invocation function including the translations of each language type is generated, and the text is replaced with the internationalization invocation function in the source code according to the position information;

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

[0167] Since the principle of the above device for solving problems is similar to that of 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 elaborated.

[0168] As Figure 8 shown is a schematic structural diagram of a computer device according to an embodiment of this specification. The permeability calculation unit or the diversion capacity calculation unit in the present invention may be the computer device in this embodiment to execute the method of the present invention. The computer device 802 may include one or more processing devices 804, such as one or more central processing units (CPUs), and each processing unit may implement one or more hardware threads.

[0169] The computer device 802 may further include any storage resource 806, which is used to store any type of information such as code, settings, data, etc.

[0170] Non-limiting, for example, the storage resource 806 may include any one or more combinations of the following: any type of RAM, any type of ROM, flash memory devices, hard disks, optical discs, etc.

[0171] More generally, any storage resource may use any technology to store information.

[0172] Furthermore, any storage resource may 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 case, when the processing device 804 executes the associated instructions stored in any storage resource or combination of storage resources, the computer device 802 can perform any operation of the associated instructions. The computer device 802 also includes one or more drive 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 may also include an input / output module 810 (I / O) for receiving various inputs (via the input device 812) and for providing various outputs (via the output device 814). A specific output mechanism may include a presentation device 816 and an associated graphical user interface (GUI) 818. In other embodiments, the input / output module 810 (I / O), the input device 812, and the output device 814 may not be included, and it may only be a computer device in the network. The computer device 802 may 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 components described above together.

[0176] The communication link 822 can be implemented in any way, for example, 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 link 822 can include any combination of hardwired links, wireless links, routers, gateway functions, name servers, etc. governed by any protocol or combination of protocols.

[0177] The embodiments of this specification also provide a computer-readable storage medium storing a computer program, and when the computer program is executed by a processor, the above method is implemented.

[0178] The embodiments of this specification also provide a computer-readable instruction, and when the processor executes the instruction, the program therein causes the processor to execute the above method.

[0179] It should be understood that in the various embodiments of the embodiments of this specification, the magnitudes of the sequence numbers of the above processes do not mean the order of execution. The order of execution of each process should be determined by its function and internal logic, and should not constitute any limitation to the implementation process of the embodiments of this specification.

[0180] It should also be understood that in the embodiments of this specification, the term "and / or" is merely a description of the association relationship between associated objects, indicating that three relationships may exist. For example, A and / or B may represent three situations: A exists alone, A and B exist simultaneously, and B exists alone. In addition, in the embodiments of this specification, the character " / " generally indicates that the associated objects before and after are in an "or" relationship.

[0181] Those of ordinary skill in the art can realize that the units and algorithm steps of each example described in combination with the embodiments disclosed in this specification can be implemented by electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described according to functions in the above description. Whether these functions are executed in a hardware or software manner depends on the specific application and design constraints of the technical solution. Professional technicians can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of the embodiments of this specification.

[0182] Those skilled in the art can clearly understand that for the convenience and brevity of description, the specific working processes of the systems, devices, and units described above can refer to the corresponding processes in the foregoing method embodiments and will not be elaborated here.

[0183] In several embodiments provided in the embodiments of this specification, it should be understood that the disclosed systems, devices, and methods can be implemented in other ways. For example, the device embodiments described above are merely illustrative. For example, the division of the units is only a logical function division. In actual implementation, there may be other division methods. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the displayed or discussed coupling or direct coupling or communication connection to each other can be an indirect coupling or communication connection through some interfaces, devices, or units, or can also be in the form of electrical, mechanical, or other connections.

[0184] The units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they can be located in one place, or can also be distributed to multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiments of this specification.

[0185] In addition, in each embodiment of the embodiments of this specification, the functional units can be integrated into one processing unit, or each unit can exist physically alone, or two or more units can be integrated into one unit. The above integrated units can be implemented in the form of hardware or in the form of software functional units.

[0186] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the embodiments of this specification, in essence, or the part that contributes to the prior art, or all or part of this technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this specification. The foregoing storage medium includes: various media such as USB flash drives, mobile hard disks, read-only memories (ROMs), random access memories (RAMs), magnetic disks, or optical discs that can store program codes.

[0187] In the embodiments of this specification, specific embodiments are used to elaborate on the principles and implementation manners of the embodiments of this specification. The description of the above embodiments is only used to help understand the methods and their core ideas of the embodiments of this specification; at the same time, for those of ordinary skill in the art, according to the ideas of the embodiments of this specification, there will be changes in the specific implementation manners and application scopes. In summary, the content of this specification should not be construed as a limitation to the embodiments of this specification.

Claims

1. A Web multilingual translation method based on AST analysis, characterized in that The method includes: 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; Invoking the internationalization call function of the copywriting in the source code of the target function module according to the target language type to obtain the target translation. Wherein, after pre-analyzing the source code of the target function module to obtain the abstract syntax tree, determining the original text of the copywriting and the position information of the copywriting in the source code, translating the original text of the copywriting into multiple language types to obtain translations in each language type, generating an internationalization call function including the translations in each language type, and replacing the copywriting with the internationalization call 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 it to the user.

2. The method according to claim 1, characterized in that, Before analyzing the source code of the target function module to obtain the abstract syntax tree, the method further includes: Pre-traversing the source code 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; Generating node information corresponding to each string according to the content, position information, and syntactic features of each string respectively; Constructing the abstract syntax tree including each node according to the node information and the nesting relationship.

3. The method according to claim 2, characterized in that, The node information includes text information, node position, and node type; Generating node information corresponding to each string according to the content, position information, and syntactic features of each string respectively further includes: For one of the strings, taking the content of the string as the text information of the node corresponding to the string; Taking the position information of the string as the node position of the node corresponding to the string; Determining the node type of the node corresponding to the string according to the syntactic features of the string.

4. The method according to claim 3, characterized in that After pre-analyzing the source code of the target function module to obtain the abstract syntax tree, determining the original text of the copywriting and the position information of the copywriting in the source code further includes: Parsing the abstract syntax tree to obtain the node information of each node; For a node, determining whether the node is a node to be translated according to the node information of the node and / or the node information of the parent node of the node; If so, indicating that the string corresponding to the node is the copywriting to be translated, taking the text information of the node as the original text of the copywriting, and taking the node position of the node as the position information of the copywriting.

5. The method according to claim 4, characterized in that, Determining whether the node is a node to be translated according to the node information of the node and / or the node information of the parent node of the node further includes: Executing a predetermined judgment rule according to the node information of the node and / or the node information of the parent node of the node to obtain a judgment result on whether the node is a node to be translated.

6. The method according to claim 5, wherein Before executing a predetermined judgment rule according to the node information of the node and / or the node information of the parent node of the node to obtain a judgment result on whether the node is a node to be translated, the method further includes: Judging whether the node type of the node is a predetermined type, and the predetermined type includes any one of literal, template string, and text content; If so, execute the predetermined judgment rule; If not, this node does not need to be translated.

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

8. The method according to claim 6, wherein If it is determined that this node is a node that needs to be translated according to the node information and / or the node information of the parent node of this node, the method further includes: Judge whether the node type of this node is the template string; If so, traverse all the parent nodes of this node upwards until the topmost parent node with the node type of template literal is found. Take the text information of this parent node as the original text of the copywriting, and take the node position of this parent node as the position information of the copywriting.

9. The method according to claim 8, characterized in that, Translating the original text of the copywriting into translations of various language types further includes: Analyze the meaning of the strings that do not need to be translated in the text information of this parent node, generate placeholders corresponding to the meanings, and replace the strings that do not need to be translated in the text information of this parent node with the placeholders; Translate the strings that need to be translated according to the meaning of the strings that need to be translated in the text information of this parent node and the meaning of their context into translations of various language types, and splice them with the placeholders to obtain translations of various language types of the text information of this parent node.

10. The method according to claim 1, wherein Generating an internationalized call function including translations of various language types further includes: Generate a unique identifier for this copywriting; Construct the corresponding relationship among the unique identifier, various language types, and translations of various language types to obtain the internationalized call function.

11. The method according to claim 10, wherein Replacing the copywriting with the internationalized call function in the source code according to the position information further includes: Take the unique identifier as a fixed parameter of the internationalized call function of this copywriting, take the language type as a dynamic parameter of the internationalized call function of this copywriting, and replace the copywriting with the internationalized call function in the source code according to the position information.

12. The method according to claim 11, wherein When there are multiple pieces of copywriting in the source code that need to replace the internationalization call functions, the method further includes: Sequentially replacing each piece of copywriting in the source code with the corresponding internationalization call function in reverse order from back to front.

13. The method according to claim 1, wherein When there are multiple pieces of copywriting in the source code that need to replace the internationalization call functions, before translating the original text of the copywriting into multiple language types, the method further includes: Removing duplicates from multiple original texts.

14. A Web multilingual translation device based on AST analysis, characterized in that The apparatus includes: A target language type determination unit, configured to determine the target language type corresponding to the user after receiving operation information of the user for a target function module on a target page; A call unit, configured to call the internationalization call function of the copywriting in the source code of the target function module according to the target language type to obtain a target translation. After performing abstract syntax tree analysis on the source code of the target function module in advance, the original text of the copywriting and the position information of the copywriting in the source code are determined, the original text of the copywriting is translated into multiple language types to obtain translations of each language type, an internationalization call function including the translations of each language type is generated, and the copywriting is replaced with the internationalization call 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 it to the user.

15. A computer device, comprising a memory, a processor, and a computer program stored on the memory, characterized in that, When the processor executes the computer program, the method according to any one of claims 1 to 13 is implemented.

16. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, and when the computer program is executed by the processor, the method according to any one of claims 1 to 13 is implemented.

Citation Information

Patent Citations

  • Original language replacement method based on multi-language tags

    CN107357568A

  • Code processing method and related equipment

    CN113076167A

  • International file translation method and device, equipment and medium

    CN116227505A

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

    CN116521169A

  • Code processing method, device and equipment and readable storage medium

    CN117234507A

Cited By

  • Front-end internationalization automatic processing method and system based on tool chain collaboration

    CN121680861A