Ruben Trancoso
2013-08-21 09:23:04 UTC
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:
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.
.
// for reference
if (localizedLookup) {
int lastDot = name.lastIndexOf('.');
String prefix = lastDot == -1 ? name : name.substring(0,
lastDot);
String suffix = lastDot == -1 ? "" : name.substring(lastDot);
String localeName = LOCALE_SEPARATOR + locale.toString();
StringBuffer buf = new StringBuffer(name.length() +
localeName.length());
buf.append(prefix);
for (;;)
{
buf.setLength(prefix.length());
String path =
buf.append(localeName).append(suffix).toString();
Object templateSource = acquireTemplateSource(path);
thanks
Ruben
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:
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.
.
// for reference
if (localizedLookup) {
int lastDot = name.lastIndexOf('.');
String prefix = lastDot == -1 ? name : name.substring(0,
lastDot);
String suffix = lastDot == -1 ? "" : name.substring(lastDot);
String localeName = LOCALE_SEPARATOR + locale.toString();
StringBuffer buf = new StringBuffer(name.length() +
localeName.length());
buf.append(prefix);
for (;;)
{
buf.setLength(prefix.length());
String path =
buf.append(localeName).append(suffix).toString();
Object templateSource = acquireTemplateSource(path);
thanks
Ruben