Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions blocks/carousel/carousel.js
Original file line number Diff line number Diff line change
@@ -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'];
Expand All @@ -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');
Expand Down
16 changes: 2 additions & 14 deletions blocks/header/header.js
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 1 addition & 9 deletions widgets/card-list/card-list.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
10 changes: 1 addition & 9 deletions widgets/plp/plp.js
Original file line number Diff line number Diff line change
@@ -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' },
Expand All @@ -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');
Expand Down
14 changes: 2 additions & 12 deletions widgets/press-releases/press-releases.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand Down
14 changes: 1 addition & 13 deletions widgets/search/search.js
Original file line number Diff line number Diff line change
@@ -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).
Expand All @@ -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
Expand Down