Reputation: 386
I am using GCP pubsub publisher to publish some message to a topic which in turn is used to send email through sendgrid API. I am able to send emails using this.
But I want to support multiple language email templates. All these emails are sent automatically based on some user actions.
Following is an example of my email template code:
import * as _ from 'lodash';
export const getUserRegEmailSubject = (data) => {
return `Account Created Successfully – ${_.get(data, 'name')}`;
};
export const getUserRegEmailBody = (data) => {
return `
<!DOCTYPE html>
<html lang="en">
<body>
<p style="padding-bottom: 5px">Thank you for registering with us.</p>
<b>Your account details:</b>
<b>Full Name: </b>${_.get(data, 'name')}<br/>
<b>Username: </b>${_.get(data, 'uid')}<br/>
<p style="padding-bottom: 5px">Thank you!!</p>
`;
}
And I'm using this email template in my service as follows;
async createUser(userData) {
// Some logic and validations
const subject = getUserRegEmailSubject(userData);
const body = getUserRegEmailBody(userData);
if(userData.email) {
const email = {
to: userData.email,
from: env.EMAIL_FROM,
subject: subject,
html: body,
};
await pubEmailReq(email);
}
}
Following is email publishing code:
export const pubEmailReq = async (email) => {
const pubSub = new PubSub();
const message = {
data: Buffer.from(JSON.stringify(email))
}
const msgId = await pubSubClient
.topic('projects/' + env.GCP_PROJECT + '/topics/sendEmail')
.publishMessage(message);
return msgId;
}
As you can see I have created the email template in only english language. But I need some way to convert it to other languages(mostly to Spanish and French as of now) before sending it to the publish email code.
I am looking for ways to translate this email template into the language preference selected by the user.
Any help will be great!!
Thank you!
Upvotes: 1
Views: 1811
Reputation: 4051
After our discussion in the comment section, I understood that you want to translate your email's body which is an HMTL page into another language using NodeJS. In order to preserve all the html tags, only translating the proper text in between them, you have to inform the translation API you will be sending an html page.
You can use the the Translation API ADVANCED v3. You can find the documentation here. Within the request body of v3, you need to specify the mime_type: text/html
, as per documentation. It will guarantee that only the text between html flags is translated. Follow the steps below,
1 - Make sure you followed the setup process, here, such as setting up the billing and authentication.
2 - Install the client libraries with:
npm install --save @google-cloud/translate
3 - Prepare your code. Below, the snippet shows how to translate a simple html text from English to German.
const projectId = 'YOUR_PROJECT_ID';
const location = 'global';
const text = 'To get in touch <a href="your_website" id="xyz">Click
here</a> and we will email you';
// Imports the Google Cloud Translation library
const {TranslationServiceClient} = require('@google-cloud/translate');
// Instantiates a client
const translationClient = new TranslationServiceClient();
async function translateText() {
// Construct request
const request = {
parent: `projects/${projectId}/locations/${location}`,
contents: [text],
mimeType: 'text/plain', // mime types: text/plain, text/html
sourceLanguageCode: 'en',
targetLanguageCode: 'de',
};
try {
// Run request
const [response] = await translationClient.translateText(request);
for (const translation of response.translations) {
console.log(`Translation: ${translation.translatedText}`);
}
} catch (error) {
console.error(error.details);
}
}
translateText();
4 - After executing the code, the output was:
Um Kontakt aufzunehmen <a path="your_website" id="xyz">Klicken Sie hier</a> und wir werden Ihnen eine E-Mail senden
Notice that the tags, such as path
and the website your_website
, were not translated.
Upvotes: 1