From 1838f0fa1686f2b508f1878dbaa8f1d942ed0827 Mon Sep 17 00:00:00 2001 From: Iris Faraway Date: Mon, 25 Sep 2023 11:15:18 +0100 Subject: [PATCH] Specify language in HTML tags (#1798) https://eaflood.atlassian.net/browse/IWTF-3674 This change makes sure that the correct language tags are applied to the HTML depending on whether the page is in English or Welsh. It also adds attributes to the language switch links to make sure those are always tagged with the correct language. --- .../__snapshots__/page-handler.spec.js.snap | 2 + .../handlers/__tests__/page-handler.spec.js | 52 ++++++++++++++++++- .../src/handlers/page-handler.js | 6 +++ .../src/pages/layout/layout.njk | 10 ++-- 4 files changed, 66 insertions(+), 4 deletions(-) diff --git a/packages/gafl-webapp-service/src/handlers/__tests__/__snapshots__/page-handler.spec.js.snap b/packages/gafl-webapp-service/src/handlers/__tests__/__snapshots__/page-handler.spec.js.snap index 76769bc602..6b58fda164 100644 --- a/packages/gafl-webapp-service/src/handlers/__tests__/__snapshots__/page-handler.spec.js.snap +++ b/packages/gafl-webapp-service/src/handlers/__tests__/__snapshots__/page-handler.spec.js.snap @@ -13,6 +13,7 @@ exports[`The page handler function sets the value of pageData with displayAnalyt "backRef": null, "displayAnalytics": false, "mssgs": undefined, + "pageLanguageSetToWelsh": false, "uri": Object { "analyticsFormAction": "/buy/process-analytics-preferences", }, @@ -41,6 +42,7 @@ exports[`The page handler function sets the value of pageData with displayAnalyt "backRef": null, "displayAnalytics": true, "mssgs": undefined, + "pageLanguageSetToWelsh": false, "uri": Object { "analyticsFormAction": "/buy/process-analytics-preferences", }, diff --git a/packages/gafl-webapp-service/src/handlers/__tests__/page-handler.spec.js b/packages/gafl-webapp-service/src/handlers/__tests__/page-handler.spec.js index 5d1327000a..49a73d74b7 100644 --- a/packages/gafl-webapp-service/src/handlers/__tests__/page-handler.spec.js +++ b/packages/gafl-webapp-service/src/handlers/__tests__/page-handler.spec.js @@ -241,6 +241,55 @@ describe('The page handler function', () => { expect(set).toBeCalledWith(result) }) }) + + describe('pageLanguageSetToWelsh', () => { + it('returns false when SHOW_WELSH_CONTENT is not true', async () => { + process.env.SHOW_WELSH_CONTENT = false + const { get } = pageHandler('', 'view', '/next/page') + const toolkit = getMockToolkit() + + await get(getMockRequest(), toolkit) + + expect(toolkit.view).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ + pageLanguageSetToWelsh: false + }) + ) + }) + + it('returns false when SHOW_WELSH_CONTENT is true but the lang is not set to cy', async () => { + process.env.SHOW_WELSH_CONTENT = true + const { get } = pageHandler('', 'view', '/next/page') + const toolkit = getMockToolkit() + + await get(getMockRequest(), toolkit) + + expect(toolkit.view).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ + pageLanguageSetToWelsh: false + }) + ) + }) + + it('returns true when SHOW_WELSH_CONTENT is true and the lang is set to cy', async () => { + process.env.SHOW_WELSH_CONTENT = true + const { get } = pageHandler('', 'view', '/next/page') + const query = { lang: 'cy' } + const request = getMockRequest({ query }) + const toolkit = getMockToolkit() + + await get(request, toolkit) + + expect(toolkit.view).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ + pageLanguageSetToWelsh: true + }) + ) + }) + }) }) const getAnalytics = overides => ({ @@ -249,7 +298,7 @@ const getAnalytics = overides => ({ ...overides }) -const getMockRequest = ({ setCurrentPermission = () => {}, path = '/buy/we/are/here', analytics, set = () => {} } = {}) => ({ +const getMockRequest = ({ setCurrentPermission = () => {}, path = '/buy/we/are/here', query = {}, analytics, set = () => {} } = {}) => ({ cache: () => ({ helpers: { page: { @@ -275,6 +324,7 @@ const getMockRequest = ({ setCurrentPermission = () => {}, path = '/buy/we/are/h getLocale: () => '' }, path, + query, url: { search: '' } diff --git a/packages/gafl-webapp-service/src/handlers/page-handler.js b/packages/gafl-webapp-service/src/handlers/page-handler.js index ae154652ac..5bf8b70e11 100644 --- a/packages/gafl-webapp-service/src/handlers/page-handler.js +++ b/packages/gafl-webapp-service/src/handlers/page-handler.js @@ -40,6 +40,11 @@ const omitPageFromAnalytics = async request => { } } +const pageLanguageSetToWelsh = request => { + const showWelshContent = process.env.SHOW_WELSH_CONTENT?.toLowerCase() === 'true' + return showWelshContent && request.query.lang === 'cy' +} + /** * Flattens the error structure from joi for use in the templates * @param e @@ -123,6 +128,7 @@ export default (path, view, completion, getData) => ({ pageData.altLang = request.i18n.getLocales().filter(locale => locale !== request.i18n.getLocale()) pageData.backRef = await getBackReference(request, view) pageData.uri = { ...(pageData.uri || {}), analyticsFormAction: addLanguageCodeToUri(request, PROCESS_ANALYTICS_PREFERENCES.uri) } + pageData.pageLanguageSetToWelsh = pageLanguageSetToWelsh(request) const analytics = await request.cache().helpers.analytics.get() pageData.analyticsMessageDisplayed = analytics ? analytics[ANALYTICS.seenMessage] : false diff --git a/packages/gafl-webapp-service/src/pages/layout/layout.njk b/packages/gafl-webapp-service/src/pages/layout/layout.njk index 49c896b1ff..aed62465e0 100644 --- a/packages/gafl-webapp-service/src/pages/layout/layout.njk +++ b/packages/gafl-webapp-service/src/pages/layout/layout.njk @@ -9,6 +9,10 @@ {% from "button/macro.njk" import govukButton %} {% from "footer/macro.njk" import govukFooter %} +{% if pageLanguageSetToWelsh %} + {% set htmlLang = "cy" %} +{% endif %} + {% macro csrf() %}
{% endmacro %} @@ -86,7 +90,7 @@
@@ -142,9 +146,9 @@

{% if _uri.queryParams.lang == 'cy' %} - English | Cymraeg + English | Cymraeg {% else %} - English | Cymraeg + English | Cymraeg {% endif%}