8000 language based region nav config by vhargrave · Pull Request #3425 · adobecom/milo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

language based region nav config #3425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 9, 2025
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
25 changes: 20 additions & 5 deletions libs/blocks/region-nav/region-nav.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { getConfig } from '../../utils/utils.js';

const queriedPages = [];

function setInternational(prefix) {
const domain = window.location.host.endsWith('.adobe.com') ? 'domain=adobe.com' : '';
const maxAge = 365 * 24 * 60 * 60; // max-age in seconds for 365 days
Expand All @@ -10,25 +12,38 @@
function handleEvent({ prefix, link, callback } = {}) {
if (typeof callback !== 'function') return;

const existingPage = queriedPages.find((page) => page.href === link.href);
if (existingPage) {
callback(existingPage.resp.ok
? link.href
: `${prefix ? `/${prefix}` : ''}/`);
return;
}

Check warning on line 21 in libs/blocks/region-nav/region-nav.js

View check run for this annotation

Codecov / codecov/patch

libs/blocks/region-nav/region-nav.js#L17-L21

Added lines #L17 - L21 were not covered by tests
fetch(link.href, { method: 'HEAD' }).then((resp) => {
queriedPages.push({ href: link.href, resp });
if (!resp.ok) throw new Error('request failed');
callback(link.href);
}).catch(() => {
const prefixUrl = prefix ? `/${prefix}` : '';
callback(`${prefixUrl}/`);
callback(`${prefix ? `/${prefix}` : ''}/`);
});
}

function decorateLink(link, path) {
export function decorateLink(link, path) {
let hrefAdapted;
let pathname = link.getAttribute('href');
if (pathname.startsWith('http')) {
try { pathname = new URL(pathname).pathname; } catch (e) { /* href does not contain domain */ }
}
const linkParts = pathname.split('/');
const prefix = linkParts[1] || 'us';
const prefix = linkParts[1] || '';
let { href } = link;
if (href.endsWith('/')) href = href.slice(0, -1);

const { languageMap } = getConfig();
if (languageMap && !getConfig().locales[prefix]) {
const valueInMap = languageMap[prefix];
href = href.replace(`/${prefix}`, valueInMap ? `/${valueInMap}` : '');
}
link.href = `${href}${path}`;

link.addEventListener('mouseover', () => {
Expand All @@ -47,7 +62,7 @@
});

link.addEventListener('click', (e) => {
setInternational(prefix);
setInternational(prefix === '' ? 'us' : prefix);
if (hrefAdapted) return;
e.preventDefault();
handleEvent({
Expand Down
65 changes: 63 additions & 2 deletions test/blocks/region-nav/region-nav.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { readFile } from '@web/test-runner-commands';
import { expect } from '@esm-bundle/chai';
import sinon from 'sinon';
import { setConfig, getConfig } from '../../../libs/utils/utils.js';
import { setConfig, getConfig, createTag } from '../../../libs/utils/utils.js';

import init from '../../../libs/blocks/region-nav/region-nav.js';
import init, { decorateLink } from '../../../libs/blocks/region-nav/region-nav.js';

document.body.innerHTML = await readFile({ path: './mocks/regions.html' });

Expand Down Expand Up @@ -104,4 +104,65 @@ describe('Region Nav Block', () => {
await clock.runAllAsync();
expect(window.open.calledWith(chfrPrefix)).to.be.true;
});

it('replaces the prefix with the mapped value when prefix is NOT in locales but is in languageMap', () => {
setConfig({
languageMap: {
ar: 'es',
at: 'de',
},
locales: {
'': { ietf: 'en-US', tk: 'hah7vzn.css' },
africa: { ietf: 'en', tk: 'hah7vzn.css' },
// Notice we do NOT include 'ar' or 'at' here so that prefix is considered "not in locales".
},
});

const link = createTag('a', { href: 'https://adobe.com/ar/' });
decorateLink(link, '/path/to/some/page');

// Assert that the href has been transformed from '/ar/' to '/es/' due to languageMap
expect(link.href).to.equal('https://adobe.com/es/path/to/some/page');
});

it('removes the prefix when prefix is NOT in locales and has an empty mapping in languageMap', () => {
setConfig({
languageMap: {
ae_ar: '',
ae_en: '',
africa: '',
ar: '',
},
locales: { '': { ietf: 'en-US', tk: 'hah7vzn.css' } },
});

const link = createTag('a', { href: 'https://adobe.com/ar/' });
decorateLink(link, '/some-page');

// Because `ar` is mapped to an empty string, the code replaces `"/ar"` with `""`
expect(link.href).to.equal('https://adobe.com/some-page');
});

it('does NOT modify href if prefix is in locales (even if present in languageMap)', () => {
setConfig({
languageMap: { ar: 'es' },
locales: { '': { ietf: 'en-US', tk: 'hah7vzn.css' }, ar: { ietf: 'ar', tk: 'lpk1hwn.css', dir: 'rtl' } }, // Now "ar" is a valid locale
});

const link = createTag('a', { href: 'https://adobe.com/ar/some-page' });
decorateLink(link, '');

// Since 'ar' is in locales, we should NOT transform
expect(link.href).to.equal('https://adobe.com/ar/some-page');
});

it('does nothing if no languageMap is defined', () => {
setConfig({ });

const link = createTag('a', { href: 'https://adobe.com/ar/some-page' });
decorateLink(link, '');

// No languageMap means no transformation
expect(link.href).to.equal('https://adobe.com/ar/some-page');
});
});
Loading
0