Create a web server to translate a text from one language to another.
- Node
- MySQL
- VS Code
- Javascript: Primary programming language
- NodeJS: Javascript runtime environment
- ExpressJS: Backend Framework
- MySQL: Database for storing translations
- External API: Google-Translate API (for actual translations)
This is a Web Server which can be used by an individual looking to translate text from one language to the desired language. The user is required to provide following three fields : -
- Source language code
- Target language code
- Text to be translated
- Clone the repository from github (https://github.com/rishitsahu/translate-server)
- Open it in a code editor (VScode preferred)
- For the MySQL database connection you have to specify the port, host, user and the password in all the 4 modules which are db, route, smartCache, cacheMiddleware according to your MySQL database.
- Install all the dependencies present in package.json with following command -npm i dependency@lastest_version --save -npm install dotenv@google-cloud/translate --save (for the api, use this command)
- Define the port number in index.json (I have set it 1010 as of now)
- Run index.json with following command node index -Server is ready. Run the request in this format http://localhost:1010/translate?src_lang=hi&target_lang=en&text=Pardon
|__translations
|___ src_lang
|___ target_lang
|___ text
|___ translation
- Used MySQL as the database since there would be three fields provided by the user and a fourth field would be the translated text, and for managing these four fields it's best to store the data in the form of table
- For the smartcaching, it stores the translation of the given text in all the languages related to the target language because a user is also likely to translate the same text to the related languages
- The Router is designed in such a way that if the demanded text is already present in the database, then the server would not hit translate API and would return the translations directly from the database
- Use https://console.cloud.google.com/ for generating API KEY from your service account. Now, enable the cloud translate API (It will ask for the billing address but wouldn't charge for less than 50 request per day)
Note: As of now, credentials of the API will be sent through the mail so I have removed the credentials from the code I am uploading here. Please find it in the mail
-
As and when index.js is run on the command prompt a connection is created with the database and a new database is created named translate_data, a table is created in the database named translations with four fields namely src_lang (source language), target_lang, text (input), translation (output) and the server with the specified port number starts.
-
Now the user is required to provide the query in a browser in the above mentioned format.
-
Firstly query is routed to cacheMiddleware. If the same input fields matches with already present data in the database, translation along with the source language, target language and the text is fetched else it is routed to smartCache.
-
Now we have a list of similar languages in which we have already defined related languages. Translations in all the related languages are made through translate API and stored in the database and the query is back to route from where we send the output along with the other fields to the user.
-Following is the result for converting "how are you" source_lang = english to target_lang=gujrati
-I verified my code for different test cases and checked that it shouldn't contain similar data in the database. If the data is already available for the given input it should return the output from there only and should not hit the API for that. I checked the database for the same with different and similar queries, and also checked for the different sections of the code so that only that part of the code works which is required for the particular query.
Translation for "hello everyone"
Translations for similar languages (Smart Caching)
-
Instead of taking language code from the user we can just have the name of the source and the target language. A separate table can be created in the database in which we can store the languages with their language codes and then get the language code from the database to pass it to the API.
-
Instead of storing strings in the database we can do the operation on the words of the string, and we can store the individual words. Now for each input string, we would break down the string into words and store those words into database. After a reasonable number of translations, our database would be containing enough words to make meaningful sentences. We can make a separate machine learning model to train the dataset. This would account for a decreased API cost but a complex database.
- mysql
- dotenv
- body-parser
- @google-cloud/translate
- nodemon
Name: Rishit Sahu
Email: rishitsahu17@gmail.com
Phone No.: +91 9588036568