Commit c927f344 authored by Ubbo Veentjer's avatar Ubbo Veentjer
Browse files

language switch link works

parent 41c151d7
Pipeline #123606 passed with stages
in 13 minutes and 2 seconds
......@@ -7,7 +7,7 @@ import java.util.HashMap;
* Keep track of language map and its name string for use with jsp controllers.
*
* This class keeps the compatibility to jsp code accessing an i18n hashmap,
* which originated in porting JavaServerFaces el-i18n functionality.
* which originated in porting Java ServerFaces EL #i18n functionality.
*
* @author Ubbo Veentjer
*/
......
package info.textgrid.rep;
package info.textgrid.rep.i18n;
import java.util.Locale;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
// config contains all the int-stuff now, compare
// https://www.baeldung.com/spring-boot-internationalization
// https://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/session-locale-resolver.html
// https://phrase.com/blog/posts/spring-boot-internationalization/
/**
* This configuration handles language switching with spring.
* Language will be first determined by the http "Accept-Language"
* request header from the browser. Using information from the
* http request header works because of not Setting the
* DefaultLocale on the SessionLocaleResolver.
*
* The parameter ?lang is used to change language manually,
* which causes a cookie to be set which spring uses on
* further requests.
*
* @author Ubbo Veentjer
*/
@Configuration
public class TgRepConfiguration implements WebMvcConfigurer {
public class I18NConfiguration implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
......@@ -33,7 +39,6 @@ public class TgRepConfiguration implements WebMvcConfigurer {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
//slr.setDefaultLocale(Locale.ENGLISH);
return slr;
}
......
<%@ taglib prefix="my" tagdir="/WEB-INF/tags" %>
<nav class="tg topbox">
<button class="tg topbox_toggle-nav"><span class="sr-only">${i18n['navigation']}</span></button>
<ul>
......@@ -31,24 +33,14 @@
<!-- #end -->
</li>
<li class="tg topbox_language">
<!--
#if ($language == 'de' || $language == 'de_DE')
#set ($url = '/en' + $theme_display.getURLCurrent().replace('/de/', '/').replace('/de_DE/', '/'))
<a class="tg topbox_language" href="$url"><span class="sr-only">Switch language to </span>English</a>
#else
#set ($url = '/de' + $theme_display.getURLCurrent().replace('/en/', '/').replace('/en_US/', '/'))
<a class="tg topbox_language" href="$url"><span class="sr-only">Sprache ndern nach </span>Deutsch</a>
#end-->
<c:choose>
<c:when test="${language == 'de'}">
<a class="tg topbox_language" href="$url"><span class="sr-only">Switch language to </span>English</a>
<a class="tg topbox_language" href="<my:replaceParam name='lang' value='en' />"><span class="sr-only">Switch language to </span>English</a>
</c:when>
<c:otherwise>
<a class="tg topbox_language" href="$url"><span class="sr-only">Sprache ndern nach </span>Deutsch</a>
<a class="tg topbox_language" href="<my:replaceParam name='lang' value='de' />"><span class="sr-only">Sprache ndern nach </span>Deutsch</a>
</c:otherwise>
</c:choose>
</li>
</ul>
</nav>
\ No newline at end of file
<%-- https://stackoverflow.com/a/33246991 --%>
<%@ tag pageEncoding="UTF-8" trimDirectiveWhitespaces="true" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ attribute name="name" required="true" type="java.lang.String" %>
<%@ attribute name="value" required="true" type="java.lang.String" %>
<c:url value="">
<%--
replaces or adds a param to a URL
if $name in query then replace its value with $value.
copies existing
--%>
<c:forEach items="${paramValues}" var="p">
<c:choose>
<c:when test="${p.key == name}">
<c:param name="${name}" value="${value}"/>
</c:when>
<c:otherwise>
<c:forEach items="${p.value}" var="val">
<c:param name="${p.key}" value="${val}"/>
</c:forEach>
</c:otherwise>
</c:choose>
</c:forEach>
<%-- if $name not in query, then add --%>
<c:if test="${empty param[name] }">
<c:param name="${name}" value="${value}"/>
</c:if>
</c:url>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment