![](/img/trans.png)
[英]Email Internationalization using Velocity/FreeMarker Templates
[英]Java Spring Freemarker internationalization for templates
我正在尝试对我的模板进行国际化,但我不确定这样做是否是最有效的方法。
我将消息提取到资源包,并使用 MessageSource 方法getMessage(String var1, @Nullable Object[] var2, Locale var3)
获取基于 Locale 的翻译,然后将其放入模型并处理模板。
private String processTemplate(String templateName, String locale) {
try {
String greeting = messageSource.getMessage("messages.greeting", null, new Locale(locale));
Map<String, String> model = new HashMap<>();
model.put("greeting", greeting);
Template template = freemarkerConfiguration.getConfiguration().getTemplate(templateName);
return FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
} catch (IOException | TemplateException e) {
log.error("Error when processing template {}", templateName);
throw new EmailNotSentException();
}
}
我的问题是是否有更有效的方式以这种方式处理模板? 或者最好对模板进行国际化?
这取决于。 如果您使用这些模板作为主要的 ui 渲染器,则国际化消息是更好的选择。 但如果这些模板用于“发送电子邮件”,我会选择它们的国际化。 在我的应用程序中,我有一个类似的用例,我定义了我的模板,如:
order-template-fr.ftl
, order-template-es.ftl
, order-template-en.ftl
,...
我在运行时根据用户的语言环境使用LOCALE_CONTRY.toLowerCase()
解析模板。 例子:
String template = "order-template-"+LOCALE_CONTRY.toLowerCase()+".ftl";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.