1

I have a requirement to send email to my contacts depending on the language selected by a custom pick-list. Although,Translation workbench provides a platform to have languages activated,and we can create custom labels and provide translations to the label text,but I also want to use Merge fields to be transmitted. I've tried everywhere,but still no solution yet to translate merge fields as well.Can anyone suggest a solution?

Email Template

Merge fields not getting translated

  • Cannot be done natively. – Adrian Larson May 11 '16 at 6:37
  • Thanks @Adrian ,is there an out-of-the-box way of achieving it? – Shobhit Saxena May 11 '16 at 6:38
0

This can be achieved using VisualForce email templates, apex:outputText, apex:param and custom labels with placeholder values in them. The outcome is functionality similar to how String.format() works in Apex. Example 1:

Custom Label MyLabel:

Hello {0} {1} and welcome to {2}.

Custom Label translation in Chinese (zh_CN), or any language you need to support:

{1}{0}您好,欢迎来到{2}.

I don't speak Chinese, but I know it has a much different grammar than English so I've auto-translated the above for the sake of this example and swapped the order of the given name and family name.

Visualforce Email Template:

<messaging:emailTemplate recipientType="User" language="{!recipient.LanguageLocaleKey}">
<messaging:htmlEmailBody>

    <p>
        <apex:outputText value="{!$Label.MyLabel}">
            <apex:param value="{!recipient.FirstName}" />
            <apex:param value="{!recipient.LastName}" />
            <apex:param value="Salesforce StackExchange" />
        </apex:outputText>
    </p>

</messaging:htmlEmailBody>
</messaging:emailTemplate>

This results in the following, depending on the User's name and language:

Hello Alice Doe and welcome to Salesforce StackExchange.

The benefit of using MessageFormat style strings for the custom labels is that it becomes easy to fit the placeholders into your translation so you can make subject-object-verb agreements, adjective order, given and family name order, etc. all make sense within your translation.

Another example using date and number formatting, adapted from this Java tutorial:

Custom Label (English, en_US):

At {0,time,short} on {2,date,long} we detected {1,number,integer} spaceships on the planet {0}.

Custom Label translation (German, de_DE):

Um {2,time,short} am {2,date,long} haben wir {1,number,integer} Raumschiffe auf dem Planeten {0} entdeckt.

Format in Visualforce:

<apex:outputText value="{!$Label.MyLabel}">
   <apex:param value="Omicron Persei 8" />
   <apex:param value="11" />
   <apex:param value="{!myValue.CreatedDate}" />
</apex:outputText>

Outcome in both languages:

en_US: At 10:16 AM on July 31, 2009, we detected 11 spaceships on the planet Omicron Persei 8.

de_DE: Um 10:16 am 31. Juli 2009 haben wir 11 Raumschiffe auf dem Planeten Omicron Persei 8 entdeckt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.