Email Translation
Easy Email Pro provides built-in support for email template translation. You can translate both the subject and content of your email templates to different languages.
Basic Usage​
Use the EditorCore.translate method to translate your email templates:
import { EmailTemplate } from "easy-email-pro-editor";
import { EditorCore } from "easy-email-pro-core";
// Your email template
const values: EmailTemplate = {
subject: "Hello, World!",
content: {
// ... your email content
},
};
// Translate the template
const newTemplate = await EditorCore.translate({
template: {
subject: values.subject,
content: values.content,
},
async translate(words) {
// Connect to your translation service
const { data } = await axios.post<Record<string, string>>(
`your-translate-server-api`,
{
source: sourceLang, // Source language
target: targetLang, // Target language
words, // Words to translate
}
);
return data;
},
});
Interface Definition​
interface TranslateConfig {
// The template to translate
template: {
subject: string;
content: any;
};
// Translation function
translate: (words: string[]) => Promise<Record<string, string>>;
}
Translation Function​
The translate function do:
- Accept an array of strings to translate
- Return a Promise that resolves to an object mapping original text to translated text
- Handle any necessary API calls to your translation service
Example implementation with a translation service:
const translate = async (words: string[]) => {
// Example using a translation API
const response = await translateAPI.translate({
texts: words,
from: "en",
to: "es",
});
// Return translations mapped to original text
return response.translations.reduce((acc, translation, index) => {
acc[words[index]] = translation;
return acc;
}, {});
};