Where does fmt:message get the locale from? - jstl

I need to know how the fmt:message tag of the JSTL gets the locale from. i dont want to use fmt:setLocale. I want to customize the locale resolver that fmt:message uses. I know implementing the Spring's LocaleResolver works for spring message source. Is there anything similar for the fmt:message tag?
Thanks in Advance.

Related

fallback messege file in spring boot is always "en"

I configured my web application as indicated in https://www.baeldung.com/spring-boot-internationalization#localeresolver
with setting Locale.ITALIAN as default locale for LocaleResolver bean.
I have two message files:
message.properties with italian messages
message_en.properties with messages in english
However, labels defined in messages_en.properties, when exists. For example with setting locale via lang=es request parameter, messages in english are shown.
The expected behaviour, if I understand, should be that if lang=en, message_en.properties should be used, where as for all other languages messages in message.properties should be used.
Suggestions?
If you use the latest version of Spring Boot (2.5.3 at the moment), the tutorial isn't as up-to-date. For example, with the latest Spring you must do additional, but simple config to override LocaleResolver bean.
Depending of your implementation, you may need to add in the application.properties file the line spring.messages.fallback-to-system-locale=false or if you overridden the "messageSource" bean, you must set messageSource.setFallbackToSystemLocale(false); in your own bean.
This way the app should work as expected, with all languages except EN using message.properties.

Spring boot and i18n, not using correct language

I use spring boot 2, thymeleaf and spring security.
In spring boot, the locale seem to be took from the browser language. Surely sent via Accept-Language
Is it possible to override that? Maybe is not neccessary...
What i try to do is to take lang value from user (CustomUserDetail) and use this value to use this value to display correct value from my thymeleaf page.
I created a class who implements AuthenticationSuccessHandler, it take lang value from user .
I do
LocaleContextHolder.setLocale(StringUtils.parseLocaleString(user.getLang()));
My browser local is in english, my user lang is french.
After user connect successfuly, web page continue to be displayed in english.
In my application.properties, I have
spring.messages.fallback-to-system-locale=false

How would I escape HTML in SpringBoot's #PathVariable?

I am using Spring 4.2.5 and I just want to escape the HTML in the URL if present. Using JSON deserializer is affecting the values of RequestBody/Param as well, but I don't want other values to be changed. What is the correct way to do this?
Apache's URL builder escapes html in parameters automatically:
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URIBuilder.html
If you're talking about html in the spring Rest controller, a filter or Jackson configuration should solve your problem.
I guess I need to use PropertyEditorSupport class to escape the HTML in the PathVariable and implement JSON deserializer for the RequestBody.

Is there a way to force locale on a given spring message?

<spring:message code="someMessageCode" />
This works with the user locale (browser locale). I have a need to force locale on a particular message. I can handle this with a scriplet but I was wondering if there is a way we can tell Spring to use a specific locale for a particular messsage.
I don't think it is possible with spring messages macro, but you can create a MessageSourceAccessor and use it.
Ex:
MessageSourceAccessor messageResolver = new MessageSourceAccessor(messageSource, locale);
Pass it to freemarker, then in freemarker
${messageResolver.getMessage("someMessageCode")}

Accessing proper resource bundle in Spring Framework

I am trying to access a resource bundle using Spring framework (WebFlow). A messages.properties file and accordingly messages_ar_AE.properties file are kept in the classpath from where the Spring Framework access the resource bundle.
The code in invoked from a xhtml file using the JSTL resourceBundle attribute.
<myCustom:includedInSetValidator set="5.0, 5.0.1, 5.1"
validationMessage="#{resourceBundle['jboss.version.error']}" />
But irrespective of locale, the "#{resourceBundle['jboss.version.error']}" always fetches the default text, i.e; from English;
As I learned from some forums I got an hint that I need to handle this using LocaleChangeInterceptor or some other predefined classes. Once the Spring Locale is set, the proper resource bundle will be loaded by default, and hence solving my problem.
I need a way to change the Spring Framework Locale programatically to set the Locale. How do I achieve this programatically ?
Thanks.
Reached the solution for the problem.
Continuing from my question, when Spring Framework encounters a JSTL expression like "#{resourceBundle['jboss.version.error']}" by default it looks for message.properties file in the classpath, unless a resource bundle is defined explicitly.
When trying to fetch the proper resource bundle, the framework looks at the at the locale it is set to. As the locale of Spring Framework was not set in my case, it was not fetching me the expected resource bundle. Out of available options i chose Spring LocaleResolver
I modified existing JSF Custom ViewHandler in my application, where I added code to set the locale of Spring Framework.
public Locale calculateLocale(FacesContext arg0) {
HttpServletRequest request = (HttpServletRequest)arg0.getExternalContext().getRequest();
HttpServletResponse response = (HttpServletResponse)arg0.getExternalContext().getResponse();
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
localeResolver.setLocale(request, response, **setYourLocaleHere**);
}
The story just doesn't end here, setting the locale in locale resolver this way would throw the error:
Cannot change HTTP accept header – use a different locale resolution strategy
Refer Cannot change HTTP accept header error
To overcome this, one should include
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
in the Spring configuration file.
And now the desired locale of the Spring Framework is set.
There could possibly a better solution than what I did. One can also suggest their solutions if any.
Thanks.
You could do this multiple ways as outlined here in the doc.

Resources