1

I have a problem about adding the language part into the existing URL after changing Language in Spring Boot

Here is my URL listing all brands by name in ascending order.

admin/brands/page/1?sortField=name&sortDir=asc

When I change the language, the URL has occured as shown below.

admin/brands/page/1?lang=fr

Here is my language change code snippets in thymeleaf shown below.

<li class="nav-item">
    <a class="nav-link" th:href="@{?lang=en}">
        <img th:src="@{/images/english.png}" width="30">
    </a>
</li>
                
<li class="nav-item">
    <a class="nav-link" th:href="@{?lang=fr}">
        <img th:src="@{/images/french.png}" width="30">
    </a>
</li>

I want to get this url admin/brands/page/1?sortField=name&sortDir=asc&lang=fr

How can I do that?

S.N
  • 2,157
  • 3
  • 29
  • 78

1 Answers1

0

Here is my solution shown below.

<li class="nav-item">
    <a class="nav-link" 
       th:with="urlBuilder=${T(org.springframework.web.servlet.support.ServletUriComponentsBuilder).fromCurrentRequest()}"
       th:href="${urlBuilder.replaceQueryParam('lang', 'en').toUriString()}">
        <img th:src="@{/images/english.png}" width="30">
    </a>
</li>
                
<li class="nav-item">
    <a class="nav-link" 
       th:with="urlBuilder=${T(org.springframework.web.servlet.support.ServletUriComponentsBuilder).fromCurrentRequest()}"
       th:href="${urlBuilder.replaceQueryParam('lang', 'tr').toUriString()}">
         <img th:src="@{/images/turkish.png}" width="30">
    </a>
</li>
S.N
  • 2,157
  • 3
  • 29
  • 78