Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get locale in freemarker template

How can I get the current locale used in a freemarker template? I have seen implementation of <spring.message code />

I need this to do a conditional

<#if locale = DE >
.....
<#else>
....
</#if>
like image 804
pethel Avatar asked Nov 04 '13 10:11

pethel


People also ask

How do I comment in FreeMarker template?

Comments: Comments are similar to HTML comments, but they are delimited by <#-- and --> . Comments will be ignored by FreeMarker, and will not be written to the output.

What is FreeMarker template language?

FreeMarker is a template engine, written in Java, and maintained by the Apache Foundation.

What is eval in FreeMarker?

eval. This built-in evaluates a string as an FTL expression. For example "1+2"? eval returns the number 3. (To render a template that's stored in a string, use the interpret built-in instead.)

What is FreeMarker Template error?

FreeMarker template error appears in logs when attempting to add a Web Content Article to a page or search for it in the Search portlet. The issue only occurs when the Summary field is empty and when using the following line in the template.


1 Answers

As stated by the Freemarker documentation:

Special variables are variables defined by the FreeMarker engine itself. To access them, you use the .variable_name syntax

.locale: Returns the current value of the locale setting. This is a string, for example en_US. For more information about locale strings see the setting directive.

So to access the current local within a Freemarker template you would use

The current locale is: ${.locale}

To use it in a conditional statement as per your requirements, you would do:

<#if .locale == "DE">
   ...
<#else>
   ...
</#if>
like image 102
Rob Blake Avatar answered Sep 18 '22 13:09

Rob Blake