jmtupin3923
(jmtupin@tchooks.com jean marc)
March 3, 2018, 7:55pm
#1
I have 2 langages and 2 messages_xx_xx.properties files. How can i get a localized message from the portal module page with thymeleaf?
In the controller with
@Inject
private Messages messages;
I can get the localized text but how can I get one in a HTML page?
for now I get this : ??warningMessage_fr_FR?? when I use
someText
I must miss something
Thanks
artamonov
(Yuriy Artamonov)
March 12, 2018, 7:50pm
#3
Hi,
Unfortunately, it is not supported out-of-the-box.
You could enable messages.properties localization in your portal-spring-dispatcher.xml in the following way:
<bean id="thymeleafMessageSource" class="com.company.demo.portal.sys.ThymeleafMessageSource"/>
<bean id="thymeleafTemplateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="thymeleafTemplateResolver"/>
<property name="templateEngineMessageSource" ref="thymeleafMessageSource"/>
<property name="additionalDialects">
<set>
<bean class="org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect"/>
</set>
</property>
</bean>
<bean id="thymeleafViewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="thymeleafTemplateEngine" />
<property name="characterEncoding" value="UTF-8"/>
</bean>
And create ThymeleafMessageSource
class:
public class ThymeleafMessageSource implements MessageSource {
@Inject
private Messages messages;
@Override
public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
if (args != null && args.length > 0) {
return messages.formatMainMessage(code, args, locale);
}
return messages.getMainMessage(code, locale);
}
@Override
public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
if (args != null && args.length > 0) {
return messages.formatMainMessage(code, args, locale);
}
return messages.getMainMessage(code, locale);
}
@Override
public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
throw new NoSuchMessageException("Unsupported message resolving mechanism");
}
}
Then, you will be able to use the standard thymeleaf localization syntax:
<h1 th:text="#{page.head}"></h1>
See this repository for the complete example: GitHub - cuba-labs/thymeleaf-l10n
We will implement this feature in the upcoming release 6.9: https://youtrack.cuba-platform.com/issue/PL-10533
xavier
(Xavier Lorente Picó)
May 23, 2022, 9:54am
#5
Hi, Yuriy
We have to use localized text in Web portal and we’ve just entered this post https://forum.cuba-platform.com/t/portal-component/16303
Looking for a solution in the forum I’ve just arrived to your answer, made in 2018 and I want to know how do you implement this feature from version 6.9, because we’re working with Cuba-Platform v 7.2.18.
Thanks in advance.
xavier
(Xavier Lorente Picó)
May 23, 2022, 10:28am
#6
Hi,
Finally, I’ve just discovered that Localized messages for the https://www.thymeleaf.org/[Thymeleaf] templates in the portal module can also be obtained by the message key from the main message pack of the portal module.
Regards,