using System.Globalization; using System.Text; using Febe.Core.Abstractions; using Febe.Core.Domain; namespace Febe.Core.Engine; /// /// Italian wording used by the engine for events, denials and the monitor (spec §10.4): dot for thousands, comma for /// decimals, «verso le» for future engine times. Independent of the process culture, so it reads the same in Docker /// (invariant globalization) and on Windows. /// public static class ItalianText { private static readonly string[] Months = [ "gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre", ]; private static readonly string[] Weekdays = ["domenica", "lunedì", "martedì", "mercoledì", "giovedì", "venerdì", "sabato"]; /// Whole number with dots for thousands: 7.682. public static string Number(long value) { var digits = Math.Abs(value).ToString(CultureInfo.InvariantCulture); var builder = new StringBuilder(); if (value < 0) { builder.Append('-'); } for (var i = 0; i < digits.Length; i++) { if (i > 0 && (digits.Length - i) % 3 == 0) { builder.Append('.'); } builder.Append(digits[i]); } return builder.ToString(); } /// Decimal number with a comma: 1,3. Trailing zeros of the decimals are kept only when asked. public static string Decimal(double value, int decimals = 1, bool keepZeros = false) { var rounded = Math.Round(value, decimals, MidpointRounding.AwayFromZero); var whole = (long)Math.Truncate(rounded); var fraction = Math.Abs(rounded - whole); var text = Number(whole); if (whole == 0 && rounded < 0) { text = "-0"; } if (decimals <= 0) { return text; } var fractionDigits = ((long)Math.Round(fraction * Math.Pow(10, decimals))).ToString(CultureInfo.InvariantCulture).PadLeft(decimals, '0'); if (!keepZeros) { fractionDigits = fractionDigits.TrimEnd('0'); } return fractionDigits.Length == 0 ? text : text + "," + fractionDigits; } /// Byte amount: «850 MB», «1,3 GB». public static string Bytes(long bytes) { const double KB = 1024; const double MB = KB * 1024; const double GB = MB * 1024; return bytes switch { >= (long)GB => Decimal(bytes / GB, 1) + " GB", >= (long)MB => Decimal(bytes / MB, bytes >= 100 * MB ? 0 : 1) + " MB", >= (long)KB => Decimal(bytes / KB, 0) + " kB", _ => Number(bytes) + " byte", }; } /// Local clock time without a leading zero on the hour: «7:42», «15:30». public static string Clock(DateTimeOffset instant, TimeZoneInfo zone) { var local = LocalCalendar.ToLocal(instant, zone); return local.Hour.ToString(CultureInfo.InvariantCulture) + ":" + local.Minute.ToString("00", CultureInfo.InvariantCulture); } /// «alle 17:32», «all'1:05». public static string At(DateTimeOffset instant, TimeZoneInfo zone) => (LocalCalendar.ToLocal(instant, zone).Hour == 1 ? "all'" : "alle ") + Clock(instant, zone); /// «dalle 16:32», «dall'1:05». public static string Since(DateTimeOffset instant, TimeZoneInfo zone) => (LocalCalendar.ToLocal(instant, zone).Hour == 1 ? "dall'" : "dalle ") + Clock(instant, zone); /// /// A future engine time, always indicative (spec §7.9): «verso le 15:30», «domani verso le 7:42», /// «giovedì verso le 7:42», «il 21 ottobre verso le 7:42». /// public static string Around(DateTimeOffset instant, DateTimeOffset now, TimeZoneInfo zone) { var hour = LocalCalendar.ToLocal(instant, zone).Hour; var time = (hour == 1 ? "verso l'" : "verso le ") + Clock(instant, zone); var prefix = DayPrefix(instant, now, zone); return prefix.Length == 0 ? time : prefix + " " + time; } /// «alle 17:32», «domani alle 7:42»: a fixed future time (e.g. the end of a throttle stop). public static string AtDay(DateTimeOffset instant, DateTimeOffset now, TimeZoneInfo zone) { var prefix = DayPrefix(instant, now, zone); var at = At(instant, zone); return prefix.Length == 0 ? at : prefix + " " + at; } /// «13 ottobre». public static string Day(DateOnly day) => day.Day.ToString(CultureInfo.InvariantCulture) + " " + Months[day.Month - 1]; /// «lunedì 13 ottobre». public static string DayWithWeekday(DateOnly day) => Weekdays[(int)day.DayOfWeek] + " " + Day(day); /// A duration in words: «45 minuti», «1 ora e 20 minuti», «2 giorni e 3 ore», «30 secondi». public static string Duration(TimeSpan span) { if (span < TimeSpan.Zero) { span = TimeSpan.Zero; } if (span < TimeSpan.FromMinutes(1)) { var seconds = (long)Math.Round(span.TotalSeconds); return seconds == 1 ? "1 secondo" : Number(seconds) + " secondi"; } if (span < TimeSpan.FromHours(1)) { return Minutes((long)Math.Round(span.TotalMinutes)); } if (span < TimeSpan.FromDays(2)) { var totalMinutes = (long)Math.Round(span.TotalMinutes); var hours = totalMinutes / 60; var minutes = totalMinutes % 60; var text = hours == 1 ? "1 ora" : Number(hours) + " ore"; return minutes == 0 ? text : text + " e " + Minutes(minutes); } var totalHours = (long)Math.Round(span.TotalHours); var days = totalHours / 24; var restHours = totalHours % 24; var dayText = days == 1 ? "1 giorno" : Number(days) + " giorni"; return restHours == 0 ? dayText : dayText + " e " + (restHours == 1 ? "1 ora" : Number(restHours) + " ore"); } /// An interval of a check («ogni 3 ore», «ogni 75 minuti», «ogni giorno»). public static string Every(TimeSpan interval) { if (interval >= TimeSpan.FromHours(23.5) && interval <= TimeSpan.FromHours(24.5)) { return "ogni giorno"; } if (interval < TimeSpan.FromHours(2) && interval.TotalMinutes % 60 != 0) { return "ogni " + Minutes((long)Math.Round(interval.TotalMinutes)); } var hours = interval.TotalHours; if (Math.Abs(hours - Math.Round(hours)) < 0.05) { var whole = (long)Math.Round(hours); return whole == 1 ? "ogni ora" : "ogni " + Number(whole) + " ore"; } return "ogni " + Decimal(hours, 1) + " ore"; } /// Italian name of a budget category, as in the table of spec §7.5. public static string Category(ActionKind kind) => kind switch { ActionKind.Home => "aperture della home", ActionKind.ProfileOpen => "profili aperti", ActionKind.StoryView => "storie viste", ActionKind.BackfillPage => "pagine di storico", ActionKind.SinglePost => "post singoli", ActionKind.FollowingListPage => "pagine dell'elenco dei seguiti", ActionKind.DataDownload => "dati scaricati", _ => kind.ToString(), }; /// Category name with a capital initial, for labels: «Profili aperti». public static string CategoryLabel(ActionKind kind) => Capitalize(Category(kind)); /// Italian name of a caution profile: «Prudente». public static string Profile(CautionProfileKind kind) => kind switch { CautionProfileKind.Prudent => "Prudente", CautionProfileKind.Balanced => "Equilibrato", CautionProfileKind.Fast => "Rapido", CautionProfileKind.Custom => "Su misura", _ => kind.ToString(), }; /// «@marta.demo». public static string Handle(string? username) => string.IsNullOrWhiteSpace(username) ? "un account" : "@" + username.TrimStart('@'); /// Count with its noun: «1 storia nuova», «3 storie nuove». public static string Count(long count, string singular, string plural) => count == 1 ? "1 " + singular : Number(count) + " " + plural; public static string Capitalize(string text) => string.IsNullOrEmpty(text) ? text : char.ToUpperInvariant(text[0]) + text[1..]; private static string Minutes(long minutes) => minutes == 1 ? "1 minuto" : Number(minutes) + " minuti"; private static string DayPrefix(DateTimeOffset instant, DateTimeOffset now, TimeZoneInfo zone) { var day = LocalCalendar.Today(instant, zone); var today = LocalCalendar.Today(now, zone); var difference = day.DayNumber - today.DayNumber; return difference switch { <= 0 => string.Empty, 1 => "domani", < 7 => Weekdays[(int)day.DayOfWeek], _ => "il " + Day(day), }; } }