Wednesday, August 21, 2013, 11:23:04 AM, Ruben Trancoso wrote:
> Hello there,
> I started using freemarker in a project where I would like to find
> templates from database as well from filesystem.
> My expectation was to have a signature on TemplateLoader where I
> could get the template name and the localeName separetely so I could query the db.
> This requirement came from the idea that on the front end the user
> will add the name and the locale on colluns like this:
> some.template, en_US
> some.template, pt_BR
> some.template, pt_PT
> some.template, en
> some.other.template, jp_JP
> but findTemplateSource blend the name and locale and perform it's
> fallback strategy, and also expect that all files are suffixed with
> .ftl (or something) resulting in lookup keys like this:
findTemplateSource gets a deduced template name that already contains
the locale postfix, inserted before the file extension (if there was
any - it's not needed).
> some_en_US.template, some.other_pt_BR.template.
> My first Idea was to extend TemplateCache, but it should break
> compatibility with the TemplateLoader interface when it comes to be a File.
> So I was wondering if some experienced user/developer could put
> some light on how to achieve this without much pain and preserving the code structure. .
I'm afraid you will have to extract this information from the template
name that findTemplateSource gets as parameter. For templates stored
on the file-system the convention is using .ftl file extension (helps
when you open them in an editor), and you probably want to keep it
transparent where the template comes from (file-system VS database).
Thus all template names should look the same, like some_en_US.ftl, and
then in the DataBaseTemplateLoader only, you will have to explode it
to "some" and Locale("en", "US"), then do the SELECT using that
extracted information. Removing the extension is trivial, but
extracting the locale information is tricky, as for that you will have
to assume that the file name doesn't use "_" for other purposes. If
you can't assume that, then it's indeed an ugly situation. (Anyway,
it's probable that TemplateLoader will have an extended version
sometimes later, then I will try to address this too. But that's in
the far and uncertain future, so...)