freemarker配置通过语言环境和模板名称获取模板

ris*_*p89 1 java spring localization freemarker

我的EmailHandler类如下:

Class EmailHandler {
    ...
    @Autowired
        protected Configuration config;
    }
    ...
    protected Template getTemplate(String templateName, Locale locale) throws IOException, TemplateException {
        return config.getTemplate(templateName, locale);
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

在我的applicationcontext.xml中,我有

<bean id="freemarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
    <property name="templateLoaderPath" value="classpath:/templates/email"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

我的模板目录结构如下:

--src
----main
------resources
--------templates
----------email
------------template1.ftl
------------template2.ftl
------------template3.ftl
------------template4.ftl
------------multilanguage
--------------fr
----------------template1.ftl
Run Code Online (Sandbox Code Playgroud)

目前,getTemplate始终使用(string,Locale.US)进行调用。但是,将来,我希望能够使用(string,Locale.FR)调用getTemplate。

以下是我的问题:1.如何更改目录结构以指定法语模板。2. config.getTemplate(templateName,locale)是什么?到底是做什么的?该方法如何在模板目录中找到Locale.US的模板?3.我想从email / multilanguage / fr目录加载我的法语模板。我怎么做?

谢谢,

里士

dde*_*any 7

当您调用时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)。