CN113076167A - Code processing method and related equipment - Google Patents

Code processing method and related equipment Download PDF

Info

Publication number
CN113076167A
CN113076167A CN202110431775.4A CN202110431775A CN113076167A CN 113076167 A CN113076167 A CN 113076167A CN 202110431775 A CN202110431775 A CN 202110431775A CN 113076167 A CN113076167 A CN 113076167A
Authority
CN
China
Prior art keywords
target node
file
source code
index value
specific language
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
CN202110431775.4A
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.)
Hangzhou Tuya Information Technology Co Ltd
Original Assignee
Hangzhou Tuya Information 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 Hangzhou Tuya Information Technology Co Ltd filed Critical Hangzhou Tuya Information Technology Co Ltd
Priority to CN202110431775.4A priority Critical patent/CN113076167A/en
Publication of CN113076167A publication Critical patent/CN113076167A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/44Arrangements for executing specific programs
    • G06F9/451Execution arrangements for user interfaces
    • G06F9/454Multi-language systems; Localisation; Internationalisation
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/70Software maintenance or management
    • G06F8/73Program documentation

Abstract

The embodiment of the application discloses a code processing method which is used for the internationalization processing process of a front-end project. The embodiment of the method comprises the following steps: obtaining an abstract syntax tree representation of a source code; determining a target node comprising a specific language pattern in an abstract syntax tree, and acquiring the position information of the target node and the specific language pattern value included by the target node; generating an index value corresponding to the specific language case value based on a preset rule; replacing a specific language pattern value included in the source code with an index value based on the location information of the target node; and writing the index value and the language-specific pattern value into the translation resource file. According to the technical scheme, the embodiment of the application has the following advantages: according to the scheme, the abstract syntax tree processing is carried out on the source code, the source code which does not comprise the specific language pattern is obtained through index value replacement, and therefore the front-end worker avoids tedious retrieval and index value setting work, and the efficiency of the code internationalization processing process is improved.

Description

Code processing method and related equipment
Technical Field
The present application relates to the field of data processing, and in particular, to a code processing method and related device
Background
With the advancement of science and technology, the same software and web page can be used by users in different countries and regions, and the types of languages used by users in different countries and regions are generally different, so that different language support schemes are required to be used during front-end display, and a front-end developer of the software or web page often uses a specific language to write document contents in the development process and needs to perform subsequent support processing so as to meet the requirements of users in different countries and regions.
At present, for the support work of multiple languages, it is necessary to configure an international support environment in the development stage, and compare representative modes such as I18n (the first and last characters I and n of the international word, 18 is the number of middle characters) L10n (the first and last characters L and n of the Localization of the english word, 10 is the number of middle characters), and the like. For example, if the web page needs to support chinese, english, german, and french, four internationalized resource files need to be generated in the development stage for the source code including the "hello" document, and each resource file needs to contain a common document index value and different translation documents. If the index value is set to tuya, in the Chinese resource file, the case corresponding to the index "tuya" is "hello" and the case corresponding to the index "tuya" in the English resource file is "hello". When the webpage is rendered or the software runs, the internationalized resource file of the corresponding language is found according to the language environment selected by the user, and the corresponding translation file is taken out for display according to the file index value in the source code. This is the principle of current page multilingual implementations.
In the development stage of source code, in order to raise efficiency, the Chinese language case can be directly written in the source code, after the development of source code is basically completed, the Chinese language cases of all interfaces can be uniformly and manually searched, the index value of the case can be set, and the case can be translated into international resource files of different languages. When the website/software is large in scale, the files with different contents can reach thousands or even hundreds of thousands. The process of manually generating internationalized resource files often takes a lot of time and is prone to errors.
Disclosure of Invention
A first aspect of an embodiment of the present application provides a code processing method, including:
obtaining an abstract syntax tree representation of a source code;
determining a target node comprising a specific language pattern in the abstract syntax tree, and acquiring the position information of the target node and the specific language pattern value included by the target node;
generating an index value corresponding to the specific language case value based on a preset rule;
replacing a specific language pattern value included in the source code with the index value based on the location information of the target node;
and writing the index value and the language-specific pattern value into a translation resource file.
Based on the code processing method provided in the first aspect of the embodiment of the present application, optionally, the determining a target node including a language-specific pattern in the abstract syntax tree, and then the method further includes:
acquiring grammatical information corresponding to the specific language pattern value based on the target node;
determining an objective function corresponding to the syntax information, wherein the objective function is used for enabling the index value to normally run in the source code;
the replacing, by the index value, a particular language-pattern value included in the source code based on the location information of the target node includes: replacing a specific language pattern value included in the source code with the objective function and the index value based on the location information of the target node.
Based on the code processing method provided in the first aspect of the embodiment of the present application, optionally, the obtaining, based on the target node, syntax information corresponding to the specific language pattern value includes:
and acquiring grammatical information of the specific language case based on the target node and the node associated with the target node.
Based on the code processing method provided in the first aspect of the embodiment of the present application, optionally, the method further includes, before obtaining the abstract syntax tree representation of the source code:
acquiring a path of a file to be scanned;
and obtaining the source codes of all files in the path of the file to be scanned.
Based on the code processing method provided in the first aspect of the embodiment of the present application, optionally, the translation resource file is a json format file;
the method further comprises the following steps:
converting the translation resource file into a preset format file;
filling a translation file corresponding to the language-specific file in the preset format file to obtain a complete translation resource file;
and converting the complete translation resource file into a json format file.
Based on the code processing method provided in the first aspect of the embodiment of the present application, optionally, the generating an index value corresponding to the specific language case value based on a preset rule includes:
and generating a unique random value, and taking the unique random value as an index value corresponding to the specific language pattern value.
Based on the code processing method provided in the first aspect of the embodiment of the present application, optionally, the source code is a source code included in an Vue file and/or a source code included in a JavaScript file.
Based on the code processing method provided in the first aspect of the embodiment of the present application, optionally, the method is implemented by a command line tool.
A second aspect of embodiments of the present application provides a code processing apparatus, including:
the device comprises an acquisition unit, a processing unit and a control unit, wherein the acquisition unit is used for acquiring abstract syntax tree expression of a source code;
a determining unit, configured to determine a target node including a language-specific pattern in the abstract syntax tree, and obtain position information of the target node and a language-specific pattern value included in the target node;
the generating unit is used for generating an index value corresponding to the specific language pattern value based on a preset rule;
a replacing unit configured to replace a specific language pattern value included in the source code with the index value based on the location information of the target node;
and the writing unit is used for writing the index value and the specific language pattern value into a translation resource file.
Based on the code processing apparatus provided in the second aspect of the embodiment of the present application, optionally, the apparatus further includes:
the target function determining unit is used for acquiring grammar information corresponding to the specific language pattern value based on the target node;
determining an objective function corresponding to the syntax information, wherein the objective function is used for enabling the index value to normally run in the source code;
the replacement unit is specifically configured to: replacing a specific language pattern value included in the source code with the objective function and the index value based on the location information of the target node.
Based on the code processing apparatus provided in the second aspect of the embodiment of the present application, optionally, the objective function determining unit is specifically configured to:
acquiring grammatical information of the specific language case based on the target node and a node associated with the target node;
and determining an objective function corresponding to the grammar information.
Based on the code processing apparatus provided in the second aspect of the embodiment of the present application, optionally, the apparatus further includes:
the source code acquisition unit is used for acquiring a path of a file to be scanned;
and obtaining the source codes of all files in the path of the file to be scanned.
Based on the code processing apparatus provided in the second aspect of the embodiment of the present application, optionally, the translation resource file is a json format file, and the apparatus further includes:
the translation unit is used for converting the translation resource file into a preset format file;
filling a translation file corresponding to the language-specific file in the preset format file to obtain a complete translation resource file;
and converting the complete translation resource file into a json format file.
Based on the code processing apparatus provided in the second aspect of the embodiment of the present application, optionally, the generating unit is specifically configured to:
and generating a unique random value, and taking the unique random value as an index value corresponding to the specific language pattern value.
Based on the code processing apparatus provided in the second aspect of the embodiment of the present application, optionally, the source code is a source code included in an Vue file or a source code included in a JavaScript file.
A third aspect of embodiments of the present application provides a computer device, including:
the system comprises a central processing unit, a memory, an input/output interface, a wired or wireless network interface and a power supply;
the memory is a transient memory or a persistent memory;
the central processing unit is configured to communicate with the memory, and the instructions in the memory are executed on the computer device to perform the method according to any one of the first aspect of the embodiments of the present application.
A fourth aspect of embodiments of the present application provides a computer-readable storage medium, including instructions, which, when executed on a computer, cause the computer to perform the method according to any one of the first aspects of embodiments of the present application.
A fifth aspect of embodiments of the present application provides a computer program product containing instructions, which when executed on a computer, cause the computer to perform the method according to any one of the first aspect of embodiments of the present application.
According to the technical scheme, the embodiment of the application has the following advantages: according to the scheme, the abstract syntax tree processing is carried out on a source code, the abstract syntax tree corresponding to the source code is obtained, the abstract syntax tree is retrieved, a target node comprising a specific language pattern in the abstract syntax tree is determined, relevant information of the target node is extracted, an index value is generated based on a preset rule to replace the specific language pattern value included in the source code, the source code not comprising the specific language pattern is obtained, and meanwhile the index value and the specific language pattern value are written into a translation resource file. And furthermore, the front-end worker avoids the complex retrieval and index value setting work and improves the efficiency of the code internationalization processing process.
Drawings
In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings needed to be used in the description of the embodiments or the prior art will be briefly introduced below, it is obvious that the drawings in the following description are only embodiments of the present application, and for those skilled in the art, other drawings can be obtained according to the provided drawings without creative efforts.
FIG. 1 is a schematic flow chart diagram illustrating an embodiment of a code processing method according to the present application;
FIG. 2 is another schematic flow chart diagram illustrating an embodiment of a code processing method of the present application;
FIG. 3 is a schematic diagram of the processing procedure of the code processing method of the present application;
FIG. 4 is another schematic diagram of the processing procedure of the code processing method of the present application;
FIG. 5 is another schematic diagram of the processing procedure of the code processing method of the present application;
FIG. 6 is a schematic diagram of a path of a translation process of the code processing method of the present application;
FIG. 7 is a schematic diagram of the structure of the code processing apparatus of the present application;
FIG. 8 is a schematic view of another embodiment of the code processing apparatus of the present application;
fig. 9 is another schematic structural diagram of the code processing apparatus of the present application.
Detailed Description
In order to make the objects, technical solutions and advantages of the present application more apparent, the present application is described in further detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the present application and are not intended to limit the present application. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
The terms "first," "second," "third," "fourth," and the like in the description and in the claims of the present application and in the drawings described above, if any, are used for distinguishing between similar elements and not necessarily for describing a particular sequential or chronological order. It will be appreciated that the data so used may be interchanged under appropriate circumstances such that the embodiments described herein may be practiced otherwise than as specifically illustrated or described herein. Furthermore, the terms "comprises," "comprising," and "having," and any variations thereof, are intended to cover a non-exclusive inclusion, such that a process, method, system, article, or apparatus that comprises a list of steps or elements is not necessarily limited to those steps or elements expressly listed, but may include other steps or elements not expressly listed or inherent to such process, method, article, or apparatus.
It should be noted that the descriptions in this application referring to "first", "second", etc. are for descriptive purposes only and are not to be construed as indicating or implying relative importance or implicitly indicating the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include at least one such feature. In addition, technical solutions between various embodiments may be combined with each other, but must be realized by a person skilled in the art, and when the technical solutions are contradictory or cannot be realized, such a combination should not be considered to exist, and is not within the protection scope of the present application.
At present, the support work for multiple languages needs to configure an international support environment in the development stage, and among the Vue projects, the highest approval is Vue I18 n. Within the industry, a popular solution for the Vue2.x project multi-language is manual processing, whereby the front-end developer names the key value, replacing the script in the code. The manual approach is only suitable for small items, and when the item is large, its time cost is large. Meanwhile, the development process of the system requires programmers to process multiple languages, which can reduce the service development efficiency. The time cost is high.
In the development stage of source code, in order to raise efficiency, the Chinese language case can be directly written in the source code, after the development of source code is basically completed, the Chinese language cases of all interfaces can be uniformly and manually searched, the index value of the case can be extracted, and the case can be translated into international resource files of different languages. When the website/software is large in scale, the files with different contents can reach thousands or even hundreds of thousands. The process of manually generating internationalized resource files often takes a lot of time and is prone to errors.
In view of the above problem, the present application provides a code processing method for internationalized processing of a source code, referring to fig. 1, an embodiment of the code processing method of the present application includes: step 101-step 105.
101. An abstract syntax tree representation of source code is obtained.
And acquiring Abstract Syntax Tree expression (Abstract Syntax Tree) of the source code needing international processing. An abstract syntax tree representation is an abstract representation of the source code syntax structure. The syntax structure of the programming language is expressed in a tree form, each node on the tree represents one structure in the source code, and the syntax structure of the source code is easier to determine by using an abstract syntax tree to express the source code. Specifically, the source codes written in different languages can be processed in different ways to obtain the corresponding abstract syntax tree expressions. If the abstract syntax tree expression is the Vue file, the Vue-template-builder is used for compiling to obtain the corresponding abstract syntax tree expression, and if the JavaScript file is the TypeScript file, the TypeScript is used for compiling to obtain the corresponding abstract syntax tree expression.
In the concrete implementation process, other manners or means may also be adopted to obtain the abstract syntax tree expression corresponding to the source code, which may be determined according to the actual situation, and is not limited herein.
102. And determining a target node comprising a specific language pattern in the abstract syntax tree, and acquiring the position information of the target node and the specific language pattern value included by the target node.
And searching based on the obtained abstract syntax tree, determining a target node comprising the specific language pattern in the abstract syntax tree, and further determining a node needing international processing. The specific adopted retrieval method can be regular matching, and if the source code comprises the Chinese language scheme, the regular matching Chinese language can be used for retrieval, so that the target node comprising the Chinese language scheme, the position information of the target node in the abstract syntax tree and the specific content of the Chinese language scheme comprised by the target node are obtained.
By using the abstract syntax tree for searching, all contents in the source code can be correctly expressed, and the searching can not be omitted, so that the implementation performance of the scheme is improved.
103. And generating an index value corresponding to the specific language pattern value based on a preset rule.
In the internationalization processing process of the source code, the principle is that an index value is used for replacing an original specific language case value, when the language environment required by a user is determined, the case value recorded in a translation resource file adopted by the corresponding language environment is determined based on the index value, and then the corresponding case value is displayed for the user, so that the requirement of the user on the display language is met.
The generation of the index value can adopt a preset rule, different index values are needed to be adopted for corresponding to different specific language case values so as to avoid that the reference relationship is disordered and the scheme cannot be implemented, and the specific preset rule can be preset by a worker and is not limited at the specific position.
104. Replacing a specific language pattern value included in the source code with the index value based on location information of a target node.
And processing the source code based on the position information determined from the abstract syntax tree, and replacing the specific language pattern value in the source code with the generated index value, wherein the generated index value is 'tuya' and the specific language pattern value in the source code is 'hello'. Then the part of the content of the 'hello' in the original source code processed by the step is replaced by the index value 'tuya', and when the source code is actually operated, the corresponding file value in the corresponding translation resource file needs to be called based on the index value 'tuya'. For the Chinese language translation resource file, the case value corresponding to the index value "tuya" is "hello", and for the English language translation resource file, the case value corresponding to the index value "tuya" is "hello". The specific file values corresponding to the different index values are set based on the user requirements, and are not limited herein.
105. And writing the index value and the language-specific pattern value into a translation resource file.
And writing the index value and the language-specific pattern value into a translation resource file so as to carry out subsequent processing. For the case that the pattern included in the source code is of the chinese language, the translation resource file obtained in this step may be considered as the translation resource file corresponding to the chinese language, and the translation resource file may also be used as the original for the translator to translate to other languages. Because the translation resource file records the corresponding relationship between the case value and the index value, after the translator translates the case value, other language translation resource files with the corresponding relationship with the index value can be obtained, and the subsequent use is convenient.
According to the technical scheme, the embodiment of the application has the following advantages: according to the scheme, the abstract syntax tree processing is carried out on the source code, the abstract syntax tree corresponding to the source code is obtained, the abstract syntax tree is retrieved, the target node comprising the specific language pattern in the abstract syntax tree is determined, the index value is generated on the basis of the preset rule to replace the specific language pattern value included in the source code by extracting the relevant information of the target node, the source code not comprising the specific language pattern is further obtained, and meanwhile, the index value and the specific language pattern value are written into the translation resource file, so that the front-end worker avoids the tedious retrieval and index value setting work, and the efficiency of the code internationalization processing process is improved. In addition, the abstract syntax tree is used for searching the specific language pattern, so that the searching processing granularity of the source code is improved, the filtering mechanism is more exquisite, and manual readjustment and supplement are not needed after the index value is automatically replaced.
Based on the embodiment described in fig. 1, in the actual code internationalization processing process, since different documents are in different language environments, only the index value is used to replace the document value in the original source code, which may cause the document value pointed by the index value not to be successfully displayed, and therefore, an appropriate adjustment needs to be performed based on the grammar environment in which the index value is located, and this adjustment process needs to consume a large amount of management of front-end workers, so to solve the above problem, a detailed embodiment that the scheme can be selectively executed in the implementation process is provided below, please refer to fig. 2, an embodiment of the present application includes: step 201-step 209.
201. And acquiring a path of the file to be scanned.
In this embodiment, all the steps of the present solution are executed by using a node.js command line tool, and node.js makes JavaScript a development platform running on a server, and is more suitable for a front-end developer to adjust vue projects. It is understood that the tool for executing the method is not limited to the command line tool, and the webpack plug-in may be used to complete the steps in the present scheme, which is not limited herein.
In the practical implementation process of the scheme, the project needing international processing may be composed of a plurality of files, so that in order to ensure the implementation efficiency of the scheme, the number of the translated resource files is reduced as much as possible, the implementability of the scheme is improved, and when the international processing is carried out, all files under one project can be selected in a mode of selecting a path of the file to be scanned, and the implementation of the scheme is facilitated. The specific implementation manner may be to use the node path module api to remove the filtered path from the selected root path, and the filtered path may also be selected during path selection, which indicates that the file does not need to be internationally processed, so as to obtain all the file paths to be scanned.
202. And obtaining the source codes of all files in the path of the file to be scanned.
The file of the path of the file to be scanned includes Vue file and JavaScript file as an example for explanation, it can be understood that in the actual implementation process of the present solution, other types of files may also be included, and the specific details are not limited herein.
203. An abstract syntax tree representation of source code is obtained.
The execution process of this step is similar to the execution process of step 101 in the embodiment corresponding to fig. 1, and details thereof are not repeated here. For the case of including Vue files and JavaScript files, Vue-template-compiler could be used to compile the Vue file, typeScript could be used to compile the Script part of the JavaScript file, Vue file, and then get the abstract syntax tree representation of the source code.
204. And determining a target node comprising a specific language pattern in the abstract syntax tree, and acquiring the position information of the target node and the specific language pattern value included by the target node.
And determining a target node comprising a specific language pattern in the abstract syntax tree, and acquiring the position information of the target node and the specific language pattern value included by the target node. The execution process of this step is similar to the execution process of step 102 in the embodiment corresponding to fig. 1, and details thereof are not repeated here.
205. And acquiring grammatical information of the specific language case based on the target node and the node associated with the target node.
Specifically, the syntax information may be determined based on different rules for the node extracted from the file type, such as Vue file or JavaScript file, and further, the syntax information corresponding to the node may be determined based on the type (type) information of the node, in some specific cases, not only the syntax information of the specific language file needs to be determined using the information of the target node, but also the information of the node used for the target node to associate with the target node needs to be determined, which may be determined according to the actual situation, and is not limited herein.
206. And determining an objective function corresponding to the grammar information.
The corresponding relationship between the syntax information and the objective function can be preset by the staff, and specifically, the contents described with reference to fig. 3 to 5 can be referred to. Fig. 3 shows the processing results that may be obtained during the internationalization process for the Script portion of the vue file, and the specific changes of the source code during the transformation from the "hello" file to the "nihao" index value are shown in fig. 3. In the case of the first line of fig. 3, the target function determined based on the source code is this. $ t (), whereas in the case of the second line of fig. 3, the target function determined based on the source code is this. $ t () part, and there are components of the target function such as function () { return }. Similarly, for the contents described in the third line and the fourth line shown in fig. 3, the syntax information applied to the "hello" document is different, and the determined objective functions are different, but the objective functions determined in this step are all to make the index values operate normally in the source code. That is, the specific language pattern pointed by the index value can be normally displayed when the source code runs, and the display mode is the same as the display mode recorded in the original source code.
Similarly, fig. 4 shows a partial example of adjusting the patterns in different grammatical situations during the internationalization process of the template part in vue file, and fig. 5 is a partial example of internationalizing the javascript part. It should be understood that fig. 3 to fig. 5 are only partial illustrations of processing different syntax information by using different objective functions, and in the implementation process of the present embodiment, a worker may adjust the syntax according to the actual situation to ensure that the specific language pattern referenced by the index value can be normally displayed in the same manner as the display manner described in the original source code, which may be determined according to the actual situation, and is not limited herein.
207. And generating a unique random value, and taking the unique random value as an index value corresponding to the specific language pattern value.
The specific index value generation mode can be composed of the file name (default is index) of the translation resource file, 4-digit conversion pinyin before the Chinese language case and digital anti-resetting. The first 4-digit pinyin conversion of the chinese language can be implemented by using an npm package (package management tool in JavaScript environment) uuid tool, and it should be understood that the specific implementation of this process may also be implemented by using other tools, which is not limited herein. For a document without duplication, a digital duplication prevention bit may not be added, for example, if the document is "hello", the corresponding generated key value is "index. niha", but if another document appears, the content is "hello, world", the corresponding generated key value is "index. niha 0001". On the premise of ensuring the one-to-one correspondence between the file content and the index value, the generation rule of the index value can be set by the staff, and the specific details are not limited here.
208. Replacing a specific language pattern value included in the source code with the objective function and the index value based on the location information of the target node.
The replacing process can refer to the contents shown in fig. 3 to fig. 5, and the determined objective function and the grammar rule of the index value should be complied with in the replacing process, so that the specific language pattern pointed by the index value can be normally displayed, and details are not repeated here.
After the replacement process of the target function and the index value for the specific language pattern in the source code is completed, the obtained source code is the source code only including the index value, and resource files of different languages matched with the index value in the source code can be generated in a matched manner so as to facilitate the subsequent use of the source code. In addition, in an environment of performing source code processing by using VSCode, an i18n all plug-in may be used to further improve the implementability of the present solution, and the i18n all plug-in may provide a convenient function of annotating a chinese document in-line at the location after the document content is replaced by the index value, which may be determined according to the actual situation, and is not limited herein.
209. And writing the index value and the language-specific pattern value into a translation resource file.
For the case of containing Chinese file content, the obtained translation resource file is a Chinese resource file which can be matched with the processed source code for use. Meanwhile, the translation resource file can also be used as an original for a translator to translate to other languages, and because the translation resource file records the corresponding relationship between the case value and the index value, the translator can also obtain the translation resource file of other languages with the corresponding relationship with the index value after translating the case value, so that the subsequent use is facilitated.
According to the technical scheme, the embodiment of the application has the following advantages: when the index value is replaced, the grammar information followed by the specific language file is determined based on the current grammar environment of the specific language file embodied by the abstract grammar tree, the corresponding objective function is determined based on the grammar information, the specific language file in the source code is replaced by using the objective function and the index value, the index value is ensured to be normally displayed when being used, and the display mode is the same as the display mode recorded in the original source code. The feasibility of the scheme is improved.
In the implementation process of the present embodiment, the node.js command line tool may further perform conversion work of the translation resource file so as to be performed in cooperation with the work of the translation worker, specifically please refer to fig. 6, where the process of importing and exporting the translation resource file includes: step 601 to step 603.
601. And converting the translation resource file into a preset format file.
The translation resource file obtained based on the last step described in the embodiment of fig. 1 or fig. 2 is a translation resource file used in cooperation with the source code, and the format of the translation resource file is generally a json format, which can be understood as a format of the translation resource file that can also be other formats that can function in cooperation with the source code, and is not limited in details.
For the translator, there are many inconveniences in performing the translation work based on the original format of the translation resource file, so the translation resource file can be converted, that is, the translation resource file is exported as a preset format file suitable for the translator to perform the translation work, a specific exported file format may be a tsv file, the completion of the process may be formatted by api tsvformatorows provided by npm package d3-dsv, and finally exported as a tsv file, the specific format of the exported preset format file and the used tool are determined according to actual situations, and the specific description is not limited herein.
602. And filling the translation file corresponding to the language-specific file in the preset format file to obtain a complete translation resource file.
The method comprises the following steps of completing translation by a translator based on a Chinese pattern recorded in a preset format file, adding pattern contents of other languages in the preset format file, and finally obtaining a complete translation resource file.
603. And converting the complete translation resource file into a json format file.
Js command line tool can also convert the complete translation resource file into a json format file, namely, the api tsvParseRows provided by npm package d3-dsv analyzes the tsv file, and data is written into the local translation json file so as to be convenient for calling a source code and ensure the smooth implementation of a front-end project.
The above description has been made on the code processing method in the embodiment of the application, and the following description is made on the code processing apparatus in the embodiment of the present invention. Referring to fig. 7, an embodiment of a code processing apparatus of the present application includes:
an obtaining unit 701, configured to obtain an abstract syntax tree representation of a source code;
a determining unit 702, configured to determine a target node including a language-specific pattern in the abstract syntax tree, and obtain position information of the target node and a language-specific pattern value included in the target node;
a generating unit 703, configured to generate an index value corresponding to the specific language case value based on a preset rule;
a replacing unit 704 configured to replace a specific language pattern value included in the source code with the index value based on the location information of the target node;
a writing unit 705, configured to write the index value and the language-specific pattern value into a translation resource file.
In this embodiment, the flow executed by each unit in the code processing apparatus is similar to the method flow described in the embodiment corresponding to fig. 1, and is not described again here.
Referring to fig. 8, another embodiment of the code processing apparatus of the present application includes:
a source code acquiring unit 801, an acquiring unit 802, a determining unit 803, an objective function determining unit 804, a generating unit 805, a replacing unit 806, a writing unit 807, and a translating unit 808. The obtaining unit 802, the determining unit 803, the generating unit 805, the replacing unit 806, and the writing unit 807 are similar to the above-described units in the embodiment corresponding to fig. 7, and detailed description thereof is omitted here.
A source code acquiring unit 801, configured to acquire a path of a file to be scanned;
and obtaining the source codes of all files in the path of the file to be scanned.
An objective function determining unit 804, configured to obtain, based on the objective node, syntax information corresponding to the specific language pattern value;
determining an objective function corresponding to the syntax information, wherein the objective function is used for enabling the index value to normally run in the source code;
the replacement unit is specifically configured to: replacing a specific language pattern value included in the source code with the objective function and the index value based on the location information of the target node.
The objective function determining unit 804 is specifically configured to:
acquiring grammatical information of the specific language case based on the target node and a node associated with the target node;
and determining an objective function corresponding to the grammar information.
The translation resource file is a json format file, and the device further comprises:
a translation unit 808, configured to convert the translation resource file into a preset format file;
filling a translation file corresponding to the language-specific file in the preset format file to obtain a complete translation resource file;
and converting the complete translation resource file into a json format file.
Fig. 9 is a schematic structural diagram of a computer device according to an embodiment of the present application, where the server 900 may include one or more Central Processing Units (CPUs) 901 and a memory 905, and the memory 905 stores one or more application programs or data therein.
In this embodiment, the specific functional module division in the central processing unit 901 may be similar to the functional module division manner of each unit described in the foregoing fig. 7, and is not described here again.
Memory 905 may be volatile storage or persistent storage, among others. The program stored in the memory 905 may include one or more modules, each of which may include a sequence of instruction operations for a server. Still further, the central processor 901 may be arranged to communicate with the memory 905, and to execute a series of instruction operations in the memory 905 on the server 900.
The server 900 may also include one or more power supplies 902, one or more wired or wireless network interfaces 903, one or more input-output interfaces 904, and/or one or more operating systems, such as Windows Server, Mac OS XTM, UnixTM, LinuxTM, FreeBSDTM, etc.
The central processing unit 901 may perform the operations performed by the code processing method in the embodiment shown in fig. 1, which are not described herein again.
The present invention also provides a computer-readable storage medium for implementing the functions of the code processing method, on which a computer program is stored, which, when executed by a processor, the processor may be adapted to perform the method as described in fig. 1.
It will be appreciated that the integrated units, if implemented in the form of software functional units and sold or used as a stand-alone product, may be stored in a corresponding one of the computer readable storage media or integrated as a computer program product for performing the above-described method. Based on such understanding, all or part of the flow of the method according to the above embodiments may be implemented by a computer program, which may be stored in a computer-readable storage medium and used by a processor to implement the steps of the above embodiments of the method. Wherein the computer program comprises computer program code, which may be in the form of source code, object code, an executable file or some intermediate form, etc. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, usb disk, removable hard disk, magnetic disk, optical disk, computer Memory, Read-Only Memory (ROM), Random Access Memory (RAM), electrical carrier wave signals, telecommunications signals, software distribution medium, and the like. It should be noted that the computer readable medium may contain content that is subject to appropriate increase or decrease as required by legislation and patent practice in jurisdictions, for example, in some jurisdictions, computer readable media does not include electrical carrier signals and telecommunications signals as is required by legislation and patent practice.
The integrated unit, if implemented in the form of a software functional unit and sold or used as a stand-alone product, may be stored in a computer readable storage medium. Based on such understanding, the technical solution of the present invention may be embodied in the form of a software product, which is stored in a storage medium and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the steps of the method according to the embodiments of the present invention. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk, and other various media capable of storing program codes.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In the several embodiments provided in the present application, it should be understood that the disclosed system, apparatus and method may be implemented in other manners. For example, the above-described apparatus embodiments are merely illustrative, and for example, the division of the units is only one logical division, and other divisions may be realized in practice, for example, a plurality of units or components may be combined or integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or units, and may be in an electrical, mechanical or other form.
The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiment.
In addition, functional units in the embodiments of the present application may be integrated into one processing unit, or each unit may exist alone physically, or two or more units are integrated into one unit. The integrated unit can be realized in a form of hardware, and can also be realized in a form of a software functional unit.
The above-mentioned embodiments are only used for illustrating the technical solutions of the present invention, and not for limiting the same; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions of the embodiments of the present invention.

Claims (10)

1. A code processing method, comprising:
obtaining an abstract syntax tree representation of a source code;
determining a target node comprising a specific language pattern in the abstract syntax tree, and acquiring the position information of the target node and the specific language pattern value included by the target node;
generating an index value corresponding to the specific language case value based on a preset rule;
replacing a specific language pattern value included in the source code with the index value based on the location information of the target node;
and writing the index value and the language-specific pattern value into a translation resource file.
2. The method of claim 1, wherein the determining the target node in the abstract syntax tree that includes the language-specific pattern further comprises:
acquiring grammatical information corresponding to the specific language pattern value based on the target node;
determining an objective function corresponding to the syntax information, wherein the objective function is used for enabling the index value to normally run in the source code;
the replacing, by the index value, a particular language-pattern value included in the source code based on the location information of the target node includes: replacing a specific language pattern value included in the source code with the objective function and the index value based on the location information of the target node.
3. The code processing method according to claim 2, wherein said obtaining, based on the target node, syntax information corresponding to the specific language pattern value comprises:
and acquiring grammatical information of the specific language case based on the target node and the node associated with the target node.
4. The method of claim 1, wherein the obtaining the abstract syntax tree representation of the source code further comprises:
acquiring a path of a file to be scanned;
and obtaining the source codes of all files in the path of the file to be scanned.
5. The code processing method of claim 1, wherein the translation resource file is a json format file;
the method further comprises the following steps:
converting the translation resource file into a preset format file;
filling a translation file corresponding to the language-specific file in the preset format file to obtain a complete translation resource file;
and converting the complete translation resource file into a json format file.
6. The method according to claim 1, wherein the generating an index value corresponding to the specific language case value based on a preset rule comprises:
and generating a unique random value, and taking the unique random value as an index value corresponding to the specific language pattern value.
7. The code processing method according to claim 1, wherein the source code is a source code included in an Vue file and/or a source code included in a JavaScript file.
8. A code processing apparatus, comprising:
the device comprises an acquisition unit, a processing unit and a control unit, wherein the acquisition unit is used for acquiring abstract syntax tree expression of a source code;
a determining unit, configured to determine a target node including a language-specific pattern in the abstract syntax tree, and obtain position information of the target node and a language-specific pattern value included in the target node;
the generating unit is used for generating an index value corresponding to the specific language pattern value based on a preset rule;
a replacing unit configured to replace a specific language pattern value included in the source code with the index value based on the location information of the target node;
and the writing unit is used for writing the index value and the specific language pattern value into a translation resource file.
9. A computer device, comprising:
the system comprises a central processing unit, a memory, an input/output interface, a wired or wireless network interface and a power supply;
the memory is a transient memory or a persistent memory;
the central processor is configured to communicate with the memory, the instructions in the memory being executable on the computer to perform the method of any of claims 1-7.
10. A computer-readable storage medium comprising instructions that, when executed on a computer, cause the computer to perform the method of any one of claims 1-7.
CN202110431775.4A 2021-04-21 2021-04-21 Code processing method and related equipment Pending CN113076167A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202110431775.4A CN113076167A (en) 2021-04-21 2021-04-21 Code processing method and related equipment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202110431775.4A CN113076167A (en) 2021-04-21 2021-04-21 Code processing method and related equipment

Publications (1)

Publication Number Publication Date
CN113076167A true CN113076167A (en) 2021-07-06

Family

ID=76618278

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202110431775.4A Pending CN113076167A (en) 2021-04-21 2021-04-21 Code processing method and related equipment

Country Status (1)

Country Link
CN (1) CN113076167A (en)

Cited By (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113806663A (en) * 2021-09-23 2021-12-17 网易(杭州)网络有限公司 Page localization method and device, electronic equipment and computer readable storage medium
CN114756183A (en) * 2022-06-16 2022-07-15 深圳市信润富联数字科技有限公司 Data printing method, device, equipment and storage medium
CN115796190A (en) * 2023-01-10 2023-03-14 杭州比智科技有限公司 Front-end international multi-language conversion method and system based on vue and webpack
CN117313751A (en) * 2023-09-28 2023-12-29 上海嗨普智能信息科技股份有限公司 Method for establishing integrated Web multi-language translation frame and using method

Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107329957A (en) * 2017-05-18 2017-11-07 网易(杭州)网络有限公司 Replace the method and computer-readable recording medium of code Chinese character string
CN110413574A (en) * 2019-07-30 2019-11-05 深圳市携众通科技有限公司 A kind of method of automatic code generating internationalized resources

Patent Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107329957A (en) * 2017-05-18 2017-11-07 网易(杭州)网络有限公司 Replace the method and computer-readable recording medium of code Chinese character string
CN110413574A (en) * 2019-07-30 2019-11-05 深圳市携众通科技有限公司 A kind of method of automatic code generating internationalized resources

Cited By (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113806663A (en) * 2021-09-23 2021-12-17 网易(杭州)网络有限公司 Page localization method and device, electronic equipment and computer readable storage medium
CN114756183A (en) * 2022-06-16 2022-07-15 深圳市信润富联数字科技有限公司 Data printing method, device, equipment and storage medium
CN115796190A (en) * 2023-01-10 2023-03-14 杭州比智科技有限公司 Front-end international multi-language conversion method and system based on vue and webpack
CN117313751A (en) * 2023-09-28 2023-12-29 上海嗨普智能信息科技股份有限公司 Method for establishing integrated Web multi-language translation frame and using method

Similar Documents

Publication Publication Date Title
CN113076167A (en) Code processing method and related equipment
WO2017016179A1 (en) International multi-language implementation method of configuration software
CN107402746B (en) Method and device for automatically generating code file
US20140006006A1 (en) Dynamic Generation of Auto-Suggest Dictionary for Natural Language Translation
CN110554875B (en) Code conversion method and device, electronic equipment and storage medium
CN110502520B (en) Method, system and equipment for data warehousing and computer readable storage medium
CN103309647A (en) Application program multi-language support system and method
CN109766085B (en) Method and device for processing enumeration type codes
CN111736840A (en) Compiling method and running method of applet, storage medium and electronic equipment
KR100853933B1 (en) Migration apparatus which convert application program of mainframe system into application program of open system and method for thereof
CN104750472A (en) Resource bundle management method and device of terminal application
CN111813409A (en) Code generation method, device, equipment and storage medium of interactive interface
CN113760266B (en) Method and device for creating function module file and electronic equipment
CN106776779B (en) Method for generating entity file by JSON data based on Mac platform
CN115408088A (en) Software system language internationalization full-process automation method
CN109947711B (en) Automatic multi-language file management method in IOS project development process
CN114327492A (en) Code translation method, device and equipment
CN116595967A (en) Natural language rule writing method based on text and related device
CN116243901A (en) Method and system for rapidly developing multilingual project of front-end page
CN111273913B (en) Method and device for outputting application program interface data represented by specifications
CN109828775B (en) WEB management system and method for multilingual translation text content
CN111290771A (en) System upgrading method, device, equipment and computer readable storage medium
CN112199335A (en) Generation method and generation system of operation log
CN111443979B (en) Document processing method, device, computer equipment and storage medium
CN117270962B (en) Method and device for processing coloring language, storage medium and electronic equipment

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
RJ01 Rejection of invention patent application after publication
RJ01 Rejection of invention patent application after publication

Application publication date: 20210706