3

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:

  • when there is already any query parameter
  • if there is a lang=en param,
  • etc.

Does anybody know how to manage this case with native Spring or Thymeleaf tools? Or I need to write my own processor for Thymeleaf?

Yev
  • 337
  • 4
  • 19
  • Do you have a sample application where you are testing this functionality out? Would be nice to be able to work against an already setup project to try and find a solution. I am guessing your language selector shows up as a header or footer on each page. I would look to setup some interceptor that adds a value to the Model which contains the url's for the current page. Then the template is just responsible for outputting those language specific urls for the page. – Shawn Clark Aug 12 '16 at 07:44
  • This could be the answer also: https://stackoverflow.com/questions/23466130/spring-mvc-how-do-i-get-current-url-in-thymeleaf/41343229#41343229 – David Oct 24 '19 at 12:04

2 Answers2

1

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

Community
  • 1
  • 1
JohanB
  • 2,068
  • 1
  • 15
  • 15
  • Thank you works like a charm. Just one precision - there is forgotten **.build().** function call in referenced ServletUriComponentsBuilder.fromCurrentRequest().replaceQueryParam(param).build().toUriString(); – Yev Aug 12 '16 at 18:10
  • @Yev, no build() method call is missing because according to documentation : [toUriString()](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/util/UriComponentsBuilder.html#toUriString--) `is a shortcut method which combines calls to build(), then UriComponents.encode() and finally UriComponents.toUriString().` – Ortomala Lokni Sep 21 '16 at 12:26
0

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;
}
nmy
  • 488
  • 8
  • 21
  • When locale is changed the user should stay on the same page. Your code is not appropriate. – Yev Aug 08 '16 at 06:36
  • How are you going to change the locale? Isn't it using the 'lang' query parameter? – nmy Aug 08 '16 at 06:46
  • I'm using the classical approach, just as you did, but my question is how to dynamically generate current page link with lang param. – Yev Aug 08 '16 at 13:43