Skip to content
Draft
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
11 changes: 2 additions & 9 deletions scripts/coveo-headless/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import buildHeadlessSearchEngine from './engine.js';
import { fetchLanguagePlaceholders } from '../scripts.js';
import { fetchLanguagePlaceholders, getCoveoSearchLocale } from '../scripts.js';
import { handleCoverSearchSubmit } from '../../blocks/browse-filters/browse-filter-utils.js';
import { COVEO_SEARCH_CUSTOM_EVENTS } from '../search/search-utils.js';

Expand All @@ -12,20 +12,13 @@ try {
console.error('Error fetching placeholders:', err);
}

const locales = new Map([
['es', 'es-ES'],
['pt-br', 'pt-BR'],
['zh-hans', 'zh-CN'],
['zh-hant', 'zh-TW'],
]);

function configureSearchHeadlessEngine({ module, searchEngine, searchHub, contextObject, advancedQueryRule }) {
const advancedQuery = module.loadAdvancedSearchQueryActions(searchEngine).registerAdvancedSearchQueries({
aq: advancedQueryRule || '',
});
const context = contextObject ? module.loadContextActions(searchEngine).setContext(contextObject) : null;
const searchConfiguration = module.loadSearchConfigurationActions(searchEngine).updateSearchConfiguration({
locale: locales.get(document.querySelector('html').lang) || document.querySelector('html').lang || 'en',
locale: getCoveoSearchLocale(),
searchHub,
});
const fields = module
Expand Down
7 changes: 2 additions & 5 deletions scripts/data-service/coveo/coveo-exl-pipeline-helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { URL_SPECIAL_CASE_LOCALES, fetchLanguagePlaceholders, getConfig } from '../../scripts.js';
import { fetchLanguagePlaceholders, getConfig, getCoveoSearchLocale } from '../../scripts.js';
import { rewriteDocsPath } from '../../utils/path-utils.js';
import CoveoDataService from './coveo-data-service.js';
import { CONTENT_TYPES, COMMUNITY_SEARCH_FACET } from './coveo-exl-pipeline-constants.js';
Expand Down Expand Up @@ -195,10 +195,7 @@ export function getExlPipelineDataSourceParams(param, fields = fieldsToInclude)
const dataSource = {
url: coveoSearchResultsUrl,
param: {
locale:
URL_SPECIAL_CASE_LOCALES.get(document.querySelector('html').lang) ||
document.querySelector('html').lang ||
'en',
locale: getCoveoSearchLocale(),
searchHub: `Experience League Learning Hub`,
numberOfResults: param.noOfResults,
excerptLength: 200,
Expand Down
49 changes: 49 additions & 0 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,55 @@ export const URL_SPECIAL_CASE_LOCALES = new Map([
['zh-hant', 'zh-TW'],
]);

/**
* Coveo search `locale` (BCP 47, hyphen). Keep separate from IMS `locales` (underscore).
* Bare html.lang codes (en/de/…) must map to region tags so multi-lang pipelines that
* key off en-US/de-DE still return Events V2; safe if multi-lang is off.
*/
export const COVEO_SEARCH_LOCALES = new Map([
['en', 'en-US'],
['de', 'de-DE'],
['fr', 'fr-FR'],
['it', 'it-IT'],
['ja', 'ja-JP'],
['ko', 'ko-KR'],
['es', 'es-ES'],
['pt-br', 'pt-BR'],
['zh-hans', 'zh-CN'],
['zh-hant', 'zh-TW'],
]);
Comment on lines +966 to +977

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor maintainability concern: this adds a third locale map (locales, URL_SPECIAL_CASE_LOCALES, now COVEO_SEARCH_LOCALES) that overlaps with the other two but has to be kept in sync by hand.

If a new language is ever added to locales/URL_SPECIAL_CASE_LOCALES and someone forgets to add it here, getCoveoSearchLocale won't error — it silently falls back to en-US (line 1007), so Coveo search would quietly run with the wrong locale for that language with no visible failure signal. Might be worth a comment near the other two maps pointing here, so future language additions don't miss this one.

Not blocking — flagging since the failure mode is silent rather than loud.


const COVEO_SEARCH_LOCALE_DEFAULT = 'en-US';

/**
* Normalize page language to a Coveo search locale (BCP 47).
* @param {string} [lang] - html.lang or path lang; defaults to documentElement.lang
* @returns {string}
*/
export function getCoveoSearchLocale(lang) {
let raw = '';
if (lang != null && String(lang).trim() !== '') {
raw = String(lang);
} else if (typeof document !== 'undefined') {
raw = document.documentElement?.lang || document.querySelector?.('html')?.lang || '';
}
const normalized = raw.trim().replace(/_/g, '-');
if (!normalized) return COVEO_SEARCH_LOCALE_DEFAULT;

const lower = normalized.toLowerCase();
if (COVEO_SEARCH_LOCALES.has(lower)) {
return COVEO_SEARCH_LOCALES.get(lower);
}

// Already region-qualified (e.g. en-US, zh-CN).
if (/^[a-z]{2,3}-[a-z0-9]{2,8}$/i.test(normalized)) {
const [language, region] = normalized.split('-');
return `${language.toLowerCase()}-${region.toUpperCase()}`;
}

return COVEO_SEARCH_LOCALE_DEFAULT;
}

// TODO: Move loadIms() out of scripts.js into a dedicated utility .
// and import it from there. Its current location causes a cyclic dependency because
// premium-learning-utils.js → profile.js → scripts.js → premium-learning-utils.js.
Expand Down
Loading