Messages

Thymeleaf zur Internationalisierung (oder zum Auslagern von Strings) nutzen

Um WebAnwendungen für ein internationales Publikum zu entwickeltn werden oft .properties-Files benutzt, welche eine Liste mit Strings (= Messages) enthalten.

Zugriff auf ein Messages-File

<p th:text="#{home.welcome}">Welcome to our grocery store!</p>

Wo muss das File liegen?

However, we have not specified a message resolver to our Template Engine during initialization, and that means that our application is using the Standard Message Resolver, implemented by class org.thymeleaf.messageresolver.StandardMessageResolver . This standard message resolver expects to find messages for /WEB-INF/templates/home.html in .properties files in the same folder and with the same name as the template, like:

  • /WEB-INF/templates/home_en.properties for English texts.
  • /WEB-INF/templates/home_es.properties for Spanish language texts.
  • /WEB-INF/templates/home_pt_BR.properties for Portuguese (Brazil) language texts.
  • /WEB-INF/templates/home.properties for default texts (if locale is not matched)

Messages mit HTML-Code

Problem: Messages werden automatisch escapted

Beispiel: Der folgende Zugriff auf eine Message

home.welcome=Welcome to our <b>fantastic</b> grocery store!

erzeugt das folgende Resultat:

<p>Welcome to our &lt;b&gt;fantastic&lt;/b&gt; grocery store!</p>

This is the default behaviour of the th:text attribute. If we want Thymeleaf to respect our XHTML tags and not escape them, we will have to use a different attribute: th:utext (for “unescaped text”):

<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>

results matching ""

    No results matching ""