Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Thymeleaf do localized template lookups like Freemarker?

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:

  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 am now learning Thymeleaf and can't find any information about a similar functionality. I know that Thymeleaf uses localized message bundles to fill in th:text elements, but can it load localized versions of the template files?

Note: I'm using Spring Boot

like image 287
bernie Avatar asked Mar 29 '16 21:03

bernie


People also ask

Which is better FreeMarker or Thymeleaf?

FreeMarker has all the essential features to generate HTML for most of the cases, and it is easier to learn. So it is not a bad idea if you want to use it. However, Thymeleaf is the way to go if you want to add custom functionality to your templates.

Is Thymeleaf a template engine?

Thymeleaf is a modern server-side Java template engine for both web and standalone environments.

Is Thymeleaf server-side rendering?

Thymeleaf is a serve-side template engine for Java. It has built-in support for Spring framework and is widely used in Spring based Projects. In fact, Spring Boot itself has been promoting thymeleaf via several thymeleaf based projects and examples in its blog.

What is a template Thymeleaf?

Thymeleaf is a modern server-side Java template engine for both web and standalone environments, capable of processing HTML, XML, JavaScript, CSS and even plain text. The main goal of Thymeleaf is to provide an elegant and highly-maintainable way of creating templates.


1 Answers

Thymeleaf's behaviour the same as Spring 4 MVC Internationalization (I guess you using Thymeleaf with Spring??), it uses messages.properties to realize that. For example you have a template with #{hello} message:

<html xmlns="http://www.w3.org/1999/xhtml" 
  xmlns:th="http://www.thymeleaf.org">
<head>
    <title></title>
</head>
<body>
    <span th:text="#{hello}">
</body>

#{hello} text will be binded to the messages.properties proprty hello.

If you locale would be another, e.g. ru_RU you just add messages_ru_RU.properties and change the locale of your application.

After that, your message will be taken from localized properties file. Note that necessarily to have messages.properties file if you using localized messages file.

like image 194
sanluck Avatar answered Sep 21 '22 15:09

sanluck