Reputation: 337
I have a traditional Spring4/Thymeleaf i18n application I switch the locale easily with classic
org.springframework.web.servlet.i18n.LocaleChangeInterceptor
and
org.springframework.web.servlet.i18n.CookieLocaleResolver
When switching I always send to the server /home?lang=fr. It works fine. But I need a more complex behaviour. What I need to do is to preserve the current page while switching the locale.
I found a half-working solution with this thymeleaf snippet:
th:with="currentUrl=(${#httpServletRequest.pathInfo + '?' + #strings.defaultString(#httpServletRequest.queryString, '')})
The problem is I need to implement myself many corner cases:
Does anybody know how to manage this case with native Spring or Thymeleaf tools? Or I need to write my own processor for Thymeleaf?
Upvotes: 3
Views: 5731
Reputation: 2148
The easiest solution is concatenating "requestURI" and "queryString". The drawback of this method is that if you click multiple times on the same link the parameter just gets added over and over again.
A workaround is to write a function that "cleans" the url before adding the parameter
For code examples, take a look at this question: Thymeleaf: add parameter to current url
Upvotes: 1
Reputation: 498
I use this in my projects & it works fine. I don't specifically redirect to any URL when locale change.
@Bean
public LocaleResolver localeResolver()
{
Locale defaultLocale = new Locale( env.getProperty( Constants.DEFAULT_LOCALE ) );
CookieLocaleResolver clr = new CookieLocaleResolver();
clr.setDefaultLocale( defaultLocale );
return clr;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor()
{
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName( "lang" );
return lci;
}
Upvotes: 0