Java freemarker配置按区域设置和模板名称获取模板
我的EmailHandler类如下所示:Java freemarker配置按区域设置和模板名称获取模板,java,spring,localization,freemarker,Java,Spring,Localization,Freemarker,我的EmailHandler类如下所示: Class EmailHandler { ... @Autowired protected Configuration config; } ... protected Template getTemplate(String templateName, Locale locale) throws IOException, TemplateException { return confi
Class EmailHandler {
...
@Autowired
protected Configuration config;
}
...
protected Template getTemplate(String templateName, Locale locale) throws IOException, TemplateException {
return config.getTemplate(templateName, locale);
}
...
}
--src
----main
------resources
--------templates
----------email
------------template1.ftl
------------template2.ftl
------------template3.ftl
------------template4.ftl
------------multilanguage
--------------fr
----------------template1.ftl
在applicationcontext.xml中,我有
<bean id="freemarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
<property name="templateLoaderPath" value="classpath:/templates/email"/>
</bean>
目前,getTemplate总是使用(string,Locale.US)调用。但是,在将来,我希望能够使用(string,Locale.FR)调用getTemplate
以下是我的问题:
1.如何更改目录结构以指定法语模板。
2.config.getTemplate(templateName,locale)是什么;你到底是怎么做的?该方法如何在模板目录中找到Locale.US的模板?
3.我想从email/multilanguage/fr目录加载法语模板。我该怎么做
谢谢
Rishi当您调用
getTemplate(“foo.ftl”,Locale.US)
时,FreeMarker首先尝试加载foo_en_US.ftl
,然后foo_en.ftl
,最后加载foo.ftl
。因此,法语模板的名称应该类似于foo_fr.ftl
为getTemplate
指定的区域设置还决定模板中locale
设置的值。但是,这可以在环境
对象中重写。如果调用env=myTemplate.createProcessingEnvironment(…),而不是myTemplate.process(…)
,则可以实现这一点;环境设置语言环境(…);env.process()
,或在模板中使用
在根据区域设置从子目录加载模板时,您可以为此实现TemplateLookupStrategy
(请参阅)