From 73f65c9c2244a3c1a6a57670b113323f9c94216b Mon Sep 17 00:00:00 2001 From: fkakatie Date: Fri, 10 Jul 2026 13:22:21 -0600 Subject: [PATCH 1/2] feat: export getLocale --- scripts/scripts.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/scripts.js b/scripts/scripts.js index b26cf44..1559628 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -114,7 +114,7 @@ function buildWidgetAutoBlocks(main) { * Returns the two-letter language code for the current page. * @returns {string} */ -function getLocale() { +export function getLocale() { const segment = window.location.pathname.split('/').filter(Boolean)[0]; const lang = (segment && /^[a-z]{2}(-[a-z]{2})?$/i.test(segment)) ? segment : 'en'; return lang.split('-')[0].toLowerCase(); From 66f56ba47c6c0c131eb2d2c2f1354fcbbab2cfa5 Mon Sep 17 00:00:00 2001 From: fkakatie Date: Fri, 10 Jul 2026 13:23:00 -0600 Subject: [PATCH 2/2] feat: use exported getLocale function instead of rewriting in each block/widget --- blocks/carousel/carousel.js | 10 +--------- blocks/header/header.js | 16 ++-------------- widgets/card-list/card-list.js | 10 +--------- widgets/plp/plp.js | 10 +--------- widgets/press-releases/press-releases.js | 14 ++------------ widgets/search/search.js | 14 +------------- 6 files changed, 8 insertions(+), 66 deletions(-) diff --git a/blocks/carousel/carousel.js b/blocks/carousel/carousel.js index 981b69f..1ebb7b6 100644 --- a/blocks/carousel/carousel.js +++ b/blocks/carousel/carousel.js @@ -1,5 +1,5 @@ // eslint-disable-next-line import/no-cycle -import { buildVideoAutoBlocks } from '../../scripts/scripts.js'; +import { buildVideoAutoBlocks, getLocale } from '../../scripts/scripts.js'; import { decorateBlock, loadBlock } from '../../scripts/aem.js'; const CAROUSEL_VARIANTS = ['slides', 'promo']; @@ -11,14 +11,6 @@ function resolveVariant(block) { return 'promo'; } -function getLocale() { - const segment = window.location.pathname.split('/').filter(Boolean)[0]; - if (segment && /^[a-z]{2}(-[a-z]{2})?$/i.test(segment)) { - return segment.split('-')[0].toLowerCase(); - } - return (document.documentElement.lang || 'en').split('-')[0].toLowerCase(); -} - async function loadCopy(lang) { const scriptPath = new URL(import.meta.url).pathname; const jsonPath = scriptPath.replace(/\.js$/, '.json'); diff --git a/blocks/header/header.js b/blocks/header/header.js index a9c529b..b790d84 100644 --- a/blocks/header/header.js +++ b/blocks/header/header.js @@ -1,5 +1,5 @@ import { getMetadata, decorateIcons } from '../../scripts/aem.js'; -import { decorateExternalLinks, loadCopy } from '../../scripts/scripts.js'; +import { decorateExternalLinks, loadCopy, getLocale } from '../../scripts/scripts.js'; import { loadFragment } from '../fragment/fragment.js'; /** @@ -175,18 +175,6 @@ function decorateNav(section) { }); } -/** - * Resolve the locale prefix from the current URL path. - * @returns {string} Locale path segment (e.g. en, fr) - */ -function getLocaleFromPath() { - const segment = window.location.pathname.split('/').filter(Boolean)[0]; - if (segment && /^[a-z]{2}(-[a-z]{2})?$/i.test(segment)) { - return segment.split('-')[0].toLowerCase(); - } - return 'en'; -} - /** * Whether the page includes a search results widget. * @returns {boolean} @@ -227,7 +215,7 @@ function decorateSearch(section, copy) { section.textContent = ''; section.append(form); - const locale = getLocaleFromPath(); + const locale = getLocale(); const searchResultsPath = `/${locale}/search`; const params = new URLSearchParams(window.location.search); diff --git a/widgets/card-list/card-list.js b/widgets/card-list/card-list.js index 4dec657..4ed9eab 100644 --- a/widgets/card-list/card-list.js +++ b/widgets/card-list/card-list.js @@ -1,15 +1,7 @@ import { createOptimizedPicture, decorateBlock, loadBlock, } from '../../scripts/aem.js'; -import { normalizeIndexImageUrl } from '../../scripts/scripts.js'; - -function getLocale() { - const segment = window.location.pathname.split('/').filter(Boolean)[0]; - if (segment && /^[a-z]{2}(-[a-z]{2})?$/i.test(segment)) { - return segment.split('-')[0].toLowerCase(); - } - return (document.documentElement.lang || 'en').split('-')[0].toLowerCase(); -} +import { normalizeIndexImageUrl, getLocale } from '../../scripts/scripts.js'; function resolveRoot(rootParam, lang) { const pagePath = window.location.pathname; diff --git a/widgets/plp/plp.js b/widgets/plp/plp.js index a9d76df..32c999a 100644 --- a/widgets/plp/plp.js +++ b/widgets/plp/plp.js @@ -1,7 +1,7 @@ import { createOptimizedPicture, decorateBlock, loadBlock, } from '../../scripts/aem.js'; -import { normalizeIndexImageUrl } from '../../scripts/scripts.js'; +import { normalizeIndexImageUrl, getLocale } from '../../scripts/scripts.js'; const FACETS = [ { key: 'region', copyKey: 'region' }, @@ -12,14 +12,6 @@ const FACETS = [ { key: 'traction-system', copyKey: 'tractionSystem' }, ]; -function getLocale() { - const segment = window.location.pathname.split('/').filter(Boolean)[0]; - if (segment && /^[a-z]{2}(-[a-z]{2})?$/i.test(segment)) { - return segment.split('-')[0].toLowerCase(); - } - return (document.documentElement.lang || 'en').split('-')[0].toLowerCase(); -} - async function loadWidgetCopy(lang) { const scriptPath = new URL(import.meta.url).pathname; const jsonPath = scriptPath.replace(/\.js$/, '.json'); diff --git a/widgets/press-releases/press-releases.js b/widgets/press-releases/press-releases.js index ab1d864..57774c9 100644 --- a/widgets/press-releases/press-releases.js +++ b/widgets/press-releases/press-releases.js @@ -1,3 +1,5 @@ +import { getLocale } from '../../scripts/scripts.js'; + /** * Load widget config from the widget's local JSON (same name as the script). * @param {string} lang - Language key (e.g. en) @@ -19,18 +21,6 @@ async function loadWidgetConfig(lang) { } } -/** - * Resolve the current site locale from the URL path or document language. - * @returns {string} Locale code (e.g. en, fr) - */ -function getLocale() { - const segment = window.location.pathname.split('/').filter(Boolean)[0]; - if (segment && /^[a-z]{2}(-[a-z]{2})?$/i.test(segment)) { - return segment.split('-')[0].toLowerCase(); - } - return (document.documentElement.lang || 'en').split('-')[0].toLowerCase(); -} - /** * Normalize a press release row from the query index. * @param {Object} row - Raw row from query-index.json diff --git a/widgets/search/search.js b/widgets/search/search.js index 8212ea7..75eb2ec 100644 --- a/widgets/search/search.js +++ b/widgets/search/search.js @@ -1,5 +1,5 @@ import { createOptimizedPicture, loadCSS } from '../../scripts/aem.js'; -import { normalizeIndexImageUrl } from '../../scripts/scripts.js'; +import { normalizeIndexImageUrl, getLocale } from '../../scripts/scripts.js'; /** * Load widget copy from the widget's local JSON (same name as the script). @@ -21,18 +21,6 @@ async function loadWidgetCopy(lang) { } } -/** - * Resolve the current site locale from the URL path or document language. - * @returns {string} Locale code (e.g. en, fr) - */ -function getLocale() { - const segment = window.location.pathname.split('/').filter(Boolean)[0]; - if (segment && /^[a-z]{2}(-[a-z]{2})?$/i.test(segment)) { - return segment.split('-')[0].toLowerCase(); - } - return (document.documentElement.lang || 'en').split('-')[0].toLowerCase(); -} - /** * Normalize a single item from the query index. * @param {Object} row - Raw row from query-index.json