Integrations With Translation Services
Overview
This guide walks you through the end-to-end process of integrating Google Cloud Translate with WellSaid Labs API to generate AI-powered voiceovers for translated text. While this guide focuses on Google Cloud Translate, the same principles apply to other translation services, such as Microsoft Translator, DeepL, or AWS Translate. You can adapt the implementation to use a different API by modifying the translation request logic.
Prerequisites
Before you begin, ensure you have:
- A Google Cloud Project with the Cloud Translation API enabled.
- A Google Cloud Service Account with JSON Key for authentication.
- A WellSaid Labs API Key for generating AI voiceovers.
- A working Node.js/React app (or any backend of your choice).
Setting Up Google Cloud Translate
Step 1: Enable Cloud Translation API
- Go to the Google Cloud Console.
- Select your project (or create a new one).
- Navigate to APIs & Services > Library.
- Search for "Cloud Translation API" and click Enable.
Step 2: Create a Service Account & Download JSON Key
- Navigate to IAM & Admin > Service Accounts.
- Click Create Service Account and give it a name (e.g.,
translation-service
). - Assign the "Cloud Translation API User" role.
- After creating the account, go to the Keys tab and click Add Key > JSON.
- Download the JSON file and store it securely in your project directory.
Setting Up WellSaid Labs API
Step 1: Get Your WellSaid Labs API Key
- Sign up for a WellSaid Labs account.
- Navigate to API Settings and copy your API Key.
Step 2: Retrieve Available AI Voices
To get a list of available AI voices, use this GET request:
curl -X GET "https://api.wellsaidlabs.com/v1/tts/avatars" \
-H "X-API-KEY: YOUR_WELLSAID_API_KEY" \
-H "Accept: application/json"
Response: You’ll receive a list of available speakers. Save the "avatar_id"
of the voice you want to use.
Writing the Integration Code
Step 1: Install Dependencies
Run this in your project folder:
npm install axios dotenv google-auth-library
Step 2: Configure Environment Variables
Create a .env
file to store your WellSaid Labs API Key:
WELLSAID_API_KEY=your_wellsaid_api_key
GOOGLE_APPLICATION_CREDENTIALS=./path-to-your-service-account.json
The GOOGLE_APPLICATION_CREDENTIALS
variable tells Google Cloud SDK where to find your JSON key.
Step 3: Implement Google Cloud Translate API
Create a translate.js
file:
const { TranslationServiceClient } = require("@google-cloud/translate");
const path = require("path");
const client = new TranslationServiceClient({
keyFilename: path.join(__dirname, process.env.GOOGLE_APPLICATION_CREDENTIALS),
});
async function translateText(text, targetLanguage) {
const projectId = "your-google-cloud-project-id"; // Replace with your actual project ID
const location = "global";
const request = {
parent: `projects/${projectId}/locations/${location}`,
contents: [text],
mimeType: "text/plain",
targetLanguageCode: targetLanguage,
};
try {
const [response] = await client.translateText(request);
return response.translations[0].translatedText;
} catch (error) {
console.error("Translation API Error:", error.message);
return text; // Fallback: Return original text if translation fails
}
}
module.exports = { translateText };
This function sends text to Google Cloud Translate and retrieves the translated version. You can adapt this function to work with other translation APIs by modifying the request structure.
Step 4: Send Translated Text to WellSaid Labs
Create a textToSpeech.js
file:
const axios = require("axios");
require("dotenv").config();
const WELLSAID_API_KEY = process.env.WELLSAID_API_KEY;
async function generateSpeech(text, speakerId) {
const url = "https://api.wellsaidlabs.com/v1/tts/stream";
try {
const response = await axios.post(
url,
{ text, speaker_id: speakerId },
{
headers: {
"X-API-KEY": WELLSAID_API_KEY,
"Content-Type": "application/json",
},
}
);
return response.data.audio_url; // Returns the generated audio URL
} catch (error) {
console.error("WellSaid API Error:", error.message);
return null;
}
}
module.exports = { generateSpeech };
This function sends text to WellSaid Labs and retrieves the AI-generated audio URL.
Conclusion
By integrating Google Cloud Translate with WellSaid Labs API, you can:
Translate text into any language.
Convert it into a realistic AI voice.
Automate multilingual voiceover creation for your app.
While this guide focuses on Google Cloud Translate, you can swap it out for Microsoft Translator, DeepL, AWS Translate, or any other translation API by modifying the translation request function. The same principles apply across different services.
Updated about 1 month ago