wi2ard
wi2ard

Reputation: 1555

How to style parts of i18n messages when using thymeleaf

I'm not sure this is the right place to ask this. I would like to know how best to style parts of messages from l10n properties files. For example, my client want this message and formatting in a help window:

This is a self-assessment and comparison application.

Simplest solution would be to include the HTML tags in the messages.properties entry for this label. The problem with that is that the 40 translators that will process the messages.properties are bound to make mistakes like deleting the <, translating the attributes or styles of the HTML markup etc. Also it makes maintaining the markup and styling difficult for the devs.

Any better way to do this?

Upvotes: 1

Views: 923

Answers (1)

riddle_me_this
riddle_me_this

Reputation: 9155

The solution I've seen typically done just uses th:utext with HTML tags in the .properties files. I would opine it does create a maintenance hassle as you mention and should be kept to a minimum.

One workaround is to create separate strings in some cases, like:

<span th:text=#{thisIsA}>This is a </span><strong><span th:text="#{selfAssessment}">self-assessment</span></strong>

However, this is error-prone since certain languages may change the order of the words. So that's not a great option.

If the HTML tags specifically are an issue, another way albeit somewhat ugly could be:

thisIsASelfAssessment=This is a {0}self-assessment{1}.

Or even

thisIsA=This is a {0}.
selfAssessment=self-assessment

But that might be confusing for the next developer reading it and may introduce the same issue you have with the 40 translators looking at it since you have curly braces. It also all becomes very tedious and generates more lines.

So in the end, you're likely best going with the simplest solution of utext.

Project-wise, you could have the initial translation done without the markup and add the markup in after they are done with a first pass at translating it. The issue may arise in the future when you need to change strings, but doing this would minimize some headache. It could make sense to keep these strings in a separate block in the .properties file so you can target them later.

Good question as I've had this issue myself.

Upvotes: 1

Related Questions