Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java freemarker配置按区域设置和模板名称获取模板_Java_Spring_Localization_Freemarker - Fatal编程技术网

Java freemarker配置按区域设置和模板名称获取模板

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

我的EmailHandler类如下所示:

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
(请参阅)