0

From documentation special variable .locale retrieves a String representation of the locale.

But how can the Locale be retrieved within freemarker markup?

If there is no built-in function I must pass the Locale to the template and pass it to each macro.

Simplified example:

Java Bean.class

public Locale getLocale();
public String getFoo(Locale locale);

Freemarker

Cannot use string locale!

<#setting locale="${bean.getLocale()}"> 
${bean.getFoo(.locale)}
djmj
  • 5,579
  • 5
  • 54
  • 92

2 Answers2

0

Freemarker's .locale is a String not a Locale.

Not sure what your getFoo() is doing, but maybe you need a Locale in there? This answer will help you build it if you need that: How to get Locale from its String representation in Java?

Community
  • 1
  • 1
sethmuss
  • 169
  • 9
0

I have added .locale_object in 2.3.21 for that (should be released on 2014-10-12).

Otherwise, if you have put the Locale into the data-model, why would you need to pass that to each macro? Macros see the data-model variables.

ddekany
  • 29,656
  • 4
  • 57
  • 64
  • good news :) Sorry i did not added it to the data-mode. Just setted it at the top of the markup. – djmj Oct 11 '14 at 22:49
  • 1
    Macros can see that variable too. Even if you are using `#import`-s and thus multiple namespaces, you can use `<#global myVar = myValue>`. – ddekany Oct 12 '14 at 07:36
  • Updated to new version and good to know global variable feature, Freemarker is very powerful! – djmj Oct 12 '14 at 22:57