我的EmailHandler类如下:
Class EmailHandler {
...
@Autowired
protected Configuration config;
}
...
protected Template getTemplate(String templateName, Locale locale) throws IOException, TemplateException {
return config.getTemplate(templateName, locale);
}
...
}在我的applicationcontext.xml里
<bean id="freemarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
<property name="templateLoaderPath" value="classpath:/templates/email"/>
</bean>模板的目录结构如下:
--src
----main
------resources
--------templates
----------email
------------template1.ftl
------------template2.ftl
------------template3.ftl
------------template4.ftl
------------multilanguage
--------------fr
----------------template1.ftl目前,getTemplate总是与(string,Locale.US)一起调用。但是,在将来,我希望能够用(string,Locale.FR)调用Locale.FR。
以下是我的问题: 1.如何更改目录结构以指定法语模板。2. config.getTemplate(templateName,locale)到底做了什么?该方法如何在模板目录中找到Locale.US的模板? 3.我想从email/multilanguage/fr目录中加载我的法语模板。我该怎么做?
谢谢,
理时
发布于 2014-04-11 20:06:10
当您调用getTemplate("foo.ftl", Locale.US)时,FreeMarker首先尝试加载foo_en_US.ftl,然后是foo_en.ftl,最后是foo.ftl。因此,法语模板应该命名为foo_fr.ftl。
为getTemplate指定的区域设置还决定模板中locale设置的值。但是,这可以在Environment对象中重写。如果您调用myTemplate.process(...)而不是调用env = myTemplate.createProcessingEnvironment(...); env.setLocale(...); env.process(),或者在模板中调用<#setting locale='...'>,则可以这样做。
在根据区域设置从子目录中加载模板时,您可以为此实现一个TemplateLookupStrategy (参见http://freemarker.org/docs/api/freemarker/cache/TemplateLookupStrategy.html)。
https://stackoverflow.com/questions/23020094
复制相似问题