'use strict';
/** Helper function to save locale based on attribute value of an element.
* (This will fail if the desired attribute is not found).
* @param {HTMLElement} element - An element in a NodeList
* @return {boolean}
*/
function _saveLocale(element) {
try {
if (element.hasAttribute('data-locale')) {
const locale = element.getAttribute('data-locale');
window.jsCookies.set('locale', locale);
} else {
DEBUG && alert('No data-locale on link!'); /* FE-2856 */// eslint-disable-line
return false;
}
} catch (err) {
console.error(err); /* FE-2856 */// eslint-disable-line
if (window.Sentry) {
window.Sentry.captureMessage(err, 'error');
}
return false;
}
return true;
}
/** Add functionality to locale links to set the locale before redirecting.
*
* Language selector links must have the class js-link--locale on them, and they
* must have a data-locale attribute which is a JSON parseable object with the
* locale key set to the xx_XX value desired. The locale value is used
* without verification of it being a valid locale.
*
* Example usage:
*
* <a href='/vyprvpn' class='js-link--locale' data-locale='{'locale':'fr_FR'}'>French</a>
*
*/
export default function localeLinks() {
// We defer because our DOM must be loaded first
document.addEventListener("DOMContentLoaded", () => {
const links = document.querySelectorAll('a.js-link--locale');
for (const link of links) {
link.onclick = function onclick() {
return _saveLocale(this);
};
}
})
}