Email Internationalization using Velocity/FreeMarker Templates

后端 未结 2 1037
不思量自难忘°
不思量自难忘° 2021-01-30 13:51

How can I achieve i18n using a templating engine such as Velocity or FreeMarker for constructing email body?

Typically people tend to create templates like:



        
2条回答
  •  春和景丽
    2021-01-30 14:21

    It turns out using one template and multiple language.properties files wins over having multiple templates.

    This creates one basic problem: If my .vm files becomes large with many lines of text, it becomes tedious to translate and manage each of them in separate resource bundle (.properties) files.

    It is even harder to maintain if your email structure is duplicated over multiple .vm files. Also, one will have to re-invent the fall-back mechanism of resource bundles. Resource bundles try to find the nearest match given a locale. For example, if the locale is en_GB, it tries to find the below files in order, falling back to the last one if none of them is available.

    • language_en_GB.properties
    • language_en.properties
    • language.properties

    I will post (in detail) what I had to do to simplify reading resource bundles in Velocity templates here.

    Accessing Resource Bundle in a Velocity template

    Spring Configuration

    
        
    
    
        
        
        
            
                
            
        
    
    
    
        
        
    
    

    TemplateHelper Class

    public class TemplateHelper {
        private static final XLogger logger = XLoggerFactory.getXLogger(TemplateHelper.class);
        private MessageSource messageSource;
        private VelocityEngine velocityEngine;
    
        public String merge(String templateLocation, Map data, Locale locale) {
            logger.entry(templateLocation, data, locale);
    
            if (data == null) {
                data = new HashMap();
            }
    
            if (!data.containsKey("messages")) {
                data.put("messages", this.messageSource);
            }
    
            if (!data.containsKey("locale")) {
                data.put("locale", locale);
            }
    
            String text =
                VelocityEngineUtils.mergeTemplateIntoString(this.velocityEngine,
                    templateLocation, data);
    
            logger.exit(text);
    
            return text;
        }
    }
    

    Velocity Template

    #parse("init.vm")
    #msg("email.hello") ${user} / $user,
    #msgArgs("email.message", [${emailId}]).
    

    #msg("email.heading")

    I had to create a short-hand macro, msg in order to read from message bundles. It looks like this:

    #**
     * msg
     *
     * Shorthand macro to retrieve locale sensitive message from language.properties
     *#
    #macro(msg $key)
    $messages.getMessage($key,null,$locale)
    #end
    
    #macro(msgArgs $key, $args)
    $messages.getMessage($key,$args.toArray(),$locale)
    #end
    

    Resource Bundle

    email.hello=Hello
    email.heading=This is a localised message
    email.message=your email id : {0} got updated in our system.
    

    Usage

    Map data = new HashMap();
    data.put("user", "Adarsh");
    data.put("emailId", "adarsh@email.com");
    
    String body = templateHelper.merge("send-email.vm", data, locale);
    

提交回复
热议问题