I'm using Spring MVC LocaleChangeInterceptor to handle change of locale. I need 2 simple links to switch the current page between English and French while keeping any existing parameter of the current URL.
The solution I've came up with looks rather ugly. A c:url tag with an empty value to link to the current page and a loop to append any existing parameter except for the language parameter that I don't want to see twice. So the code is as follow.
<c:url value="" var="englishURL">
<c:forEach items="${param}" var="currentParam">
<c:if test="${currentParam.key != 'siteLanguage'}">
<c:param name="${currentParam.key}" value="${currentParam.value}"/>
</c:if>
</c:forEach>
<c:param name="siteLanguage" value="en"/>
</c:url>
<c:url value="" var="frenchURL">
<c:forEach items="${param}" var="currentParam">
<c:if test="${currentParam.key != 'siteLanguage'}">
<c:param name="${currentParam.key}" value="${currentParam.value}"/>
</c:if>
</c:forEach>
<c:param name="siteLanguage" value="fr"/>
</c:url>
<a href="${englishURL}">English</a> / <a href="${frenchURL}">Français</a>
Does someone have a less ugly way to create these links? I know I can create a custom tag to avoid the duplication but I feel that's a very small improvement since I need to declare the tag and all.