10
votes

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

1
I opened a feature request for this: github.com/thymeleaf/thymeleaf/issues/497 - bernie

1 Answers

1
votes

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.