Files
Febe/src/Febe.Web/wwwroot/js/core/text.js
T
Claude e33540246e Interfaccia web, parte 1: ospite, sicurezza, API, modalità dimostrativa, libreria, visualizzatore e account
Febe.Web: AddFebeWeb/UseFebe/FebeWebHost per le due vesti, interfaccia incorporata con IFileProvider proprio, indirizzi italiani, API JSON con contesto generato, eventi SSE, controlli di Host/Origin, gettone del desktop e password con blocco (§13.1), INativeShell e capacità per veste. Modalità --demo con 38 account e circa 18.000 elementi finti e scene del motore. Pagine Libreria, Visualizzatore, Account, Aggiungi account e Scheda account (contenuti, impostazioni con piano, cronologia); schermate in docs/schermate con le differenze dai mockup; prove delle API, della sicurezza, dell'esportazione CSV e dei dati dimostrativi.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
2026-10-13 19:14:35 +00:00

167 lines
6.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Texts and number/date formats. Every string the user reads comes from testi/<lingua>.json (spec §3: adding a
// language must not touch the code). Numbers use dot for thousands and comma for decimals (spec §10.4); future engine
// times are always «verso le».
let texts = {};
/** Loads the text resource (once, at start). */
export async function loadTexts(language = 'it') {
const response = await fetch(`/testi/${language}.json`, { headers: { Accept: 'application/json' } });
if (!response.ok) throw new Error('testi');
texts = await response.json();
}
function lookup(key) {
let node = texts;
for (const part of key.split('.')) {
if (node == null) return undefined;
node = node[part];
}
return node;
}
/**
* The text of a key. Values with «|» are «singular|plural» chosen by vars.n; {name} placeholders are filled from vars.
* A missing key shows the key itself, so a gap is visible instead of silently empty.
*/
export function t(key, vars) {
let value = lookup(key);
if (value == null) return key;
if (typeof value !== 'string') return value;
if (value.includes('|')) {
const [one, many] = value.split('|');
value = vars && Number(vars.n) === 1 ? one : many;
}
if (vars) {
value = value.replace(/\{(\w+)\}/g, (match, name) => {
if (!(name in vars)) return match;
const v = vars[name];
return typeof v === 'number' ? number(v) : String(v);
});
}
return value;
}
/** A list of texts (weekday and month names). */
export function list(key) {
const value = lookup(key);
return Array.isArray(value) ? value : [];
}
/** Whole number with dots for thousands: 48.212 (also for four digits: 1.284). */
export function number(value) {
const n = Math.round(Number(value) || 0);
const digits = Math.abs(n).toString();
let out = '';
for (let i = 0; i < digits.length; i++) {
if (i > 0 && (digits.length - i) % 3 === 0) out += '.';
out += digits[i];
}
return (n < 0 ? '-' : '') + out;
}
/** Decimal with a comma: 1,3. Trailing zeros dropped. */
export function decimal(value, decimals = 1) {
const factor = 10 ** decimals;
const rounded = Math.round(Number(value) * factor) / factor;
const whole = Math.trunc(rounded);
let fraction = Math.round(Math.abs(rounded - whole) * factor).toString().padStart(decimals, '0').replace(/0+$/, '');
const head = (rounded < 0 && whole === 0 ? '-' : '') + number(whole);
return fraction ? `${head},${fraction}` : head;
}
/** «412 kB», «1,3 GB». */
export function bytes(value) {
const b = Number(value) || 0;
const KB = 1024, MB = KB * 1024, GB = MB * 1024;
if (b >= GB) return t('unita.GB', { n: decimal(b / GB, 1) });
if (b >= MB) return t('unita.MB', { n: decimal(b / MB, b >= 100 * MB ? 0 : 1) });
if (b >= KB) return t('unita.kB', { n: number(b / KB) });
return t('unita.byte', { n: number(b) });
}
/** Video duration: «0:24», «1:02», «1:02:03». */
export function duration(seconds) {
const s = Math.max(0, Math.round(Number(seconds) || 0));
const h = Math.floor(s / 3600), m = Math.floor((s % 3600) / 60), r = s % 60;
const ss = String(r).padStart(2, '0');
return h > 0 ? `${h}:${String(m).padStart(2, '0')}:${ss}` : `${m}:${ss}`;
}
/** Parses «2026-10-13» as a calendar day (no time zone involved). */
export function parseDay(day) {
const [y, m, d] = String(day).split('-').map(Number);
return { y, m, d, weekday: new Date(Date.UTC(y, m - 1, d)).getUTCDay(), number: Date.UTC(y, m - 1, d) / 86400000 };
}
/** «martedì 13 ottobre» (with the year when it is not the year of «today»). */
export function dayName(day, today) {
const p = parseDay(day);
const base = `${list('tempo.giorni')[p.weekday]} ${p.d} ${list('tempo.mesi')[p.m - 1]}`;
return today && parseDay(today).y !== p.y ? `${base} ${p.y}` : base;
}
/** «13 ottobre» (or «13 ottobre 2025» in another year). */
export function shortDay(day, today) {
const p = parseDay(day);
const base = `${p.d} ${list('tempo.mesi')[p.m - 1]}`;
return today && parseDay(today).y !== p.y ? `${base} ${p.y}` : base;
}
/** Days between two calendar days (b − a). */
export function daysBetween(a, b) {
return Math.round(parseDay(b).number - parseDay(a).number);
}
export function capitalize(text) {
return text ? text[0].toUpperCase() + text.slice(1) : text;
}
/** «verso le 16:10», «domani verso le 7:42», «giovedì verso le 7:42», «il 21 ottobre verso le 7:42». */
export function around(local) {
if (!local) return '';
const hour = Number(String(local.clock).split(':')[0]);
const time = t(hour === 1 ? 'tempo.versoUna' : 'tempo.verso', { ora: local.clock });
const prefix = dayPrefix(local);
return prefix ? `${prefix} ${time}` : time;
}
/** «alle 16:10», «ieri alle 7:42». */
export function at(local) {
if (!local) return '';
const hour = Number(String(local.clock).split(':')[0]);
const time = t(hour === 1 ? 'tempo.allUna' : 'tempo.alle', { ora: local.clock });
if (local.dayDelta === 0) return time;
if (local.dayDelta === -1) return `${t('tempo.ieri')} ${time}`;
const prefix = dayPrefix(local);
return prefix ? `${prefix} ${time}` : `${t('tempo.il', { giorno: shortDay(local.day) })} ${time}`;
}
function dayPrefix(local) {
const d = local.dayDelta;
if (d <= 0) return '';
if (d === 1) return t('tempo.domani');
if (d < 7) return list('tempo.giorni')[parseDay(local.day).weekday];
return t('tempo.il', { giorno: shortDay(local.day) });
}
/** «8 min fa», «1 ora fa», «ieri», «11 giorni fa» relative to the server's now (milliseconds). */
export function ago(ms, nowMs, today, day) {
const minutes = Math.max(0, Math.floor((nowMs - ms) / 60000));
if (minutes < 1) return t('tempo.adesso');
if (minutes < 60) return t('tempo.minutiFa', { n: minutes });
const hours = Math.floor(minutes / 60);
if (day && today) {
const days = daysBetween(day, today);
if (days === 1 && hours >= 6) return t('tempo.ieri');
if (days >= 2 && days < 14) return t('tempo.giorniFa', { n: days });
}
if (hours < 24) return t('tempo.oreFa', { n: hours });
const days = Math.floor(hours / 24);
if (days === 1) return t('tempo.ieri');
if (days < 14) return t('tempo.giorniFa', { n: days });
if (days < 60) return t('tempo.settimaneFa', { n: Math.floor(days / 7) });
if (days < 365) return t('tempo.mesiFa', { n: Math.floor(days / 30) });
return t('tempo.anniFa', { n: Math.floor(days / 365) });
}