Skip to content

Commit

Permalink
fixed Asqatasun#188 - Webapp: refactor languages switcher
Browse files Browse the repository at this point in the history
  • Loading branch information
dzc34 committed Oct 3, 2018
1 parent 41b4ef0 commit ae7ce3d
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 124 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ none
- [#183 - Added config. informations in the logs when starting the webapp](https://github.com/Asqatasun/Contrast-Finder/issues/183)
- [#185 - Added new configuration option for logging colors results](https://github.com/Asqatasun/Contrast-Finder/issues/185)
- [#187 - Added new configuration options: available languages and default language](https://github.com/Asqatasun/Contrast-Finder/issues/187)
- [#188 - Webapp: languages switcher now depends on the configuration options](https://github.com/Asqatasun/Contrast-Finder/issues/188)
- [#186 - Webapp: added new page to provide build ID (commit hash)](https://github.com/Asqatasun/Contrast-Finder/issues/186)

### Refactoring
Expand Down
51 changes: 51 additions & 0 deletions docker/SNAPSHOT-local/test_config-file/60.languages/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
FROM asqatasun/contrast-finder:pre-requisites_Ubuntu-18.04_tomcat-8.5

# #### usage ######################################################
#
# --- Building this docker image
# docker build -t asqatasun/contrast-finder:SNAPSHOT_local .
#
# --- Launch a container
# docker run -p 127.0.0.1:8087:8080 --name contrast.finder -d asqatasun/contrast-finder:SNAPSHOT_local
#
# --- Playing with this docker image
# docker exec -ti contrast.finder /bin/cat /softwares-version.txt
# docker exec -ti contrast.finder /bin/bash
# docker logs -f contrast.finder
#
# #### FROM ######################################################
# asqatasun/contrast-finder:pre-requisites_Ubuntu-18.04_tomcat-8.5 https://github.com/Asqatasun/Contrast-Finder/blob/master/docker/pre-requisites/pre-requisites_Ubuntu-18.04_tomcat-8.5/Dockerfile
# |-- ubuntu:18.04 https://github.com/tianon/docker-brew-ubuntu-core/blob/dist-amd64/bionic/Dockerfile
###################################################################

# environment variables
ENV CONTRAST_FINDER_RELEASE="0.9.3-SNAPSHOT"
ENV LANGUAGES="en,fr,es"

# Add contrast-finder .war
ADD contrast-finder-webapp_${CONTRAST_FINDER_RELEASE}.tar.gz /root

# Install contrast-finder
RUN cd /root/contrast-*/install/ && \
mv -v contrast-*.war contrast-finder.war && \
mv -v contrast-*.war /var/lib/tomcat8/webapps && \
rm -rf /root/contrast-* && \
echo "languages=${LANGUAGES}" >> ${CONF_FILE} && \
echo ${CONTRAST_FINDER_RELEASE} >> ${INFO_FILE} && \
echo "\n--- ${CONF_FILE} ---" >> ${INFO_FILE} && \
cat ${CONF_FILE} >> ${INFO_FILE} && \
echo "\n--- Logs -----------" >> ${INFO_FILE} && \
echo "${TOMCAT_LOG}" >> ${INFO_FILE} && \
echo "${LOG_FILE}" >> ${INFO_FILE} && \
echo "--------------------" >> ${INFO_FILE} && \
cat ${INFO_FILE} > ${LOG_FILE} && echo "-- Install: Ok"

# Health Check of the Docker Container
# ----> see asqatasun/contrast-finder:pre-requisites_Ubuntu-18.04_tomcat-8.5

CMD service tomcat8 start ; \
tail -f -n 50 ${TOMCAT_LOG} \
${LOG_FILE}



Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,22 @@ public abstract class AbstractController {
*/
private boolean addConfigToLogs;

/**
* ordered languages list
* see: this.languages
*/
private List<String> orderedLanguagesList;

/**
* available languages
* see: this.languages
*/
private Map<String, String> availableLanguages;


/**
* ordered languages list
* see: this.languages
* texts of switch in language target
*/
private List<String> orderedLanguagesList;
private Map<String, String> textsOfSwitchLanguage;

/**
* piwik analytics key
Expand Down Expand Up @@ -104,11 +108,11 @@ public abstract class AbstractController {
* - comma-separated language codes (ISO 639-1: two-letter codes)
* - order will be the same in the webapp menu
*
* currently : "en,fr,es,pt,ko"
* currently : "en,es,fr,pt,ko"
* can be override in the following file:
* /etc/contrast-finder/contrast-finder.conf
*/
@Value("${languages:en,fr,es,pt,ko}")
@Value("${languages:en,es,fr,pt,ko}")
private String languages;

/**
Expand Down Expand Up @@ -181,6 +185,7 @@ protected void addCommonViewData(final Model model, String pageName) {
model.addAttribute("defaultAlgorithm", defaultAlgorithm);
model.addAttribute("availableLanguages", availableLanguages);
model.addAttribute("orderedLanguagesList", orderedLanguagesList);
model.addAttribute("textsOfSwitchLanguage", textsOfSwitchLanguage);
model.addAttribute("defaultLanguage", defaultLanguage);
model.addAttribute("pageName", pageName);
}
Expand Down Expand Up @@ -248,8 +253,19 @@ public void setLoadAvailableLanguages(String loadAvailableLanguages) {
// LOGGER.debug("locale.getDisplayLanguage() : " + locale.getDisplayLanguage());
// LOGGER.debug("locale.getDisplayLanguage(locale): " + locale.getDisplayLanguage(locale));
}
this.availableLanguages = languagesMap;
this.orderedLanguagesList = languagesList;

// @@@TODO using .properties file
Map<String, String> switchLang = new HashMap<>();
switchLang.put("en", "Switch to english");
switchLang.put("es", "Cambiar a español");
switchLang.put("fr", "Passer en français");
switchLang.put("pt", "em Português");
switchLang.put("ko", "한국어로 전환");

this.textsOfSwitchLanguage = switchLang;
this.availableLanguages = languagesMap;
this.orderedLanguagesList = languagesList;
// LOGGER.debug(textsOfSwitchLanguage.toString());
LOGGER.debug(availableLanguages.toString());
LOGGER.debug(orderedLanguagesList.toString());
}
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/main/resources/conf/contrast-finder.conf.dist
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@
# - comma-separated language codes (ISO 639-1: two-letter codes)
# - order will be the same in the webapp menu
###############################################
# languages=en,fr,es,pt,ko
# languages=en,es,fr,pt,ko
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ page.result-error.titleTag=Error
page.about.titleTag=About Contrast-Finder
page.about.metaDescription=
page.about.title=About Contrast-Finder
page.about.fileList.title=Static files
page.about.fileList.title=Additional text files
page.about.build.title=Build info
page.about.availableLanguages=Available languages
### END WIP - Do not translate ############################
info.about.title=What is <em>Contrast Finder</em>?
info.about=<p> \
Expand Down
68 changes: 51 additions & 17 deletions webapp/src/main/webapp/WEB-INF/pages/about.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,48 @@
<div class="help">
<h1><fmt:message key="page.about.title"/></h1>

<h2><fmt:message key="page.about.fileList.title"/></h2>
<%-- TEST language code is not available --%>
<c:set var="isAvalaibleLanguage" value="false" scope="page" />
<c:forEach var="langCode" items="${orderedLanguagesList}">
<c:choose>
<c:when test="${lang == langCode}">
<c:set var="isAvalaibleLanguage" value="true" scope="page" />
</c:when>
</c:choose>
</c:forEach>
<c:choose>
<c:when test="${isAvalaibleLanguage == 'false'}">
<%-- <hr>NOT avalaible language <hr> --%>
<%-- <c:redirect url="/?lang=${defaultLanguage}"/>--%>
<%-- <%
response.setStatus(301);
response.setHeader( "Location", "./?lang=en" );
response.setHeader( "Connection", "close" );
%> --%>
</c:when>
</c:choose>



<h2><fmt:message key="page.about.availableLanguages"/></h2>
<ul>
<li><a href="humans.txt">humans.txt</a> </li>
<li><a href="contribute.json">contribute.json</a> </li>
<li><a href=".well-known/security.txt">security.txt</a> </li>
<c:forEach var="langCode" items="${orderedLanguagesList}">
<li lang="${langCode}">
<a hreflang="${langCode}"
title="Contrast-Finder in english"
href="./?lang=${langCode}">${langCode} - ${fn:toLowerCase(availableLanguages[langCode])}</a>
</li>
</c:forEach>
</ul>
<%-- <c:forEach var="langCode" items="${orderedLanguagesList}">
${langCode} - ${availableLanguages[langCode]} <br>
</c:forEach> <hr> --%>
<%-- <c:forEach items="${availableLanguages}" var="entry">
${entry.key} - ${entry.value} <br>
</c:forEach> <hr> --%>



<% //Get version of application
java.util.Properties prop = new java.util.Properties();
prop.load(getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF"));
Expand Down Expand Up @@ -54,30 +90,28 @@

<h2><fmt:message key="page.about.build.title"/></h2>
<ul>
<li>version: <%=appliVersion%> </li>
<li>version: <strong><%=appliVersion%></strong> </li>
<li>git branch: <%=gitBranch%> </li>
<li>git commit: <a href="<%=gitURL%>"><%=gitCommit%></a> </li>
<li>Build:
<ul>
<li>date: <%=appliBuildDate%> </li>
<li>ID: <%=appliBuildID%> </li>
<li>Platform: <%=appliBuildOs%> <%=appliBuildArch%>,
Java <%=appliBuildJava%>,
Maven <%=appliBuildMaven%> </li>
Java <%=appliBuildJava%>,
Maven <strong><%=appliBuildMaven%></strong>
</li>
</ul>
</li>
</ul>

<hr>
<c:forEach var="langCode" items="${orderedLanguagesList}">
${langCode} -
${availableLanguages[langCode]}
<br>
</c:forEach>
<hr>
<c:forEach items="${availableLanguages}" var="entry">
${entry.key} - ${entry.value}<br>
</c:forEach>
<h2><fmt:message key="page.about.fileList.title"/></h2>
<ul>
<li><a href="contribute.json">contribute.json</a> </li>
<li><a href="humans.txt">humans.txt</a> </li>
<li><a href="robots.txt">robots.txt</a> </li>
<li><a href=".well-known/security.txt">security.txt</a> </li>
</ul>

</div>
</div> <!-- class="container' -->
Expand Down
142 changes: 45 additions & 97 deletions webapp/src/main/webapp/WEB-INF/template/header.jspf
Original file line number Diff line number Diff line change
Expand Up @@ -3,104 +3,52 @@
<%@page pageEncoding="UTF-8"%>

<header id="header" class="page-header">
<c:set var="properQueryString" scope="page" value="${fn:replace(pageContext.request.queryString, '&', '&amp;')}"/>
<c:choose>
<c:when test="${not empty pageContext.request.queryString}">
<c:choose>
<c:when test="${fn:contains(pageContext.request.queryString, 'lang=en')}">
<c:set var="frUrl" scope="request" value="?${fn:replace(properQueryString,'lang=en','lang=fr')}" />
<c:set var="enUrl" scope="request" value="?${properQueryString}"/>
<c:set var="esUrl" scope="request" value="?${fn:replace(properQueryString,'lang=en','lang=es')}" />
<c:set var="koUrl" scope="request" value="?${fn:replace(properQueryString,'lang=en','lang=ko')}" />
<c:set var="ptUrl" scope="request" value="?${fn:replace(properQueryString,'lang=en','lang=pt')}" />
</c:when>
<c:when test="${fn:contains(pageContext.request.queryString, 'lang=es')}">
<c:set var="frUrl" scope="request" value="?${fn:replace(properQueryString,'lang=es','lang=fr')}" />
<c:set var="enUrl" scope="request" value="?${fn:replace(properQueryString,'lang=es','lang=en')}" />
<c:set var="esUrl" scope="request" value="?${properQueryString}"/>
<c:set var="koUrl" scope="request" value="?${fn:replace(properQueryString,'lang=es','lang=ko')}" />
<c:set var="ptUrl" scope="request" value="?${fn:replace(properQueryString,'lang=es','lang=pt')}" />
</c:when>
<c:when test="${fn:contains(pageContext.request.queryString, 'lang=fr')}">
<c:set var="frUrl" scope="request" value="?${properQueryString}"/>
<c:set var="enUrl" scope="request" value="?${fn:replace(properQueryString,'lang=fr','lang=en')}" />
<c:set var="esUrl" scope="request" value="?${fn:replace(properQueryString,'lang=fr','lang=es')}" />
<c:set var="koUrl" scope="request" value="?${fn:replace(properQueryString,'lang=fr','lang=ko')}" />
<c:set var="ptUrl" scope="request" value="?${fn:replace(properQueryString,'lang=fr','lang=pt')}" />
</c:when>
<c:when test="${fn:contains(pageContext.request.queryString, 'lang=ko')}">
<c:set var="frUrl" scope="request" value="?${fn:replace(properQueryString,'lang=ko','lang=fr')}" />
<c:set var="enUrl" scope="request" value="?${fn:replace(properQueryString,'lang=ko','lang=en')}" />
<c:set var="esUrl" scope="request" value="?${fn:replace(properQueryString,'lang=ko','lang=es')}" />
<c:set var="koUrl" scope="request" value="?${properQueryString}"/>
<c:set var="ptUrl" scope="request" value="?${fn:replace(properQueryString,'lang=ko','lang=pt')}" />
</c:when>
<c:when test="${fn:contains(pageContext.request.queryString, 'lang=pt')}">
<c:set var="frUrl" scope="request" value="?${fn:replace(properQueryString,'lang=pt','lang=fr')}" />
<c:set var="enUrl" scope="request" value="?${fn:replace(properQueryString,'lang=pt','lang=en')}" />
<c:set var="esUrl" scope="request" value="?${fn:replace(properQueryString,'lang=pt','lang=es')}" />
<c:set var="koUrl" scope="request" value="?${fn:replace(properQueryString,'lang=pt','lang=ko')}" />
<c:set var="ptUrl" scope="request" value="?${properQueryString}"/>
</c:when>
<c:otherwise>
<c:set var="frUrl" scope="request" value="?${properQueryString}&amp;lang=fr"/>
<c:set var="enUrl" scope="request" value="?${properQueryString}&amp;lang=en"/>
<c:set var="esUrl" scope="request" value="?${properQueryString}&amp;lang=es"/>
<c:set var="koUrl" scope="request" value="?${properQueryString}&amp;lang=ko"/>
<c:set var="ptUrl" scope="request" value="?${properQueryString}&amp;lang=pt"/>
</c:otherwise>
</c:choose>
</c:when>
<c:otherwise>
<c:set var="frUrl" scope="request" value="?lang=fr"/>
<c:set var="enUrl" scope="request" value="?lang=en"/>
<c:set var="esUrl" scope="request" value="?lang=es"/>
<c:set var="koUrl" scope="request" value="?lang=ko"/>
<c:set var="ptUrl" scope="request" value="?lang=pt"/>
</c:otherwise>
</c:choose>
<nav id="lang-switcher" aria-label="<fmt:message key="nav.language-switching.aria-label"/>" role="navigation">
<c:set scope="page"
var="properQueryString"
value="${fn:replace(pageContext.request.queryString, '&', '&amp;')}"/>
<nav aria-label="<fmt:message key="nav.language-switching.aria-label"/>"
role="navigation"
id="lang-switcher">
<ul>
<c:choose>
<c:when test="${lang == 'en'}">
<li><strong aria-current="page">english</strong></li>
</c:when>
<c:otherwise>
<li lang="en"><a href="${enUrl}" hreflang="en" title="Switch to english">english</a></li>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${lang == 'es'}">
<li><strong aria-current="page">español</strong></li>
</c:when>
<c:otherwise>
<li lang="es"><a href="${esUrl}" hreflang="es" title="Cambiar a español">español</a></li>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${lang == 'fr'}">
<li><strong aria-current="page">français</strong></li>
</c:when>
<c:otherwise>
<li lang="fr"><a href="${frUrl}" hreflang="fr" title="Passer en français">français</a></li>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${lang == 'pt'}">
<li><strong aria-current="page">português</strong></li>
</c:when>
<c:otherwise>
<li lang="pt"><a href="${ptUrl}" hreflang="pt" title="em Português">português</a></li>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${lang == 'ko'}">
<li><strong aria-current="page">한국어</strong></li>
</c:when>
<c:otherwise>
<li lang="ko"><a href="${koUrl}" hreflang="ko" title="한국어로 전환">한국어</a></li>
</c:otherwise>
</c:choose>
<c:forEach var="langCode" items="${orderedLanguagesList}">
<%--compute the URL of the menu item --%>
<c:choose>
<c:when test="${empty pageContext.request.queryString}">
<c:set var="langURL" value="?lang=${langCode}" scope="page" />
</c:when>
<c:otherwise>
<c:choose>
<c:when test="${fn:contains(pageContext.request.queryString, 'lang=')}">
<c:set var="newLang" value="lang=${langCode}" scope="page" />
<c:set var="currentLang" value="lang=${lang}" scope="page" />
<c:set var="langURL" scope="page"
value="?${fn:replace(properQueryString, currentLang, newLang)}"/>
</c:when>
<c:otherwise>
<c:set var="langURL" scope="page"
value="?${properQueryString}&amp;lang=${langCode}"/>
</c:otherwise>
</c:choose>
</c:otherwise>
</c:choose>

<%-- display the menu item --%>
<c:choose>
<c:when test="${lang == langCode}">
<li><strong aria-current="page">
${fn:toLowerCase(availableLanguages[langCode])}
</strong>
</li>
</c:when>
<c:otherwise>
<li lang="${langCode}">
<a hreflang="${langCode}"
title="${textsOfSwitchLanguage[langCode]}"
href="${langURL}">${fn:toLowerCase(availableLanguages[langCode])}</a>
</li>
</c:otherwise>
</c:choose>
</c:forEach>
</ul>
</nav>

Expand Down

0 comments on commit ae7ce3d

Please sign in to comment.