/* ============================================================ carino-lang.js — fleet-wide language preference + switcher. ------------------------------------------------------------ Drop this file into any Carino project and add it AFTER the header exists (TV's native header) or after carino-navbar.js (every other site): It injects a compact language button (a language glyph + AUTO, or the picked code: EN / ES / PT / 日本語 / RU) into the header's right cluster, left of the Sys. Status control. Clicking it opens a dropdown: Auto (follow the browser) plus the five languages. AUTOMATIC: the language resolves without any user action — ?lang= in the URL > fleet-wide choice > browser language > English. The fleet-wide choice is a cookie on .carino.systems, so one pick on any subdomain applies to every site in the fleet (subdomains do NOT share localStorage — the cookie is the only thing that makes the preference travel). The same choice is mirrored into per-site localStorage, which is what off-domain copies (local dev, forks) read, and what keeps the pick alive on Safari — ITP caps a JS-set cookie at seven days. The file owns only the preference. Each app owns its own dictionary and reacts via: window.CarinoLang.current // 'en' | 'es' | 'pt-BR' | 'ja' | 'ru' window.CarinoLang.set(code) // programmatic switch window.addEventListener('carino:langchange', (e) => e.detail.lang) An app with no dictionary simply stays English — the choice still persists for the sites that do translate. Self-contained: styles are inlined and scoped under #cnLangBtn / #cnLangBox with hard-coded colours, so it is safe on light-themed or Tailwind pages and needs no CDN. ============================================================ */ (function () { 'use strict'; var A = '#eab308', BG = '#0f0f0f', BORDER = '#262626', MUTED = '#8a8a8a', TEXT = '#fff'; var MONO = '"IBM Plex Mono",ui-monospace,SFMono-Regular,Menlo,monospace'; // [code, button label, native name] var LANGS = [ ['en', 'EN', 'English'], ['es', 'ES', 'Español'], ['pt-BR', 'PT', 'Português'], ['ja', '日本語', '日本語'], ['ru', 'RU', 'Русский'], ]; var CSS = '' + '#cnLangBtn{display:flex;align-items:center;gap:6px;background:transparent;border:1px solid ' + BORDER + ';' + 'color:' + MUTED + ';font-family:' + MONO + ';font-size:.8rem;text-transform:uppercase;letter-spacing:.1em;' + 'padding:0 10px;height:32px;border-radius:4px;cursor:pointer;transition:.2s;white-space:nowrap;}' + '#cnLangBtn:hover{border-color:' + A + ';color:' + A + ';background:rgba(234,179,8,.08);}' + '#cnLangBox{position:fixed;top:56px;right:12px;width:180px;background:' + BG + ';' + 'border:1px solid ' + BORDER + ';border-radius:8px;padding:6px;box-shadow:0 18px 40px rgba(0,0,0,.6);' + 'opacity:0;visibility:hidden;transform:translateY(-8px);transition:.18s;z-index:400;}' + '#cnLangBox.open{opacity:1;visibility:visible;transform:translateY(0);}' + '#cnLangBox button{display:flex;justify-content:space-between;align-items:center;width:100%;' + 'background:transparent;border:0;border-radius:5px;padding:7px 10px;cursor:pointer;text-align:left;' + 'color:' + TEXT + ';font-size:.8rem;}' + '#cnLangBox button:hover{background:rgba(234,179,8,.08);color:' + A + ';}' + '#cnLangBox button .code{font-family:' + MONO + ';font-size:.65rem;letter-spacing:.1em;color:' + MUTED + ';}' // "Русский" is the only Cyrillic on an English page, and it is inside a // menu nobody has opened yet — enough to pull a whole Cyrillic webfont // subset down on every first visit in every language. A system face for // that one label costs nothing and every other row keeps the site's font. + '#cnLangBox button .nat[lang="ru"]{font-family:system-ui,-apple-system,"Segoe UI",Roboto,sans-serif;}' + '#cnLangBox button.active{color:' + A + ';}' + '#cnLangBox button.active .code{color:' + A + ';}'; /* ---- resolution ---- */ function resolve(tag) { if (!tag) return null; var l = String(tag).toLowerCase(); if (l.indexOf('es') === 0) return 'es'; if (l.indexOf('pt') === 0) return 'pt-BR'; if (l.indexOf('ja') === 0) return 'ja'; if (l.indexOf('ru') === 0) return 'ru'; if (l.indexOf('en') === 0) return 'en'; return null; } var onFleet = /(^|\.)carino\.systems$/.test(location.hostname); /* Storage is a MIRROR, not an either/or. The parent-domain cookie is the only thing that carries the choice across subdomains, but Safari's ITP caps the lifetime of any cookie written from JS at seven days — so on Safari the fleet-wide pick silently reverted to Auto about once a week, while Gecko and Chromium honoured the full Max-Age=31536000. localStorage is not capped that way, so we write both: the cookie for travel, the localStorage copy as a same-origin backstop. Reads prefer the cookie (it is the one another subdomain can have just updated) and fall through to the mirror when it is gone. */ function readCookie() { try { var m = document.cookie.match(/(?:^|;\s*)carino_lang=([^;]+)/); return m ? resolve(decodeURIComponent(m[1])) : null; } catch (e) { return null; } } function readStored() { var c = onFleet ? readCookie() : null; if (c) return c; try { return resolve(localStorage.getItem('carino_lang')); } catch (e) { return null; } } // Secure is added only where it means something: a browser rejects a Secure // cookie outright from a plain-http origin, so setting it unconditionally // would throw the preference away on any fleet site still served over http. var COOKIE = '; Domain=.carino.systems; Path=/; SameSite=Lax' + (location.protocol === 'https:' ? '; Secure' : ''); function store(code) { if (onFleet) { try { document.cookie = 'carino_lang=' + encodeURIComponent(code) + '; Max-Age=31536000' + COOKIE; } catch (e) { /* cookies blocked */ } } try { localStorage.setItem('carino_lang', code); } catch (e) { /* private mode */ } } function clearStore() { if (onFleet) { try { document.cookie = 'carino_lang=; Max-Age=0' + COOKIE; } catch (e) { /* cookies blocked */ } } try { localStorage.removeItem('carino_lang'); } catch (e) { /* private mode */ } } // AUTO is the default: no stored choice means "follow the browser". A // manual pick stores it fleet-wide; picking Auto again clears it. var urlLang = resolve(new URLSearchParams(location.search).get('lang')); if (urlLang) store(urlLang); var stored = urlLang || readStored(); var manual = !!stored; var current = stored || resolve(navigator.language) || 'en'; document.documentElement.lang = current; // Translated