import { inject, signal, Signal } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { NavigationEnd, Router } from '@angular/router'; import { filter } from 'rxjs'; /** * The site's chrome (header and footer menus) stays English everywhere, but * the labels around a tool - its breadcrumb, the "stays on your computer" * line, Related tools, Recent changes, the feedback button - belong to the * page, so the two pages written in another language get them in it: the * Italian e-invoice page (/fattura-elettronica) and the German one * (/e-rechnung). Every other page gets null and keeps the English. */ export type ChromeLang = 'it' | 'de'; const PAGES: Readonly> = { 'fattura-elettronica': 'it', 'e-rechnung': 'de' }; export function chromeLangFor(url: string): ChromeLang | null { return PAGES[url.split(/[?#]/)[0].replace(/^\/+|\/+$/g, '')] ?? null; } /** The page's chrome language as a signal, following navigation. Call from a constructor or field initialiser. */ export function chromeLang(): Signal { const router = inject(Router); const lang = signal(chromeLangFor(router.url)); router.events .pipe(filter((e): e is NavigationEnd => e instanceof NavigationEnd), takeUntilDestroyed()) .subscribe((e) => lang.set(chromeLangFor(e.urlAfterRedirects))); return lang; } const STRINGS: Readonly>> = { 'Home': { it: 'Home', de: 'Startseite' }, 'Breadcrumb': { it: 'Percorso di navigazione', de: 'Navigationspfad' }, 'Stays on your computer': { it: 'Resta sul tuo computer', de: 'Bleibt auf Ihrem Computer' }, 'Sent to johnmcm.com': { it: 'Inviato a johnmcm.com', de: 'Wird an johnmcm.com gesendet' }, 'A program you download': { it: 'Un programma da scaricare', de: 'Ein Programm zum Herunterladen' }, 'How this works': { it: 'Come funziona', de: 'So funktioniert es' }, 'Related tools': { it: 'Strumenti correlati', de: 'Verwandte Tools' }, 'Every file type, and the tool that opens it': { it: 'Ogni tipo di file e lo strumento che lo apre', de: 'Jeder Dateityp und das Tool, das ihn öffnet' }, 'Recent changes': { it: 'Modifiche recenti', de: 'Letzte Änderungen' }, 'Everything new on the site': { it: 'Tutte le novità del sito', de: 'Alle Neuerungen auf der Website' }, 'Suggested by a visitor': { it: 'Suggerito da un visitatore', de: 'Von einem Besucher vorgeschlagen' }, 'Something not right? Tell me': { it: 'Qualcosa non va? Scrivimi', de: 'Stimmt etwas nicht? Schreiben Sie mir' }, 'Report a problem or suggest something': { it: 'Segnala un problema o suggerisci qualcosa', de: 'Ein Problem melden oder etwas vorschlagen' }, }; /** The group names, as a heading or crumb, and as they follow "for" in a sentence. */ const GROUPS: Readonly>> = { 'PDF': { it: ['PDF', 'PDF'], de: ['PDF', 'PDF'] }, 'Office & documents': { it: ['Office e documenti', 'Office e documenti'], de: ['Office und Dokumente', 'Office und Dokumente'] }, 'Email, chats & backups': { it: ['Email, chat e backup', 'email, chat e backup'], de: ['E-Mail, Chats und Backups', 'E-Mail, Chats und Backups'] }, 'Photos & images': { it: ['Foto e immagini', 'foto e immagini'], de: ['Fotos und Bilder', 'Fotos und Bilder'] }, 'Video': { it: ['Video', 'video'], de: ['Video', 'Video'] }, 'Audio': { it: ['Audio', 'audio'], de: ['Audio', 'Audio'] }, 'Text & writing': { it: ['Testo e scrittura', 'testo e scrittura'], de: ['Texte und Schreiben', 'Texte und Schreiben'] }, 'Design, 3D & maps': { it: ['Design, 3D e mappe', 'design, 3D e mappe'], de: ['Design, 3D und Karten', 'Design, 3D und Karten'] }, 'Data & spreadsheets': { it: ['Dati e fogli di calcolo', 'dati e fogli di calcolo'], de: ['Daten und Tabellen', 'Daten und Tabellen'] }, 'Developer & learning': { it: ['Sviluppo e apprendimento', 'sviluppo e apprendimento'], de: ['Entwicklung und Lernen', 'Entwicklung und Lernen'] }, }; /** A chrome label in the page's language, or the English when there is none. */ export function chromeText(lang: ChromeLang | null, english: string): string { return (lang && STRINGS[english]?.[lang]) || english; } /** A group's name for a crumb or heading in the page's language. */ export function groupName(lang: ChromeLang | null, english: string): string { return (lang && GROUPS[english]?.[lang][0]) || english; } /** "More audio tools", "Altri strumenti per audio", "Weitere Tools für Audio"; `inSentence` is the English mid-sentence form. */ export function moreTools(lang: ChromeLang | null, english: string, inSentence: string): string { const g = lang ? GROUPS[english]?.[lang][1] : undefined; if (lang === 'it' && g) return `Altri strumenti per ${g}`; if (lang === 'de' && g) return `Weitere Tools für ${g}`; return `More ${inSentence} tools`; } /** "All audio tools", "Tutti gli strumenti per audio", "Alle Tools für Audio". */ export function allTools(lang: ChromeLang | null, english: string, inSentence: string): string { const g = lang ? GROUPS[english]?.[lang][1] : undefined; if (lang === 'it' && g) return `Tutti gli strumenti per ${g}`; if (lang === 'de' && g) return `Alle Tools für ${g}`; return `All ${inSentence} tools`; } /** The date of a change in the page's language. */ export function dateLocale(lang: ChromeLang | null): string { return lang === 'it' ? 'it-IT' : lang === 'de' ? 'de-DE' : 'en-GB'; }