question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Localized template lookups (Freemarker style)

See original GitHub issue

Freemarker (by default) uses the locale to build the file names it looks for when loading and including templates. For example, loading tos.ftl (the template) with the en_US locale would look for these template files in order and use the first one it finds:

  1. tos_en_US.ftl
  2. tos_en.ftl
  3. tos.ftl

This can be useful to translate whole pages when the pages are completely different between different languages. For example, a “Terms of Service” page might be mostly static so different languages would have completely different content. In this case, it is a hassle to externalize the whole content to messages loaded from message bundles. i know it is possible to use th:if which toggle sections based on the current locale but it’s not as easy to navigate between different languages using this.

Could such a lookup mechanism be added to Thymeleaf?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:5
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
danielfernandezcommented, May 25, 2016

With the current Thymeleaf 3.0 APIs, an implementation of ITemplateResolver could be created that resolved a different template for each locale by means of specifying such locale in the templateResolutionAttributes map that can be passed as a parameter to the ITemplateEngine.process(...) methods.

However, this would mean you’d need to actually pass the locale twice: once in the IContext and another time in the templateResolutionAttributes, and of course make sure it is the same locale.

In 3.0 there is probably no better way of doing this, because template resolution is a separate phase from that of actual template process, and the results of template resolution might be cached without explicitly depending on the locale as a part of the cache key — though the templateResolutionAttributes will be a part of such cache key, so you should be fine using it.

I’m leaving this in the Wish List so that we can evaluate the possibility of creating some kind of locale-oriented specific template resolution functionalities in next versions.

1reaction
ivan-gammelcommented, Jul 24, 2017

@berniegp I have this feature working with current stable Thymeleaf 3, being implemented this way: https://github.com/resource4j/resource4j/blob/master/integration/thymeleaf3/src/main/java/com/github/resource4j/thymeleaf3/Resource4jTemplateEngine.java

You can try this solution as a workaround with your own template resolver.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TemplateLookupStrategy (FreeMarker 2.3.31 API)
A template lookup strategy meant to operate solely with template names, ... Through an example: Assuming localized lookup is enabled and that a...
Read more >
include - Apache FreeMarker Manual
If you specifying more asterisks, the template won't be found. Localized lookup. A locale is a language and an optional country or dialect...
Read more >
Settings - Apache FreeMarker Manual
Settings are named values that influence the behavior of FreeMarker. Examples of settings are: locale , number_format , default_encoding ...
Read more >
TemplateLookupContext (FreeMarker 2.3.31 API)
equals(Object) and Object.hashCode() method. java.util.Locale, getTemplateLocale(). null if localized lookup is ...
Read more >
Configuration (FreeMarker 2.3.31 API)
Enables/disables localized template lookup. void, setLogTemplateExceptions(boolean value). Specifies if TemplateException -s thrown by template processing are ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found