diff --git a/libs/blocks/quiz-results/quiz-results.js b/libs/blocks/quiz-results/quiz-results.js index b1c7c3f159..7465782fc7 100644 --- a/libs/blocks/quiz-results/quiz-results.js +++ b/libs/blocks/quiz-results/quiz-results.js @@ -1,6 +1,6 @@ import { createTag, getConfig } from '../../utils/utils.js'; import { handleStyle } from '../section-metadata/section-metadata.js'; -import { getNormalizedMetadata } from '../quiz/utils.js'; +import { getNormalizedMetadata, getLocalizedURL } from '../quiz/utils.js'; import { decorateSectionAnalytics } from '../../martech/attributes.js'; export const LOADING_ERROR = 'Could not load quiz results:'; @@ -19,7 +19,7 @@ async function loadFragments(el, experiences) { } function redirectPage(quizUrl, debug, message) { - const url = (quizUrl) ? quizUrl.text : 'https://adobe.com'; + const url = quizUrl ? getLocalizedURL(quizUrl.text) : 'https://adobe.com'; window.lana.log(message, { tags: 'errorType=error,module=quiz-results' }); if (debug === 'quiz-results') { diff --git a/libs/blocks/quiz/quiz.js b/libs/blocks/quiz/quiz.js index 3aabbde78e..413963e545 100644 --- a/libs/blocks/quiz/quiz.js +++ b/libs/blocks/quiz/quiz.js @@ -7,6 +7,7 @@ import { DecorateBlockBackground, DecorateBlockForeground } from './quizcontaine import { initConfigPathGlob, handleResultFlow, handleNext, transformToFlowData, getQuizData, getAnalyticsDataForBtn, getUrlParams, isValidUrl, + getLocalizedURL, } from './utils.js'; import StepIndicator from './stepIndicator.js'; @@ -265,7 +266,7 @@ const App = ({ }; const fragmentURL = getStringValue('footerFragment'); if (fragmentURL) { - loadFragments(fragmentURL); + loadFragments(getLocalizedURL(fragmentURL)); } const iconBg = getStringValue('icon-background-color'); if (iconBg) { diff --git a/libs/blocks/quiz/utils.js b/libs/blocks/quiz/utils.js index 4af0f53590..6bf625db5f 100644 --- a/libs/blocks/quiz/utils.js +++ b/libs/blocks/quiz/utils.js @@ -18,7 +18,7 @@ const initConfigPath = (quizMetaData) => { const quizConfigPath = quizMetaData.data.text; const urlParams = new URLSearchParams(window.location.search); const stringsPath = urlParams.get('quiz-data'); - return (filepath) => `${stringsPath || quizConfigPath}${filepath}`; + return (filepath) => `${stringsPath || getLocalizedURL(quizConfigPath)}${filepath}`; }; async function fetchContentOfFile(path) { @@ -84,7 +84,8 @@ export const findAndStoreResultData = async (answers = []) => { let umbrellaProduct = ''; if (resultData.matchedResults.length > 0) { - destinationPage = resultData.matchedResults[0].url; + destinationPage = getLocalizedURL(resultData.matchedResults[0].url); + primaryProductCodes = resultData.primary; secondaryProductCodes = resultData.secondary; umbrellaProduct = resultData.matchedResults[0]['umbrella-result']; @@ -168,11 +169,11 @@ export const structuredFragments = ( resultResources?.data?.forEach((row) => { if (umbrellaProduct) { if (umbrellaProduct && row.product === umbrellaProduct) { - structureFragments.push(row[fragment]); + structureFragments.push(getLocalizedURL(row[fragment])); } } else if (primaryProducts?.length > 0 && primaryProducts.includes(row.product) && row[fragment]) { - structureFragments.push(row[fragment]); + structureFragments.push(getLocalizedURL(row[fragment])); } }); }); @@ -233,7 +234,7 @@ const getNestedFragments = (resultResources, productCodes, fragKey) => { function insertFragment() { row[fragKey]?.split(',').forEach((val) => { - fragArray.push(val.trim()); + fragArray.push(getLocalizedURL(val.trim())); }); } }); @@ -497,3 +498,9 @@ export const getAnalyticsDataForLocalStorage = (config) => { export const isValidUrl = (url) => VALID_URL_RE.test(url); export const getNormalizedMetadata = (el) => normalizeKeys(getMetadata(el)); + +export const getLocalizedURL = (originalURL) => { + const { locale } = getConfig(); + const { prefix, ietf = 'en-US' } = locale || {}; + return ietf !== 'en-US' && !originalURL.startsWith(`${prefix}/`) ? `${prefix}${originalURL}` : originalURL; +}; diff --git a/test/blocks/quiz/utils.test.js b/test/blocks/quiz/utils.test.js index 8a9eeb52f4..15bf95bad0 100644 --- a/test/blocks/quiz/utils.test.js +++ b/test/blocks/quiz/utils.test.js @@ -12,7 +12,7 @@ const { findAndStoreResultData, } = await import('../../../libs/blocks/quiz/utils.js'); -const locales = { '': { ietf: 'en-US', tk: 'hah7vzn.css' } }; +let locales = { '': { ietf: 'en-US', tk: 'hah7vzn.css' } }; const conf = { locales }; const QUIZ_BASE_PATH = 'https://mockdata/path/to/quiz'; @@ -245,6 +245,28 @@ describe('Quiz', () => { expect(flowData).to.be.an('array').of.length(5); }); + it('Testing getLocalizedURL with country code or without country code', async () => { + locales = { '': { ietf: 'de-DE', tk: 'hah7vzn.css' } }; + setConfig({ locales, pathname: '/de' }); + + // Import getLocalizedURL function + const { getLocalizedURL } = await import('../../../libs/blocks/quiz/utils.js'); + + expect(getLocalizedURL('/path/to/quiz/uar-results')).to.equal('/de/path/to/quiz/uar-results'); + expect(getLocalizedURL('/de/path/to/quiz/uar-results')).to.equal('/de/path/to/quiz/uar-results'); + }); + + it('Testing getLocalizedURL without locale define', async () => { + locales = { '': { } }; + setConfig({ locales, pathname: '/de' }); + + // Import getLocalizedURL function + const { getLocalizedURL } = await import('../../../libs/blocks/quiz/utils.js'); + + expect(getLocalizedURL('/path/to/quiz/uar-results')).to.equal('/path/to/quiz/uar-results'); + expect(getLocalizedURL('/de/path/to/quiz/uar-results')).to.equal('/de/path/to/quiz/uar-results'); + }); + describe('Testing storeResultInLocalStorage with empty results as input', async () => { let resultToDelegate; const primaryProductCodes = [];