CN116521169A - Code processing method, apparatus, storage medium, and program product - Google Patents

Code processing method, apparatus, storage medium, and program product Download PDF

Info

Publication number
CN116521169A
CN116521169A CN202210078642.8A CN202210078642A CN116521169A CN 116521169 A CN116521169 A CN 116521169A CN 202210078642 A CN202210078642 A CN 202210078642A CN 116521169 A CN116521169 A CN 116521169A
Authority
CN
China
Prior art keywords
translated
translation
code
file
target
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202210078642.8A
Other languages
Chinese (zh)
Inventor
邓敏捷
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Zitiao Network Technology Co Ltd
Original Assignee
Beijing Zitiao Network Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Zitiao Network Technology Co Ltd filed Critical Beijing Zitiao Network Technology Co Ltd
Priority to CN202210078642.8A priority Critical patent/CN116521169A/en
Publication of CN116521169A publication Critical patent/CN116521169A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/44Encoding
    • G06F8/443Optimisation
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/42Syntactic analysis
    • G06F8/425Lexical analysis
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/44Encoding
    • G06F8/447Target code generation

Abstract

The embodiment of the disclosure provides a code processing method, a device, a storage medium and a program product, wherein the code processing method provided by the embodiment of the invention obtains a grammar tree of a code file through determining the type of the code file to be processed and a target loader corresponding to the type of the code file; extracting a data unit from the grammar tree according to a preset domain-specific language, wherein the data unit comprises a document to be translated and attribute information; obtaining a translation of a target language corresponding to the to-be-translated version, and generating a target sentence corresponding to the to-be-translated version according to the translation and the attribute information; and replacing the file to be translated in the code file with a target sentence corresponding to the file to be translated, and obtaining the internationalized code file. The target loader automatically acquires the text to be translated, automatically generates the corresponding target sentence, and further replaces the text in the code file, so that an automatic code internationalization process is realized, and internationalization development efficiency improvement and quality guarantee of code project cross languages are improved.

Description

Code processing method, apparatus, storage medium, and program product
Technical Field
The embodiment of the disclosure relates to the technical field of computer and network communication, in particular to a code processing method, a device, a storage medium and a program product.
Background
At present, internationalization resources are required to be configured in a development stage when multi-language webpages or software are produced, namely, a code is required to be translated into a document of a target language type. When the webpage is rendered or the software runs, the webpage is displayed in the corresponding language type according to the language environment selected by the user.
In the prior art, when internationalization is performed on a code of an application program, research and development personnel are usually required to extract a document from the code, non-research and development personnel (products and translation personnel) are required to maintain the document and translate the document, and then research and development personnel are required to implement final internationalization of the code according to the translation, so that the whole process is long in time consumption and low in efficiency, document maintenance is complex, and errors are easy to occur.
Disclosure of Invention
The embodiment of the disclosure provides a code processing method, a device, a storage medium and a program product, so as to realize an automatic code internationalization process, improve international development efficiency improvement of code projects across languages and ensure quality.
In a first aspect, an embodiment of the present disclosure provides a code processing method, including:
Determining the type of a code file to be processed, and acquiring a grammar tree of the code file through a target loader corresponding to the type of the code file;
extracting a data unit from the grammar tree according to a preset domain-specific language, wherein the data unit comprises a to-be-translated version and attribute information;
obtaining a translation of a target language corresponding to a to-be-translated version, and generating a target sentence corresponding to the to-be-translated version according to the translation and the attribute information;
and replacing the to-be-translated version in the code file with a target sentence corresponding to the to-be-translated version to obtain an internationalized code file.
In a second aspect, an embodiment of the present disclosure provides a code processing apparatus, including:
the loading unit is used for determining the type of the code file to be processed and acquiring a grammar tree of the code file through a target loader corresponding to the type of the code file;
the extraction unit is used for extracting data units from the grammar tree according to a preset domain-specific language, wherein the data units comprise a to-be-translated version and attribute information;
the translation unit is used for obtaining the translation of the target language corresponding to the to-be-translated version and generating a target sentence corresponding to the to-be-translated version according to the translation and the attribute information;
And the restoring unit is used for replacing the to-be-translated version in the code file with the target statement corresponding to the to-be-translated version to obtain the internationalized code file.
In a third aspect, an embodiment of the present disclosure provides an electronic device, including: at least one processor and memory;
the memory stores computer-executable instructions;
the at least one processor executes computer-executable instructions stored in the memory such that the at least one processor performs the code processing method as described above in the first aspect and the various possible designs of the first aspect.
In a fourth aspect, embodiments of the present disclosure provide a computer-readable storage medium having stored therein computer-executable instructions which, when executed by a processor, implement the code processing method as described above in the first aspect and the various possible designs of the first aspect.
In a fifth aspect, embodiments of the present disclosure provide a computer program product comprising computer-executable instructions which, when executed by a processor, implement the code processing method according to the first aspect and the various possible designs of the first aspect.
The code processing method, the device, the storage medium and the program product provided by the embodiment of the disclosure, the code processing method provided by the embodiment of the disclosure obtains a grammar tree of a code file by determining the type of the code file to be processed and a target loader corresponding to the type of the code file; extracting a data unit from the grammar tree according to a preset domain-specific language, wherein the data unit comprises a to-be-translated version and attribute information; obtaining a translation of a target language corresponding to the to-be-translated version, and generating a target sentence corresponding to the to-be-translated version according to the translation and the attribute information; and replacing the file to be translated in the code file with a target sentence corresponding to the file to be translated, and obtaining the internationalized code file. According to the embodiment of the disclosure, the target loader automatically acquires the text to be translated, and automatically generates the corresponding target sentence, so that the text to be translated is replaced in the code file, an automatic code internationalization process is realized, and international development efficiency improvement and quality guarantee of code project cross languages are improved.
Drawings
In order to more clearly illustrate the embodiments of the present disclosure or the solutions in the prior art, a brief description will be given below of the drawings that are needed in the embodiments or the description of the prior art, it being obvious that the drawings in the following description are some embodiments of the present disclosure, and that other drawings may be obtained from these drawings without inventive effort to a person of ordinary skill in the art.
FIG. 1 is a system example diagram of a code processing method according to an embodiment of the present disclosure;
FIG. 2 is a flow chart of a code processing method according to an embodiment of the disclosure;
FIG. 3 is a schematic diagram of a syntax tree and a data unit according to an embodiment of the disclosure;
FIG. 4 is a flowchart of a code processing method according to another embodiment of the present disclosure;
FIG. 5a is a diagram of a general data unit according to one embodiment of the present disclosure;
FIG. 5b is a schematic diagram of a data unit and internationalized document function according to one embodiment of the present disclosure;
FIG. 6a is a pipeline code schematic of a code processing method according to an embodiment of the present disclosure;
FIG. 6b is a pipeline diagram of a code processing method according to an embodiment of the present disclosure;
FIG. 7 is a block diagram of a code processing apparatus provided in an embodiment of the present disclosure;
fig. 8 is a schematic hardware structure of an electronic device according to an embodiment of the disclosure.
Detailed Description
For the purposes of making the objects, technical solutions and advantages of the embodiments of the present disclosure more apparent, the technical solutions of the embodiments of the present disclosure will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present disclosure, and it is apparent that the described embodiments are some embodiments of the present disclosure, but not all embodiments. All other embodiments, which can be made by one of ordinary skill in the art without inventive effort, based on the embodiments in this disclosure are intended to be within the scope of this disclosure.
In the prior art, when internationalization is performed on a code of an application program, research and development personnel are usually required to extract a document from the code, non-research and development personnel (products and translation personnel) are required to maintain the document and translate the document, and then research and development personnel are required to implement final internationalization of the code according to the translation, so that the whole process is long in time consumption and low in efficiency, document maintenance is complex, and errors are easy to occur.
Aiming at the technical problems, the disclosure provides a code processing method, which is used for realizing an automatic code internationalization process without manual processing and maintenance, improving internationalization development efficiency improvement and quality assurance of code projects in cross languages, and specifically, obtaining a document to be translated in a code file by determining the type of the code file to be processed and a target loader corresponding to the type of the code file; and obtaining the translation of the target language corresponding to the to-be-translated document, generating a target sentence corresponding to the to-be-translated document according to the translation and the attribute information, and replacing the to-be-translated document in the code file with the target sentence corresponding to the to-be-translated document to obtain the internationalized code file.
The code processing method provided by the present disclosure is applied to a system as shown in fig. 1, and includes a storage device 101 and a processing device 102, where the storage device 101 stores a code folder to be processed (a code folder of an application program), the processing device 102 may scan the code folder to be processed of the processing device 101 to obtain a code file to be processed, determine a type of the code file to be processed, obtain a syntax tree of the code file through a target loader corresponding to the type of the code file, extract a data unit from the syntax tree according to a preset domain-specific language, where the data unit includes a to-be-translated version and attribute information, obtain a translation of a target language corresponding to the to-be-translated version, generate a target sentence corresponding to the to-be-translated version, replace the to-be-translated version in the code file with the target sentence corresponding to the to-be-translated version, obtain an internationalized code file, and store the internationalized code file in the storage device 101.
The following description of the embodiments of the present disclosure will be made clearly and fully with reference to the accompanying drawings, in which it is evident that the embodiments described are only some, but not all, of the embodiments of the present application. All other embodiments, which can be made by one of ordinary skill in the art without undue burden from the disclosure, are within the scope of the present application.
Referring to fig. 2, fig. 2 is a flowchart illustrating a code processing method according to an embodiment of the disclosure. The method of the present embodiment may be applied to a terminal device or a server, and the code processing method includes:
s201, determining the type of a code file to be processed, and acquiring a grammar tree of the code file through a target loader corresponding to the type of the code file.
In this embodiment, a plurality of code files may be included in an application, so when internationalization is performed on a code of an application, a code file to be processed that needs internationalization needs to be acquired first, where the code file to be processed may be all code files in a code folder or some specific code files in the code folder. In an alternative embodiment, a code folder to be processed (a code folder of an application program) is scanned, and a list of code files to be processed included in the code folder is obtained. For example, a Glob module can be used for scanning the code folder, wherein Glob is a module for searching file catalogues and files, is simple and has very few contents, and can be used for searching file path names conforming to specific rules, so that a list of to-be-processed code files contained in the code folder can be obtained by scanning the code folder through the Glob module, and the types of each to-be-processed code file can be obtained, for example, javaScript series, VUE, go, python and the like can be included according to the programming language division type of codes.
After determining the type of the code file to be processed, the grammar tree of the code file can be obtained through a target loader corresponding to the type of the code file, and the file to be translated can be extracted from the code file based on the grammar tree, so that the file to be translated can be conveniently converted into an internationalized file function.
In this embodiment, each programming language has a mode of converting its code language into a syntax tree, and the nodes have their positional relationship of nodes, and for the use of internationalization frameworks, most of the conversion from a document form to an internationalization document function, so according to this feature, a target loader corresponding to the programming language type of the code file can be used to obtain the syntax tree of the code file, the target loader can read the content of the code file, then convert the content into the syntax tree, and traverse the syntax tree in a different manner, and extract the document to be translated from the syntax tree. For example, for JavaScript-series code files, a JSLoader may be employed to extract a syntax tree in the form of a label (tag); for code files of the VUE language, a corresponding loader may be employed to extract a syntax tree in the form of a DOM (Document Object Model, document objectification model) tree; code files for the Go language; AST (Abstract Syntax Tree ) may be extracted with a corresponding loader; for code files in the Python language, a corresponding loader may be used to extract the syntax tree of ASTToken.
S202, extracting data units from the grammar tree according to a preset domain-specific language, wherein the data units comprise a to-be-translated version and attribute information.
In the above embodiment, the syntax tree of the code file and the extracted data unit are acquired, for example, as follows: for code files of JavaScript series, a JSLoader loader can be used for extracting a grammar tree in a label form, and a visitor mode is used for consuming the grammar tree to extract a data unit; for code files of the VUE language, a corresponding loader can be adopted to extract a grammar tree in the form of a DOM tree, and data units can be extracted from the grammar tree through a traversing (universal) process; and code files for the Go language; an AST grammar tree can be extracted by adopting a corresponding loader, and a data unit can be extracted from the grammar tree through a transsal process; for code files in the Python language, a corresponding loader may be used to extract the syntax tree of ASTToken, from which data units may be extracted by a trade procedure.
Optionally, in this embodiment, a standard domain-specific language (Domain Specific Language, DSL) may be pre-specified as a standard data extraction unit in combination with a specific identifier of an internationalized service, so that the target loader may extract, from the syntax tree, a data unit according to a preset DSL, where the data unit includes at least one to-be-translated version and attribute information of the to-be-translated version.
As an example, a code file to be processed is as follows:
the code file may obtain a syntax tree of the code file shown in fig. 3 through the target loader, and extract a data unit, that is, a DSL type data unit, from the syntax tree according to a preset domain-specific language DSL, where the to-be-translated version is a "home page component", and attribute information of the to-be-translated version is: astPath, type (defining the type of document), start and end (beginning and ending subscripts of source string), line and column (which are their actual line information), document state (e.g., processed state processed or unprocessed state unprocessed), etc. The attribute information may be directly extracted from the syntax tree, or some attribute information may be injected, where the injection may be divided into local injection and remote issuing injection, for example, one injection for remote issuing, where a document for remote issuing has some document states, such as translation just translated, test issue, on-line issue, etc., and may be written into a data unit, so as to obtain a target data unit.
On the basis of the above embodiment, optionally, after extracting the data unit from the syntax tree, the method further includes:
Generating a document identifier for the document to be translated, acquiring a translation storage path, and writing the document identifier and the translation storage path into the data unit.
In this embodiment, a document identifier may be generated for the document to be translated, which is used to uniquely identify the document to be translated, and also used to identify the translated document of the document to be translated, and may be indexed according to the document identifier. Specifically, a translation of a certain language of the document to be translated can be used as a document identifier, or a character string can be randomly generated to be used as the document identifier of the document to be translated. The embodiment may also pre-allocate a storage location of the translation, and write the corresponding storage path of the translation and the document identifier as attributes into each data unit.
S203, obtaining the translation of the target language corresponding to the to-be-translated version, and generating a target sentence corresponding to the to-be-translated version according to the translation and the attribute information.
In this embodiment, the document to be translated may be translated into a translation of a target language, machine translation or manual translation may be selected, optionally, one or more target languages may be selected, the translated translation may be stored, for example, in a file or in a database, and a storage path of the translation may be obtained. On the basis of the embodiment, if a translation storage path of a translation is allocated in advance, storing the translation according to the translation storage path; optionally, the translation may be associated with the document identifier, so that a corresponding translation may be obtained when the document identifier is retrieved, and based on this, index information of the translation may be obtained according to the translation storage path and the document identifier.
In this embodiment, a to-be-translated version generates a target sentence corresponding to the to-be-translated version according to the translation and the attribute information.
Optionally, for some annotation files to be translated, the translation of the file to be translated may be directly used as a target sentence, that is, the annotation file to be translated in the code file is directly replaced with the corresponding translation.
Optionally, for some other text to be translated in the code file, the target sentence corresponding to the text to be translated may be an internationalized text function corresponding to the text to be translated, that is, a function when the code runs, and optionally, may be an internationalized text function of I18n, where I18n (the source is the first and last character I and n of english word interaction, and 18 is the middle character number) is simply referred to as "internationalized", which refers to a process of unhooking software from a specific language and region in designing software, and when the software is transplanted to a different language and region, the software itself is not used as a change or modification on internal engineering. In this embodiment, the to-be-translated version generates a target sentence corresponding to the to-be-translated version according to the translation and the attribute information, that is, generates a function similar to that of I18n.t ("xx") for the to-be-translated version, where "xx" represents index information of the translation, and is used to find the corresponding translation according to the index information during operation.
Optionally, on the basis of the foregoing embodiment, the process of generating, according to the translation and the attribute information, the target sentence corresponding to the to-be-translated version may specifically include:
generating an internationalization file function according to the attribute information, file identification and translation storage path in the data unit, and taking the internationalization file function as a target sentence corresponding to the to-be-translated file, wherein the internationalization file function comprises index information of the translation and is used for acquiring the translation according to the index information of the translation when the internationalization file function runs.
In this embodiment, after the data unit is extracted, the target parameters required in the internationalization document function may be obtained from the attribute information based on the attribute information in the data unit, and the target parameters, the document identifier and the translation storage path may be synthesized according to the specified template of the internationalization document function, so as to obtain the final internationalization document function, for example, "i18n. By way of example, after the code file is subjected to I18n conversion, the following internationalized code file can be obtained:
The internationalization document function corresponding to the document to be translated "first page component" in "< p > first page component is" { I18n.t ('home_components', { }, 'first page component') } ", wherein" home_components "is the document identification of the document to be translated" first page component ", and" import { I18n } from '@ ies/starling_intel' "represents the storage path of the translation (stored in the Starling open source platform). When the internationalized code file is operated, the corresponding translation can be obtained according to the index information of the translation, namely, a translation storage position is found according to the storage path of the translation, and the translation corresponding to the to-be-translated version is searched according to the version identification in the translation storage position, so that the translation corresponding to the to-be-translated version is obtained.
However, for the annotation "// home page component", the translation itself may be used as a target sentence, i.e., "// home_components", so as to directly replace the annotation to-be-translated text in the code file with the corresponding translation.
S204, replacing the to-be-translated version in the code file with a target sentence corresponding to the to-be-translated version, and obtaining the internationalized code file.
In the embodiment, the code file to be translated is required to be replaced by a corresponding target sentence, so that an internationalized code file can be obtained, wherein the replacement process can be automatically realized, manual processing is not required, the internationalized code file can be obtained through the replacement process of the code file to be processed, and a 'home page component' in a 'p' home page component is replaced by a 'I18n.t (' home_components ',' home page component ')'; the// home page component is replaced with "// home_components" and so on, and will not be described in detail herein.
According to the code processing method provided by the embodiment, the type of the code file to be processed is determined, and the grammar tree of the code file is obtained through the target loader corresponding to the type of the code file; extracting a data unit from the grammar tree according to a preset domain-specific language, wherein the data unit comprises a to-be-translated version and attribute information; obtaining a translation of a target language corresponding to the to-be-translated version, and generating a target sentence corresponding to the to-be-translated version according to the translation and the attribute information; and replacing the file to be translated in the code file with a target sentence corresponding to the file to be translated, and obtaining the internationalized code file. According to the method, the target loader automatically acquires the text to be translated, automatically generates the corresponding target sentence, and further replaces the text in the code file, so that an automatic code internationalization process is realized, and international development efficiency improvement and quality guarantee of code project cross languages are improved.
In the above embodiment, when the to-be-translated text in the code file is replaced with the target sentence corresponding to the to-be-translated text in S204, because the positions of the to-be-translated text are different, the formats are different, and the like, different strategies are required to be adopted to replace the to-be-translated text in the code file with the corresponding target sentence. Specifically, the location of the document to be translated is different, and the different formats and other factors can be different in the type of the data unit. Therefore, as shown in fig. 4, S204 may replace the to-be-translated version in the code file with the target sentence corresponding to the to-be-translated version, which may specifically include:
S301, determining the type of the data unit, and acquiring a preset restoration strategy corresponding to the type of the data unit;
s302, replacing the to-be-translated version in the code file with a target sentence corresponding to the to-be-translated version according to the preset restoration strategy.
In this embodiment, the type of the data unit may be determined according to the initial format or the positional relationship of the document to be translated, for example, in the case of determining the type of the data unit according to the initial format of the document to be translated, if the document to be translated is in a bracket, a specific format needs to be adopted for the corresponding internationalized document function, for example, a special character such as "$", "'", "/", etc., and/or a bracket of a specific form is added, so that at this time, the preset restoration policy includes the target format of the internationalized document function corresponding to the initial format of the document to be translated, and then the internationalized document function may be adjusted according to the target format of the internationalized document function during replacement, and then the document to be translated in the code file may be replaced by the adjusted internationalized document function.
On the basis of the above embodiment, as an example, the general data unit extracted from the syntax tree is generally shown in fig. 5a, while the DSL type data unit extracted from the syntax tree in this embodiment is shown in fig. 5b, and the internationalized text function in fig. 5b may be generated according to the DSL type data unit, and the specific principle may be referred to the above embodiment, which is not repeated herein.
On the basis of the above embodiments, the critical flow may also be pipelined.
Specifically, the code processing method under the general flow is as follows, the code is scanned= > to generate a document identifier= > (uploading document+document translation) = > to release the document= > to replace the code, some differences may be generated in different business flows, if a person needs to be translated, all operations in the mode are split, the operation has a concept, and a certain upper threshold exists. Therefore, in this embodiment, the flow may be generalized and pipelined to form a pipeline, and a series of key nodes are set by using the principle of the packing tool, namely, the weback (table flow management tool), and each node may be combined with different service scenarios, so that the lifecycle may be connected in series. As shown in fig. 6a, a global instance is transmitted for each parameter of the Hooks (hook), and all nodes can consume the parameter, so that the extensibility of the scheme is greatly improved. The pipeline of the code processing method flow is shown in fig. 6 b.
The setup phase (setup) completes various preamble data configurations, for example: detecting configuration, initializing a Hooks & progress bar, initializing a file connector, initializing a life cycle operation stage execution purpose, initializing a life cycle output stage execution purpose, initializing a loader (Loaders), and scanning and collecting files, initializing plug-ins (plug ins) and the like involved in the flow according to grammar rules in the loader;
And the operation stage (run) is used for completing various scanned Code file extraction and replacement operations, including a pre-scanning processing operation, a scanning operation, a text mark generation, a post-scanning processing operation, a pre-replacement processing operation, a replacement operation, a target sentence (Code) generation, a post-replacement processing operation and the like. The scanning operation can return the scanned and read file content to corresponding DSL data according to the parser function in the loader object of the scanning source; generating a document identification operation, namely generating a key value for a document to be translated in a scanning result through a dynamically configured sequence policy; the replacement operation, if scanning annotation data exist, the notes and the release cases are collected and translated in a centralized way, in addition, the replacement operation (default) and the updating operation are distinguished, if the replacement operation is performed, the unprocessed cases are subjected to replacement processing, and if the replacement operation is performed, the processed cases are updated according to new and old items in the starling configuration according to the currently used key value; and generating target sentences, namely generating the scanned result data by using different generating strategies according to different types of type identifiers, and then performing replacement operation according to the position information in the data.
Output stage (Emit), which can process parse & replace yield data.
Corresponding to the code processing method of the above embodiment, fig. 7 is a block diagram of the structure of the code processing apparatus provided by the embodiment of the present disclosure. For ease of illustration, only portions relevant to embodiments of the present disclosure are shown. Referring to fig. 7, the code processing apparatus 700 includes: a loading unit 701, an extracting unit 702, a translating unit 703, and a restoring unit 704.
The loading unit 701 is configured to determine a type of a code file to be processed, and obtain, by using a target loader corresponding to the type of the code file, a syntax tree of the code file;
an extracting unit 702, configured to extract a data unit from the syntax tree according to a preset domain-specific language, where the data unit includes a to-be-translated version and attribute information;
the translation unit 703 is configured to obtain a translation of a target language corresponding to a to-be-translated document, and generate a target sentence corresponding to the to-be-translated document according to the translation and the attribute information;
and a restoring unit 704, configured to replace a to-be-translated version in the code file with a target sentence corresponding to the to-be-translated version, thereby obtaining an internationalized code file.
In accordance with one or more embodiments of the present disclosure, the loading unit 701 is further configured to, after extracting a data unit from the syntax tree:
generating a document identifier for the document to be translated, acquiring a translation storage path, and writing the document identifier and the translation storage path into the data unit;
the translation unit 703 is further configured to, after obtaining a translation of a target language corresponding to the document to be translated:
and storing the translation according to the translation storage path and correlating with the document identification.
According to one or more embodiments of the present disclosure, when generating the target sentence corresponding to the to-be-translated version according to the translated version and the attribute information, the translation unit 703 is configured to:
generating an internationalization file function according to the attribute information, file identification and translation storage path in the data unit, and taking the internationalization file function as a target sentence corresponding to the to-be-translated file, wherein the internationalization file function comprises index information of the translation and is used for acquiring the translation according to the index information of the translation when the internationalization file function runs.
According to one or more embodiments of the present disclosure, when replacing a to-be-translated version in the code file with a target sentence corresponding to the to-be-translated version, the restoring unit 704 is configured to:
Determining the type of the data unit, and acquiring a preset restoration strategy corresponding to the type of the data unit;
and replacing the to-be-translated version in the code file with a target sentence corresponding to the to-be-translated version according to the preset restoration strategy.
In accordance with one or more embodiments of the present disclosure, the restoration unit 704 is configured, when determining the type of the data unit, to:
and acquiring an initial format of the to-be-translated text in the data unit, and determining the type of the data unit according to the initial format of the to-be-translated text.
According to one or more embodiments of the present disclosure, the preset restoration policy includes a target format of an internationalized document function corresponding to an initial format of the document to be translated;
the restoring unit 704 is configured to, when replacing a to-be-translated version in the code file with a target sentence corresponding to the to-be-translated version according to the preset restoring policy:
and replacing the to-be-translated version in the code file with the internationalized file function according to the target format of the internationalized file function.
According to one or more embodiments of the present disclosure, the apparatus further includes a scanning unit configured to scan a code folder to be processed before determining a type of the code file to be processed, and obtain a list of code files to be processed included in the code folder.
The code processing apparatus provided in this embodiment may be used to execute the technical solution of the foregoing method embodiment, and its implementation principle and technical effects are similar, and this embodiment will not be described herein again.
Referring to fig. 8, there is shown a schematic structural diagram of an electronic device 800 suitable for use in implementing embodiments of the present disclosure, which electronic device 800 may be a terminal device or a server. The terminal device may include, but is not limited to, a mobile terminal such as a mobile phone, a notebook computer, a digital broadcast receiver, a personal digital assistant (Personal Digital Assistant, PDA for short), a tablet (Portable Android Device, PAD for short), a portable multimedia player (Portable Media Player, PMP for short), an in-vehicle terminal (e.g., an in-vehicle navigation terminal), and the like, and a fixed terminal such as a digital TV, a desktop computer, and the like. The electronic device shown in fig. 8 is merely an example and should not be construed to limit the functionality and scope of use of the disclosed embodiments.
As shown in fig. 8, the electronic device 800 may include a processing means (e.g., a central processor, a graphics processor, etc.) 801 that may perform various appropriate actions and processes according to a program stored in a Read Only Memory (ROM) 802 or a program loaded from a storage 808 into a random access Memory (Random Access Memory, RAM) 803. In the RAM 803, various programs and data required for the operation of the electronic device 800 are also stored. The processing device 801, the ROM 802, and the RAM 803 are connected to each other by a bus 804. An input/output (I/O) interface 805 is also connected to the bus 804.
In general, the following devices may be connected to the I/O interface 805: input devices 806 including, for example, a touch screen, touchpad, keyboard, mouse, camera, microphone, accelerometer, gyroscope, and the like; an output device 807 including, for example, a liquid crystal display (Liquid Crystal Display, LCD for short), a speaker, a vibrator, and the like; storage 808 including, for example, magnetic tape, hard disk, etc.; communication means 809. The communication means 809 may allow the electronic device 800 to communicate wirelessly or by wire with other devices to exchange data. While fig. 8 shows an electronic device 800 having various means, it is to be understood that not all of the illustrated means are required to be implemented or provided. More or fewer devices may be implemented or provided instead.
In particular, according to embodiments of the present disclosure, the processes described above with reference to flowcharts may be implemented as computer software programs. For example, embodiments of the present disclosure include a computer program product comprising a computer program embodied on a computer readable medium, the computer program comprising program code for performing the method shown in the flowcharts. In such an embodiment, the computer program may be downloaded and installed from a network via communication device 809, or installed from storage device 808, or installed from ROM 802. The above-described functions defined in the methods of the embodiments of the present disclosure are performed when the computer program is executed by the processing device 801.
It should be noted that the computer readable medium described in the present disclosure may be a computer readable signal medium or a computer readable storage medium, or any combination of the two. The computer readable storage medium can be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or a combination of any of the foregoing. More specific examples of the computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the context of this disclosure, a computer-readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In the present disclosure, however, the computer-readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, with the computer-readable program code embodied therein. Such a propagated data signal may take any of a variety of forms, including, but not limited to, electro-magnetic, optical, or any suitable combination of the foregoing. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: electrical wires, fiber optic cables, RF (radio frequency), and the like, or any suitable combination of the foregoing.
The computer readable medium may be contained in the electronic device; or may exist alone without being incorporated into the electronic device.
The computer-readable medium carries one or more programs which, when executed by the electronic device, cause the electronic device to perform the methods shown in the above-described embodiments.
Computer program code for carrying out operations of the present disclosure may be written in one or more programming languages, including an object oriented programming language such as Java, smalltalk, C ++ and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code may execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any kind of network, including a local area network (Local Area Network, LAN for short) or a wide area network (Wide Area Network, WAN for short), or it may be connected to an external computer (e.g., connected via the internet using an internet service provider).
The flowcharts and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present disclosure. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams and/or flowchart illustration, and combinations of blocks in the block diagrams and/or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The units involved in the embodiments of the present disclosure may be implemented by means of software, or may be implemented by means of hardware. The name of the unit does not in any way constitute a limitation of the unit itself, for example the first acquisition unit may also be described as "unit acquiring at least two internet protocol addresses".
The functions described above herein may be performed, at least in part, by one or more hardware logic components. For example, without limitation, exemplary types of hardware logic components that may be used include: a Field Programmable Gate Array (FPGA), an Application Specific Integrated Circuit (ASIC), an Application Specific Standard Product (ASSP), a system on a chip (SOC), a Complex Programmable Logic Device (CPLD), and the like.
In the context of this disclosure, a machine-readable medium may be a tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. The machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. The machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any suitable combination of the foregoing. More specific examples of a machine-readable storage medium would include an electrical connection based on one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
According to a first aspect, according to one or more embodiments of the present disclosure, there is provided a code processing method, including:
determining the type of a code file to be processed, and acquiring a grammar tree of the code file through a target loader corresponding to the type of the code file;
extracting a data unit from the grammar tree according to a preset domain-specific language, wherein the data unit comprises a to-be-translated version and attribute information;
obtaining a translation of a target language corresponding to a to-be-translated version, and generating a target sentence corresponding to the to-be-translated version according to the translation and the attribute information;
and replacing the to-be-translated version in the code file with a target sentence corresponding to the to-be-translated version to obtain an internationalized code file.
According to one or more embodiments of the present disclosure, after the extracting the data unit from the syntax tree, the method further includes:
generating a document identifier for the document to be translated, acquiring a translation storage path, and writing the document identifier and the translation storage path into the data unit;
after the translated version of the target language corresponding to the to-be-translated version is obtained, the method further comprises the following steps:
and storing the translation according to the translation storage path and correlating with the document identification.
According to one or more embodiments of the present disclosure, the generating, according to the translation and the attribute information, a target sentence corresponding to the to-be-translated version includes:
generating an internationalization file function according to the attribute information, file identification and translation storage path in the data unit, and taking the internationalization file function as a target sentence corresponding to the to-be-translated file, wherein the internationalization file function comprises index information of the translation and is used for acquiring the translation according to the index information of the translation when the internationalization file function runs.
According to one or more embodiments of the present disclosure, the generating an internationalized document function according to the attribute information, the document identification, and the translation storage path in the data unit includes:
acquiring target parameters required in the internationalization file function from the attribute information;
and synthesizing the target parameters, the document identification and the translation storage path into the internationalized document function according to a preset template.
According to one or more embodiments of the present disclosure, the replacing the to-be-translated version in the code file with the target sentence corresponding to the to-be-translated version includes:
Determining the type of the data unit, and acquiring a preset restoration strategy corresponding to the type of the data unit;
and replacing the to-be-translated version in the code file with a target sentence corresponding to the to-be-translated version according to the preset restoration strategy.
According to one or more embodiments of the present disclosure, the determining the type of the data unit includes:
and acquiring an initial format of the to-be-translated text in the data unit, and determining the type of the data unit according to the initial format of the to-be-translated text.
According to one or more embodiments of the present disclosure, the preset restoration policy includes a target format of an internationalized document function corresponding to an initial format of the document to be translated;
the replacing the to-be-translated version in the code file with the target sentence corresponding to the to-be-translated version according to the preset restoration strategy includes:
and replacing the to-be-translated version in the code file with the internationalized file function according to the target format of the internationalized file function.
According to one or more embodiments of the present disclosure, before determining the type of the code file to be processed, the method further includes:
scanning a code folder to be processed to obtain a code file list to be processed, wherein the code file list to be processed is included in the code folder.
In a second aspect, according to one or more embodiments of the present disclosure, there is provided a code processing apparatus including:
the loading unit is used for determining the type of the code file to be processed and acquiring a grammar tree of the code file through a target loader corresponding to the type of the code file;
the extraction unit is used for extracting data units from the grammar tree according to a preset domain-specific language, wherein the data units comprise a to-be-translated version and attribute information;
the translation unit is used for obtaining the translation of the target language corresponding to the to-be-translated version and generating a target sentence corresponding to the to-be-translated version according to the translation and the attribute information;
and the restoring unit is used for replacing the to-be-translated version in the code file with the target statement corresponding to the to-be-translated version to obtain the internationalized code file.
According to one or more embodiments of the present disclosure, the loading unit, after extracting the data unit from the syntax tree, is further configured to:
generating a document identifier for the document to be translated, acquiring a translation storage path, and writing the document identifier and the translation storage path into the data unit;
The translation unit is further configured to, after obtaining a translation of a target language corresponding to the document to be translated:
and storing the translation according to the translation storage path and correlating with the document identification.
According to one or more embodiments of the present disclosure, when generating a target sentence corresponding to the to-be-translated version according to the translated version and the attribute information, the translation unit is configured to:
generating an internationalization file function according to the attribute information, file identification and translation storage path in the data unit, and taking the internationalization file function as a target sentence corresponding to the to-be-translated file, wherein the internationalization file function comprises index information of the translation and is used for acquiring the translation according to the index information of the translation when the internationalization file function runs.
According to one or more embodiments of the present disclosure, the translation unit is configured, when generating an internationalized document function according to the attribute information, the document identifier, and the translation storage path in the data unit, to:
acquiring target parameters required in the internationalization file function from the attribute information;
and synthesizing the target parameters, the document identification and the translation storage path into the internationalized document function according to a preset template.
According to one or more embodiments of the present disclosure, when replacing a to-be-translated version in the code file with a target sentence corresponding to the to-be-translated version, the restoring unit is configured to:
determining the type of the data unit, and acquiring a preset restoration strategy corresponding to the type of the data unit;
and replacing the to-be-translated version in the code file with a target sentence corresponding to the to-be-translated version according to the preset restoration strategy.
According to one or more embodiments of the present disclosure, the restoration unit, when determining the type of the data unit, is configured to:
and acquiring an initial format of the to-be-translated text in the data unit, and determining the type of the data unit according to the initial format of the to-be-translated text.
According to one or more embodiments of the present disclosure, the preset restoration policy includes a target format of an internationalized document function corresponding to an initial format of the document to be translated;
the restoring unit is configured to, when replacing a to-be-translated version in the code file with a target sentence corresponding to the to-be-translated version according to the preset restoring policy:
and replacing the to-be-translated version in the code file with the internationalized file function according to the target format of the internationalized file function.
According to one or more embodiments of the present disclosure, the apparatus further includes a scanning unit configured to scan a code folder to be processed before determining a type of the code file to be processed, and obtain a list of code files to be processed included in the code folder.
In a third aspect, according to one or more embodiments of the present disclosure, there is provided an electronic device comprising: at least one processor and memory;
the memory stores computer-executable instructions;
the at least one processor executes computer-executable instructions stored in the memory such that the at least one processor performs the code processing method as described above in the first aspect and the various possible designs of the first aspect.
In a fourth aspect, according to one or more embodiments of the present disclosure, there is provided a computer-readable storage medium having stored therein computer-executable instructions which, when executed by a processor, implement the code processing method as described above in the first aspect and the various possible designs of the first aspect.
In a fifth aspect, according to one or more embodiments of the present disclosure, there is provided a computer program product comprising computer-executable instructions which, when executed by a processor, implement the code processing method as described above in the first aspect and in the various possible designs of the first aspect.
The foregoing description is only of the preferred embodiments of the present disclosure and description of the principles of the technology being employed. It will be appreciated by persons skilled in the art that the scope of the disclosure referred to in this disclosure is not limited to the specific combinations of features described above, but also covers other embodiments which may be formed by any combination of features described above or equivalents thereof without departing from the spirit of the disclosure. Such as those described above, are mutually substituted with the technical features having similar functions disclosed in the present disclosure (but not limited thereto).
Moreover, although operations are depicted in a particular order, this should not be understood as requiring that such operations be performed in the particular order shown or in sequential order. In certain circumstances, multitasking and parallel processing may be advantageous. Likewise, while several specific implementation details are included in the above discussion, these should not be construed as limiting the scope of the present disclosure. Certain features that are described in the context of separate embodiments can also be implemented in combination in a single embodiment. Conversely, various features that are described in the context of a single embodiment can also be implemented in multiple embodiments separately or in any suitable subcombination.
Although the subject matter has been described in language specific to structural features and/or methodological acts, it is to be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or acts described above. Rather, the specific features and acts described above are example forms of implementing the claims.

Claims (11)

1. A code processing method, comprising:
determining the type of a code file to be processed, and acquiring a grammar tree of the code file through a target loader corresponding to the type of the code file;
extracting a data unit from the grammar tree according to a preset domain-specific language, wherein the data unit comprises a to-be-translated version and attribute information;
obtaining a translation of a target language corresponding to a to-be-translated version, and generating a target sentence corresponding to the to-be-translated version according to the translation and the attribute information;
and replacing the to-be-translated version in the code file with a target sentence corresponding to the to-be-translated version to obtain an internationalized code file.
2. The method of claim 1, further comprising, after said extracting data units from said syntax tree:
Generating a document identifier for the document to be translated, acquiring a translation storage path, and writing the document identifier and the translation storage path into the data unit;
after the translated version of the target language corresponding to the to-be-translated version is obtained, the method further comprises the following steps:
and storing the translation according to the translation storage path and correlating with the document identification.
3. The method according to claim 2, wherein the generating the target sentence corresponding to the translation to be translated according to the translation and the attribute information includes:
generating an internationalization file function according to the attribute information, file identification and translation storage path in the data unit, and taking the internationalization file function as a target sentence corresponding to the to-be-translated file, wherein the internationalization file function comprises index information of the translation and is used for acquiring the translation according to the index information of the translation when the internationalization file function runs.
4. A method according to claim 3, wherein generating an internationalized document function from the attribute information, document identification and translation storage path in the data unit comprises:
Acquiring target parameters required in the internationalization file function from the attribute information;
and synthesizing the target parameters, the document identification and the translation storage path into the internationalized document function according to a preset template.
5. The method according to any one of claims 1-4, wherein replacing the to-be-translated version in the code file with the target sentence corresponding to the to-be-translated version includes:
determining the type of the data unit, and acquiring a preset restoration strategy corresponding to the type of the data unit;
and replacing the to-be-translated version in the code file with a target sentence corresponding to the to-be-translated version according to the preset restoration strategy.
6. The method of claim 5, wherein said determining the type of the data unit comprises:
and acquiring an initial format of the to-be-translated text in the data unit, and determining the type of the data unit according to the initial format of the to-be-translated text.
7. The method of claim 6, wherein the preset restore policy includes a target format of an internationalized document function corresponding to the initial format of the document to be translated;
The replacing the to-be-translated version in the code file with the target sentence corresponding to the to-be-translated version according to the preset restoration strategy includes:
and replacing the to-be-translated version in the code file with the internationalized file function according to the target format of the internationalized file function.
8. A code processing apparatus, comprising:
the loading unit is used for determining the type of the code file to be processed and acquiring a grammar tree of the code file through a target loader corresponding to the type of the code file;
the extraction unit is used for extracting data units from the grammar tree according to a preset domain-specific language, wherein the data units comprise a to-be-translated version and attribute information;
the translation unit is used for obtaining the translation of the target language corresponding to the to-be-translated version and generating a target sentence corresponding to the to-be-translated version according to the translation and the attribute information;
and the restoring unit is used for replacing the to-be-translated version in the code file with the target statement corresponding to the to-be-translated version to obtain the internationalized code file.
9. An electronic device, comprising: at least one processor and memory;
The memory stores computer-executable instructions;
the at least one processor executing computer-executable instructions stored in the memory causes the at least one processor to perform the method of any one of claims 1-7.
10. A computer readable storage medium having stored therein computer executable instructions which when executed by a processor implement the method of any of claims 1-7.
11. A computer program product comprising computer-executable instructions which, when executed by a processor, implement the method of any of claims 1-7.
CN202210078642.8A 2022-01-24 2022-01-24 Code processing method, apparatus, storage medium, and program product Pending CN116521169A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210078642.8A CN116521169A (en) 2022-01-24 2022-01-24 Code processing method, apparatus, storage medium, and program product

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210078642.8A CN116521169A (en) 2022-01-24 2022-01-24 Code processing method, apparatus, storage medium, and program product

Publications (1)

Publication Number Publication Date
CN116521169A true CN116521169A (en) 2023-08-01

Family

ID=87403428

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210078642.8A Pending CN116521169A (en) 2022-01-24 2022-01-24 Code processing method, apparatus, storage medium, and program product

Country Status (1)

Country Link
CN (1) CN116521169A (en)

Similar Documents

Publication Publication Date Title
CN108139891B (en) Method and system for generating suggestions to correct undefined token errors
CN108549538B (en) Code detection method and device, storage medium and test terminal
CN111736840A (en) Compiling method and running method of applet, storage medium and electronic equipment
WO2021253641A1 (en) Shading language translation method
US11029924B2 (en) Program optimization by converting code portions to directly reference internal data representations
CN109947431B (en) Code generation method, device, equipment and storage medium
CN112783482B (en) Visual form generation method, device, equipment and storage medium
US20230088670A1 (en) Reverse compiler
CN111124541A (en) Configuration file generation method, device, equipment and medium
CN111125216A (en) Method and device for importing data into Phoenix
CN107729347B (en) Method, device and equipment for acquiring synonym label and computer readable storage medium
CN116521169A (en) Code processing method, apparatus, storage medium, and program product
CN111273913B (en) Method and device for outputting application program interface data represented by specifications
CN115129598A (en) Risk detection method, device, system and medium for SQL (structured query language) statements
CN111539200B (en) Method, device, medium and electronic equipment for generating rich text
CN113050987A (en) Interface document generation method and device, storage medium and electronic equipment
US9720660B2 (en) Binary interface instrumentation
CN116010445B (en) Processing method and device of database SQL script
CN114896273B (en) SQL statement configuration method and equipment compatible with storage process and storage medium
CN114416035A (en) Data processing method and device, electronic equipment and storage medium
CN113326231A (en) Picture resource processing method and device
CN113157793A (en) ETL file splitting method and device
CN113448923A (en) File generation method and device and terminal
CN117648299A (en) Label acquisition method and device, electronic equipment and storage medium
CN117407002A (en) Transcoding method, transcoding device, computer equipment and storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination