【问题标题】:How to use multi lingual email templates in NestJS?如何在 NestJS 中使用多语言电子邮件模板?
【发布时间】:2021-05-12 22:34:24
【问题描述】:

我正在使用 GCP pubsub 发布者将一些消息发布到一个主题,该主题又用于通过 sendgrid API 发送电子邮件。我可以使用它发送电子邮件。

但我想支持多语言电子邮件模板。所有这些电子邮件都是根据一些用户操作自动发送的。

以下是我的电子邮件模板代码示例:

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>
  `;
}

我在我的服务中使用这个电子邮件模板如下;

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);
  }
}

以下是邮件发布代码:

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;
}

如您所见,我只用英语创建了电子邮件模板。但在将其发送到发布电子邮件代码之前,我需要一些方法将其转换为其他语言(目前主要是西班牙语和法语)。

我正在寻找方法将此电子邮件模板翻译成用户选择的语言首选项。

任何帮助都会很棒!

谢谢!

【问题讨论】:

  • 为了更深入地调查,您是否发布了您发送的整封电子邮件?另外,您从哪里获得电子邮件的正文?是静态的吗?
  • 是的,我发布了整封电子邮件。我从第一个代码 sn-p 中创建的 getUserRegEmailBody 函数获取电子邮件正文。
  • 您可以使用来自userdataemail's body 作为翻译API 的输入,如here 中所述。这对你有用吗?
  • 是的,这是一个选项,实际上我将在几个星期内使用它,因为电子邮件正文的输出将是一个 HTML 模板,我相信这个 API 无法翻译。虽然如果我只解析电子邮件模板中的字符串并将其传递给 API,它会完美地翻译它。谢谢,我试试这个!
  • 为了进一步为社区做出贡献,我将在上面发表我的评论作为答案。好吗?

标签: node.js google-cloud-platform nestjs multilingual google-cloud-pubsub


【解决方案1】:

在我们在评论部分进行讨论后,我了解到您想使用 NodeJS 将您的电子邮件正文(一个 HMTL 页面)翻译成另一种语言。为了保留所有 html 标记,只翻译它们之间的正确文本,您必须通知翻译 API 您将发送一个 html 页面。

您可以使用翻译 APIADVANCED v3。您可以找到文档here。在 v3 的请求正文中,您需要按照 documentation 指定 mime_type: text/html。它将保证只翻译 html 标志之间的文本。请按照以下步骤操作,

1 -确保您遵循了设置流程here,例如设置结算和身份验证。

2 - 安装客户端库:

npm install --save @google-cloud/translate

3 - 准备您的代码。下面,sn-p 展示了如何将简单的 html 文本从英语翻译成德语。

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 - 执行代码后,输出为:

Um Kontakt aufzunehmen <a path="your_website" id="xyz">Klicken Sie hier</a> und wir werden Ihnen eine E-Mail senden

请注意,path 和网站your_website 等标签未翻译。

【讨论】:

  • @YogeshSingh 这是answer your question吗?
  • @WytrzymałyWiktor 是的,这回答了我的问题。
猜你喜欢
  • 2013-09-10
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 2023-03-31
  • 2012-05-17
  • 2010-12-18
  • 1970-01-01
  • 2012-11-09
相关资源
最近更新 更多