Skip to content

muneeb21/translation-api

Repository files navigation

TRANSLATION API WITH CACHE IMPLEMENTATION

A design of an API for translation of text to any particular language the user wants. There is implementation of cache using Redis for repeatedly hitting api's and smart caching is also implemented.

Tech stack used

Nodejs
Express framework
Redis (For cache)

Setup the API

Fireup the Redis server on PORT 6379
Run npm start (In VSC terminal)

Theme of the API

  • Translates text
  • Implements caching for repeated api hits
  • Pre-Caching is being done in such a way that if user enters a language then same text for other languages of same region also gets cached anticipating that user might also want to translate in some other language.
  • For example if the user translates the text to kannada, then translation to languages such as hindi, telugu, bengali etc will also be stored in cache which will reduce the response time of the api(For hindi,telugu etc).
  • Expiration time of each data is set in cache.

Key modules used

  • vitalets/google-translate-api (for translating text)
  • iso-639-1 (for detecting language code)

Explanation of Design Decisions

  • Express framework used
  • Controller folder has the functionality of text translation and pre-caching
  • Middleware folder contails the middleware of cache that act as a check before moving to the controller, if key is present in redis then it would return the value and not move to the controller else if not found it will move to the controller
  • Client enters text and language he wants to translate it to
  • At the server side Language code is detected by using iso-639-1 module as we need this code for translation
  • vitalets/google-translate-api module is used to translate the text, text and language code are passed as parameters to translate function provided by this module and in response we get the translated text
  • This translated text is cached to reduce response time of repeated api hits
  • For similar languages a list of related languages is stored
  • Smart pre-cache function is being called in the translate text function in which related languages of the entered language are cheched, translated and stored in cache
  • Evaluation of the results is done by comparing the response time of the api

Screenshots

alt text

Repeated hit for same text alt text

Example for pre-caching

alt text

Pre-caching

alt text