Search code examples
localizationtapestryemail-templates

Localization of e-mail templates in Tapestry 5?


Tapestry provides a great localization support for their pages and components. I would like to send localized e-mail corresponding to the users language preference.

Anybody has come across a solution for localizing e-mail templates that integrates well with Tapestry? (I don't mind if it is a not using Tapestry's templating engine)


Solution

  • I have created a separate directory/package for each language and retrieve the template file. And then used ThreadLocale..getLocale().getLanguage() to retrieve the current user's so I can send the e-mail on that language too. Here is the relevant code snippet:

    public class MailSender {
        private static final String EMAIL_TEMPLATE_ROOT = "com/xxx/emailtemplate/";
    
        @Inject
        private ThreadLocale locale;
    
        public void sendEmail(..., final String emailTemplateFileName) {
            String emailTemplateFilePath = getEmailTemplateFilePath(emailTemplateFileName);
            ....
        }
    
        private String getEmailTemplateFilePath(String templateLocation) {
            String language = locale.getLocale().getLanguage();
    
            return EMAIL_TEMPLATE_ROOT + language + "/" + templateLocation;
        }
    }
    

    This is not specific to a templating engine. You can use this technique with your favourite. (I kept Velocity as it was used by the project already)