Can Thymeleaf do localized template lookups like Freemarker? - spring

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:
tos_en_US.ftl
tos_en.ftl
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

I know it's a little bit too late but here is how I would attempt to solve this using the current version of Thymeleaf:
Presuming you have these 2 localised templates under path (from the template root) path/to/template:
localized_template_en.html
localized_template_ru.html
In your SomeController.class:
#GetMapping( "/locale_test" )
public String getLocalisedPage( Model model ) {
return "path/to/template/localized_template_"
+ LocaleContextHolder.getLocale().getLanguage();
}
As a result you will get the correct template given you use Spring localisation features (such as LocaleChangeInterceptor). If not - instead of using LocaleContextHolder implement your custom logic for postfix selection.

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.

Related

Spring Form Validation Implementation

My question is, what is the use of the springForm:form tag?
Why the prefix springform ??
Why can't I use my usual form tag??
Can someone tell me the difference?
I visited this link, but I'm unable to understand. Can someone explain it better?
A P it is just a custom tag reference.
<%# taglib prefix="springForm" uri="http://www.springframework.org/tags/form"%>
In above line one should informing that I am going to use spring tag using prefix springForm so that you will avail all spring tags support in your jsp.
And you can use normal form, but you won't avail the functionalities of spring tags.
Example path attribute is not supported in normal form,but it is supported in spring,so no need to map(binding),Spring will automatically map the value to mentioned path variable.

Change InternationalizationCheck from JSTL '<fmt:message key>' format?

In the SonarQube Web Plugin, the InternationalizationCheck rule searches for internationalization in the form of <fmt:message key=...>. Is there any way to change this? All my internationalization is in the format <f: message key=...>. I assume this was alterable in the 'attributes' section, but I haven't figured out what exactly that section is for, or what to put there.
For example, currently the rule searches for any internationalization in the form of
<fmt:message key="login.label.username" />
As described in the rule's noncompliant/compliant code example. However, in my application, internationalization takes the form of
<f:message key="login.label.username" />
Which is incorrectly labelled as an error by the rule. Nothing in the InternationalizationCheck's source code shows how it explicitly looks for the that format, so I need to find a way to make it accept my format as an acceptable line of code instead of giving it a false positive.
Is there any way to specify for the InternationalizationCheck to look for a different format of Internationalization, other than the JSTL taglib prefix of 'fmt'?

How to internationalize Javascript Content with Spring I18N?

How Can I Internationalize Strings located in Javascript File while Building a Spring MVC WebApp ?
I'm using <spring:message code="label1" /> to Internationalize some static Strings In the JSP Files, but what about error messages located in my js files for example? is there a way to resolve internationalization without including the plain js code in the jsp file ?
I thought about creating a list of values, one for each error, and proceed with ajax calls whenever the page is refreshed, so I can get the right message according the value of the lang variable, but is it wise to do so ?!
Include this piece of code in some common jsp , say header.jsp.
<script type="text/javascript">
function getSystemLocale() {
var systemLocale ='<%=RequestContextUtils.getLocale(request)%>';
return systemLocale;
}
function getResource(){
jQuery.i18n.properties({
name:'JS_Messages',
path: getContextPath()+'/resource-bundles/JS_Messages/',
mode:'both',
language:getSystemLocale(),
callback: function() {
jQuery.i18n.prop('error_message');
}
});
}
</script>
Structure will something as below. It will load all the properties files from the given path and search for the variable based on locale.
Remember the naming convention of file , ending with locale.
Need to include jquery.i18n.properties-min-1.0.9 for the same.
and simply in javascript you can access the key as alert(error_occured);

User/Form information in meta tags?

I have noticed on some MVC apps that certain infomation is stored in the meta tags like so:
<meta name="app-form-key" content="DMRNNNcRNkzzzghzNiM1N2DQwM5YNMyWzmkGUhULIj1ggEMwTyDYZAxkTTMMcuO1jRGD4MTO">
<meta name="app-userid" content="20031">
<meta name="app-username" content="driz">
What would the reasons be for doing this? Hoping to learn more about MVC and the practices of current sites that use this pattern.
For what its worth - this shouldn't be specific in any way to an MVC pattern. Nothing in MVC dictates data to be sent this way and considering it's not used in form posts, I can only imagine its some implementations way of storing page info that is not dependent on a form, since the meta tags can be enumerated via javascript easily.
I know its not a direct answer to your question, but - from a patterns standpoint - there is no specific reason to do this. It's not by any means part of say ASP.Net's MVC either.
The fact a form key is there, I'd be curious if its used for any client side validation/encryption or hash checking.

i18n in ExtJS & Spring MVC

I've searched a bit on the web but haven't really found a concrete solution to internationalize an application running ExtJS & Spring MVC. Currently (for testing), I define global javascript variables in the JSP's & assign the string literals using JSTL tags.
Has anyone had any success in coming up w/ a clear/clean/flexible solution on this?
Any thoughts are really appreciated. TIA.
There are 3 types of strings that you should translate in an application
Static strings on you page -> as Adam said using a template is a good idea. MVC can solve the problem. So you keep all your strings in a db or a properties file.
Error messages strings that are send to Ext J.S -> Here you have two solutions, either you allow Ext J.S to handle them , so this means that you have js files with the translations loaded
OR
on the MVC before you send the reply you add the translation on your response
Values from a db table that must be translated, e.g car parts. In this case you keep the key in you car_parts table and the translations in a properties file or on a translation table in db.
On my current app I perform the translation on the MVC level and I keep the translations on the db. The advantage are
1. The translation logic is one place the MVC and not split.
2. Your translations are in one place and whenever you make an error on a translation you do not have to make a new release, you just update the db OR you provide a form on the application admins to do it on their own. Backup of the translation table is required!!!
This is how it is done in my application, which is using Pylons, Ext JS and a template engine called Mako.
I'm treating JavaScript files as templates. I keep each class in a separate file and use a defined by me function require() to handle dependencies. My server processes these templates, handles dependencies, joins them together and compresses. Those templates contain calls to gettext-like function "_" e.g.:
window.setTitle("${_('My title')}");
These templates are regular Mako templates and can be preprocessed by a gettext-like utility to create .po files. The .po generator I'm using (Babel) puts positions of found strings as comments inside a .po file, which is convenient for me, because I know where the string appeared.
Take a look at the Spring MVC Ajax example frpm Spring.
if you use JSP views, you can use fmt tag library and resource bundles.
If you use Velocity views, use #springMessage('my.key') as explained in Spring documentation.

Resources