| 1 | const STORAGE_KEY = 'vcc.language.v1'; |
| 2 | |
| 3 | export const LANGUAGES = { |
| 4 | es: 'Español', |
| 5 | en: 'English', |
| 6 | }; |
| 7 | |
| 8 | const STRINGS = { |
| 9 | es: { |
| 10 | 'app.title': 'VibeCodeViewer — Metrópolis de Cristal', |
| 11 | 'mode.basic': 'Básico', |
| 12 | 'mode.git': 'Git', |
| 13 | 'mode.ownership': 'Owners', |
| 14 | 'mode.blast': 'Blast', |
| 15 | 'button.aerial': 'Vista Aérea', |
| 16 | 'button.skyline': 'Vista Altura Edificios', |
| 17 | 'button.resetView': 'Reset Vista', |
| 18 | 'button.settings': 'Ajustes', |
| 19 | 'button.legend': 'Leyenda', |
| 20 | 'button.ground': 'Suelo Opaco', |
| 21 | 'button.openFolder': 'Abrir Carpeta…', |
| 22 | 'button.reopen': 'Reabrir {name}', |
| 23 | 'button.resetSettings': 'Restablecer ajustes', |
| 24 | 'label.language': 'Idioma', |
| 25 | 'label.themeTitle': 'Tema de color', |
| 26 | 'search.title': 'Búsqueda', |
| 27 | 'search.placeholder': 'Buscar fichero… (p. ej. *.js, src/*.css)', |
| 28 | 'tree.title': 'Árbol de ficheros', |
| 29 | 'help.title': 'Navegación', |
| 30 | 'help.drag.key': 'Arrastrar', |
| 31 | 'help.drag.value': 'orbitar · rueda zoom', |
| 32 | 'help.click.key': 'Clic', |
| 33 | 'help.click.value': 'seleccionar fichero / edificio', |
| 34 | 'help.fly.key': 'F', |
| 35 | 'help.fly.value': 'modo vuelo (atraviesa paredes)', |
| 36 | 'help.move.key': 'W A S D', |
| 37 | 'help.move.value': 'mover · ratón mirar', |
| 38 | 'help.vertical.key': 'Space / C', |
| 39 | 'help.vertical.value': 'subir / bajar · Shift turbo', |
| 40 | 'help.views.key': '1 · 2 · 3', |
| 41 | 'help.views.value': 'reset · aérea · altura', |
| 42 | 'help.clear.key': 'X', |
| 43 | 'help.clear.value': 'limpiar selección · / buscar', |
| 44 | 'help.toggles.key': 'L · G', |
| 45 | 'help.toggles.value': 'leyenda · suelo opaco', |
| 46 | 'help.tree.key': '↑↓ ←→', |
| 47 | 'help.tree.value': 'navegar árbol · plegar/desplegar', |
| 48 | 'settings.title': 'Ajustes', |
| 49 | 'settings.close': 'Cerrar', |
| 50 | 'settings.bloom': 'Bloom HDR', |
| 51 | 'settings.exposure': 'Exposición', |
| 52 | 'settings.brightness': 'Brillo', |
| 53 | 'settings.contrast': 'Contraste', |
| 54 | 'settings.saturate': 'Saturación', |
| 55 | 'settings.lines': 'Aristas', |
| 56 | 'settings.files': 'Celdas', |
| 57 | 'settings.glass': 'Cristal', |
| 58 | 'settings.facades': 'Fachadas', |
| 59 | 'settings.labels': 'Rótulos', |
| 60 | 'settings.ground': 'Suelo', |
| 61 | 'settings.fog': 'Niebla', |
| 62 | 'preset.safe': 'Seguro', |
| 63 | 'preset.neon': 'Neón', |
| 64 | 'preset.cinema': 'Cine', |
| 65 | 'preset.intense': 'Intenso', |
| 66 | 'theme.prefix': 'Tema · {label}', |
| 67 | 'path.none': '— sin selección · clic en un edificio o busca un fichero —', |
| 68 | 'tree.basementSuffix': 'sótano', |
| 69 | 'modeUnavailable.git': 'sin datos git (ni .git/index legible)', |
| 70 | 'modeUnavailable.ownership': 'sin reglas de code ownership', |
| 71 | 'modeUnavailable.blast': 'sin relaciones de código detectadas', |
| 72 | 'stats.urbs': ' · {count} urbanizaciones', |
| 73 | 'stats.main': '{base} ficheros + {ignored} ignorados · {buildings} edificios{urbs} · {districts} distritos · {fps} fps{gpu}{analysis}', |
| 74 | 'gpu.direct': 'render directo', |
| 75 | 'gpu.software': ' · render software · {mode}', |
| 76 | 'gpu.high': ' · GPU {brand} · {mode}', |
| 77 | 'gpu.other': ' · {mode}', |
| 78 | 'gpu.discrete': 'discreta', |
| 79 | 'analysis.loading': 'Escaneando…', |
| 80 | 'analysis.loadingDetail': 'Escaneando… {msg}', |
| 81 | 'progress.files': '{count} ficheros', |
| 82 | 'progress.analysisCheckpoint': 'análisis {analyzed}/{total} · checkpoint {checkpoints}', |
| 83 | 'legend.impact': 'Impacto: dependen de él', |
| 84 | 'legend.dependencies': 'Dependencias: usa de ellos', |
| 85 | 'gitStatus.M': 'Modificado', |
| 86 | 'gitStatus.A': 'Añadido (staged)', |
| 87 | 'gitStatus.U': 'Sin seguimiento', |
| 88 | 'gitStatus.R': 'Renombrado', |
| 89 | 'gitStatus.D': 'Borrado', |
| 90 | }, |
| 91 | en: { |
| 92 | 'app.title': 'VibeCodeViewer — Crystal Metropolis', |
| 93 | 'mode.basic': 'Basic', |
| 94 | 'mode.git': 'Git', |
| 95 | 'mode.ownership': 'Owners', |
| 96 | 'mode.blast': 'Blast', |
| 97 | 'button.aerial': 'Aerial View', |
| 98 | 'button.skyline': 'Building Height View', |
| 99 | 'button.resetView': 'Reset View', |
| 100 | 'button.settings': 'Settings', |
| 101 | 'button.legend': 'Legend', |
| 102 | 'button.ground': 'Opaque Ground', |
| 103 | 'button.openFolder': 'Open Folder…', |
| 104 | 'button.reopen': 'Reopen {name}', |
| 105 | 'button.resetSettings': 'Reset Settings', |
| 106 | 'label.language': 'Language', |
| 107 | 'label.themeTitle': 'Color theme', |
| 108 | 'search.title': 'Search', |
| 109 | 'search.placeholder': 'Search file… (e.g. *.js, src/*.css)', |
| 110 | 'tree.title': 'File tree', |
| 111 | 'help.title': 'Navigation', |
| 112 | 'help.drag.key': 'Drag', |
| 113 | 'help.drag.value': 'orbit · wheel zoom', |
| 114 | 'help.click.key': 'Click', |
| 115 | 'help.click.value': 'select file / building', |
| 116 | 'help.fly.key': 'F', |
| 117 | 'help.fly.value': 'fly mode (crosses walls)', |
| 118 | 'help.move.key': 'W A S D', |
| 119 | 'help.move.value': 'move · mouse look', |
| 120 | 'help.vertical.key': 'Space / C', |
| 121 | 'help.vertical.value': 'up / down · Shift turbo', |
| 122 | 'help.views.key': '1 · 2 · 3', |
| 123 | 'help.views.value': 'reset · aerial · height', |
| 124 | 'help.clear.key': 'X', |
| 125 | 'help.clear.value': 'clear selection · / search', |
| 126 | 'help.toggles.key': 'L · G', |
| 127 | 'help.toggles.value': 'legend · opaque ground', |
| 128 | 'help.tree.key': '↑↓ ←→', |
| 129 | 'help.tree.value': 'navigate tree · collapse/expand', |
| 130 | 'settings.title': 'Settings', |
| 131 | 'settings.close': 'Close', |
| 132 | 'settings.bloom': 'Bloom HDR', |
| 133 | 'settings.exposure': 'Exposure', |
| 134 | 'settings.brightness': 'Brightness', |
| 135 | 'settings.contrast': 'Contrast', |
| 136 | 'settings.saturate': 'Saturation', |
| 137 | 'settings.lines': 'Edges', |
| 138 | 'settings.files': 'Cells', |
| 139 | 'settings.glass': 'Glass', |
| 140 | 'settings.facades': 'Facades', |
| 141 | 'settings.labels': 'Labels', |
| 142 | 'settings.ground': 'Ground', |
| 143 | 'settings.fog': 'Fog', |
| 144 | 'preset.safe': 'Safe', |
| 145 | 'preset.neon': 'Neon', |
| 146 | 'preset.cinema': 'Cinema', |
| 147 | 'preset.intense': 'Intense', |
| 148 | 'theme.prefix': 'Theme · {label}', |
| 149 | 'path.none': '— no selection · click a building or search for a file —', |
| 150 | 'tree.basementSuffix': 'basement', |
| 151 | 'modeUnavailable.git': 'no git data (no readable .git/index)', |
| 152 | 'modeUnavailable.ownership': 'no code ownership rules', |
| 153 | 'modeUnavailable.blast': 'no code relationships detected', |
| 154 | 'stats.urbs': ' · {count} developments', |
| 155 | 'stats.main': '{base} files + {ignored} ignored · {buildings} buildings{urbs} · {districts} districts · {fps} fps{gpu}{analysis}', |
| 156 | 'gpu.direct': 'direct render', |
| 157 | 'gpu.software': ' · software render · {mode}', |
| 158 | 'gpu.high': ' · GPU {brand} · {mode}', |
| 159 | 'gpu.other': ' · {mode}', |
| 160 | 'gpu.discrete': 'discrete', |
| 161 | 'analysis.loading': 'Scanning…', |
| 162 | 'analysis.loadingDetail': 'Scanning… {msg}', |
| 163 | 'progress.files': '{count} files', |
| 164 | 'progress.analysisCheckpoint': 'analysis {analyzed}/{total} · checkpoint {checkpoints}', |
| 165 | 'legend.impact': 'Impact: depend on it', |
| 166 | 'legend.dependencies': 'Dependencies: used by it', |
| 167 | 'gitStatus.M': 'Modified', |
| 168 | 'gitStatus.A': 'Added (staged)', |
| 169 | 'gitStatus.U': 'Untracked', |
| 170 | 'gitStatus.R': 'Renamed', |
| 171 | 'gitStatus.D': 'Deleted', |
| 172 | }, |
| 173 | }; |
| 174 | |
| 175 | function normalizeLang(value) { |
| 176 | const raw = String(value || '').toLowerCase(); |
| 177 | if (raw.startsWith('es')) return 'es'; |
| 178 | if (raw.startsWith('en')) return 'en'; |
| 179 | return null; |
| 180 | } |
| 181 | |
| 182 | function initialLanguage() { |
| 183 | const url = normalizeLang(new URLSearchParams(location.search).get('lang')); |
| 184 | if (url) return url; |
| 185 | const stored = normalizeLang(localStorage.getItem(STORAGE_KEY)); |
| 186 | if (stored) return stored; |
| 187 | return normalizeLang(navigator.language) || 'es'; |
| 188 | } |
| 189 | |
| 190 | let currentLanguage = initialLanguage(); |
| 191 | const listeners = new Set(); |
| 192 | |
| 193 | document.documentElement.lang = currentLanguage; |
| 194 | |
| 195 | export function language() { |
| 196 | return currentLanguage; |
| 197 | } |
| 198 | |
| 199 | export function t(key, params = {}) { |
| 200 | const table = STRINGS[currentLanguage] ?? STRINGS.es; |
| 201 | const fallback = STRINGS.es[key] ?? key; |
| 202 | const template = table[key] ?? fallback; |
| 203 | return String(template).replace(/\{(\w+)\}/g, (_, name) => params[name] ?? ''); |
| 204 | } |
| 205 | |
| 206 | export function setLanguage(next) { |
| 207 | const normalized = normalizeLang(next) || 'es'; |
| 208 | if (normalized === currentLanguage) return currentLanguage; |
| 209 | currentLanguage = normalized; |
| 210 | localStorage.setItem(STORAGE_KEY, normalized); |
| 211 | document.documentElement.lang = normalized; |
| 212 | for (const fn of listeners) fn(normalized); |
| 213 | return currentLanguage; |
| 214 | } |
| 215 | |
| 216 | export function onLanguageChange(fn) { |
| 217 | listeners.add(fn); |
| 218 | return () => listeners.delete(fn); |
| 219 | } |
| 220 | |
| 221 | export function applyDocumentTranslations(root = document) { |
| 222 | document.title = t('app.title'); |
| 223 | root.querySelectorAll('[data-i18n]').forEach((el) => { el.textContent = t(el.dataset.i18n); }); |
| 224 | root.querySelectorAll('[data-i18n-placeholder]').forEach((el) => { el.placeholder = t(el.dataset.i18nPlaceholder); }); |
| 225 | root.querySelectorAll('[data-i18n-title]').forEach((el) => { el.title = t(el.dataset.i18nTitle); }); |
| 226 | root.querySelectorAll('[data-i18n-aria-label]').forEach((el) => { el.setAttribute('aria-label', t(el.dataset.i18nAriaLabel)); }); |
| 227 | } |