This is a simple Node.js server built using Express that provides an API for translating text using the google-translate-api-browser
package.
The API supports translation to a specified target language by leveraging Google Translate.
- Translation API: Translate any text to a specified target language.
- CORS Enabled: Cross-origin requests are supported for external API calls.
- Error Handling: Provides error messages for missing parameters or failed translations.
Before starting, ensure you have the following installed:
- Node.js (version 14 or higher)
- npm (version 6 or higher)
Follow the steps below to set up the project locally.
git clone https://github.com/NguyenQuocAnDev27/translation-api.git cd translation-api
Run the following command to install the required Node.js dependencies:
npm install express google-translate-api-browser cors
Start the server by running the following command:
node server.js
The server will be running on http://localhost:9000
.
Endpoint: /translate
Method: POST
Description: This endpoint accepts text and target language code, returning the translated version of the text.
text
(String, required): The text to be translated.targetLang
(String, required): The language code to translate the text into (e.g., 'en' for English, 'fr' for French).
{
"text": "Bonjour",
"targetLang": "en"
}
{
"originalText": "Bonjour",
"translatedText": "Hello"
}
- 400 Bad Request: If the required
text
ortargetLang
is missing from the request body, the API will respond with:
{
"error": "Missing required parameters"
}
- 500 Internal Server Error: If the translation fails for any reason, such as an external API issue, the API will return:
{
"error": "Translation failed"
}
You can test the API easily using Postman. Follow these steps:
Download and open Postman from here if you don’t already have it installed.
- Set the Method to
POST
. - Set the URL to
http://localhost:9000/translate
.
- Under the Body tab, select raw.
- Choose JSON from the dropdown.
- Add the following JSON object:
{
"text": "Hola",
"targetLang": "en"
}
Click Send. If everything is working, you should receive a response similar to this:
{
"originalText": "Hola",
"translatedText": "Hello"
}
- Method:
POST
- URL:
http://localhost:9000/translate
- Headers:
- Content-Type:
application/json
- Content-Type:
- Body (JSON):
{ "text": "Bonjour", "targetLang": "en" }
To deploy this application on a cloud platform or VPS, follow these steps:
- Install Node.js: Make sure Node.js is installed on your server.
- Clone the repository:
git clone https://github.com/yourusername/translation-api.git
- Install dependencies:
npm install
- Start the server using a process manager like PM2 (recommended for production):
pm2 start index.js
- Missing Required Parameters: Ensure that both
text
andtargetLang
are present in the request body. This error will return a 400 status with the message:
{
"error": "Missing required parameters"
}
- Translation Failed: This can occur if the translation service is down or there is an issue with the external API. It will return a 500 status with the message:
{
"error": "Translation failed"
}
If you're accessing the API from a front-end app and facing CORS issues, ensure that CORS is properly configured on your front-end.
- Node.js: JavaScript runtime environment.
- Express: Web framework for Node.js.
- google-translate-api-browser: A lightweight package to handle Google Translate requests.
- CORS: Middleware to enable cross-origin resource sharing.
This project is licensed under the MIT License. Feel free to use, modify, and distribute it as per the terms of the license.