-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlang-switch.html
More file actions
124 lines (115 loc) · 4.7 KB
/
Copy pathlang-switch.html
File metadata and controls
124 lines (115 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<script>
// =============================================================
// Bilingual language switcher (EN default ⇄ IT under /it/)
// Injected after <body> on every page. English content lives at
// the site root; Italian counterparts live under /it/ with the
// same file names. On Italian pages this also localizes the
// (project-global) navbar labels/footer and rewrites nav links so
// navigation stays within /it/.
//
// Note: Quarto's quarto-nav.js normalizes navbar hrefs to absolute
// root URLs on load — which would send Italian-page nav links back
// to the English pages. We therefore (a) derive hrefs from the
// resolved URL by inserting "/it/" (idempotent) and (b) re-apply
// via a MutationObserver so the fix survives Quarto's pass.
// =============================================================
(function () {
var isIt = (document.documentElement.lang || "").toLowerCase().indexOf("it") === 0;
var LABELS = {
"About": "Chi sono",
"Short Bio": "Biografia",
"Curriculum Vitae": "Curriculum",
"Research": "Ricerca",
"Publications": "Pubblicazioni",
"Projects": "Progetti",
"Software": "Software",
"Statistics": "Statistiche",
"Teaching": "Didattica",
"Photos": "Foto",
"Contacts": "Contatti"
};
function counterpartUrl() {
var path = window.location.pathname;
if (path.charAt(path.length - 1) === "/") path += "index.html";
return isIt
? path.replace("/it/", "/")
: path.replace(/\/([^\/]*)$/, "/it/$1");
}
function localizeLabels() {
document.querySelectorAll(
".navbar a.nav-link, .navbar a.dropdown-item, .navbar .navbar-brand"
).forEach(function (el) {
var key = el.textContent.trim();
if (!LABELS.hasOwnProperty(key)) return;
var done = false;
el.childNodes.forEach(function (n) {
if (n.nodeType === 3 && n.textContent.trim()) {
n.textContent = n.textContent.replace(key, LABELS[key]);
done = true;
}
});
if (!done) el.textContent = LABELS[key];
});
}
// Keep navbar links inside /it/ by inserting "/it/" into the
// resolved path. Idempotent and base-path agnostic.
function fixNavHrefs() {
var origin = window.location.origin;
document.querySelectorAll(".navbar a[href]").forEach(function (el) {
if (el.classList.contains("lang-switch")) return;
var raw = el.getAttribute("href");
if (!raw || raw.charAt(0) === "#") return; // dropdown toggles / anchors
var u;
try { u = new URL(el.href); } catch (e) { return; }
if (u.origin !== origin) return; // external (Scholar, ResearchGate)
if (/(^|\/)it\//.test(u.pathname)) return; // already localized
if (!/\.html$/.test(u.pathname) && u.pathname.charAt(u.pathname.length - 1) !== "/") return;
u.pathname = u.pathname.replace(/\/([^\/]*)$/, "/it/$1");
el.setAttribute("href", u.pathname + u.search + u.hash);
});
}
function localizeFooter() {
var footer = document.querySelector(".nav-footer");
if (!footer || footer.dataset.itDone) return;
footer.innerHTML = footer.innerHTML
.replace("Department of Economics and Statistics", "Dipartimento di Scienze Economiche e Statistiche")
.replace("University of Naples Federico II", "Università degli Studi di Napoli Federico II")
.replace("Built with", "Realizzato con");
footer.dataset.itDone = "1";
}
function addToggle() {
if (document.querySelector(".nav-link.lang-switch")) return;
var nav = document.querySelector(".navbar .navbar-nav");
if (!nav) return;
var li = document.createElement("li");
li.className = "nav-item";
var a = document.createElement("a");
a.className = "nav-link lang-switch";
a.href = counterpartUrl();
a.textContent = isIt ? "EN" : "IT";
a.setAttribute("aria-label", isIt ? "Switch to English" : "Passa alla versione italiana");
a.setAttribute("hreflang", isIt ? "en" : "it");
li.appendChild(a);
nav.appendChild(li);
}
function apply() {
if (isIt) { localizeLabels(); fixNavHrefs(); localizeFooter(); }
addToggle();
}
function init() {
apply();
// Re-apply after Quarto's navbar normalization pass, then watch
// for any further mutations (idempotent, so it settles quickly).
if (isIt) {
var nav = document.querySelector(".navbar");
if (nav) {
var obs = new MutationObserver(function () { localizeLabels(); fixNavHrefs(); });
obs.observe(nav, { subtree: true, childList: true, attributes: true, attributeFilter: ["href"] });
}
}
window.addEventListener("load", apply);
}
if (document.readyState !== "loading") init();
else document.addEventListener("DOMContentLoaded", init);
})();
</script>