Files
simple-aircraft-manager-rails/app/javascript/controllers/theme_controller.js
T
rickyandClaude Opus 5.5 89ed0a76f9 Move all user-facing text into config/locales/en.yml for localization
- Views use lazy t(".key") lookups; sentences with values or links stay
  whole (interpolation, _html keys); counts use one/other plurals
- Stored-value labels (statuses, types, priorities...) are LabelSets read
  from labels: in the current locale; field names and validation messages
  under activerecord:; flash and activity text under flash:
- Services, models, helpers, mailer subjects, the logbook PDF, procedures
  templates, records package README and transcription warnings localized;
  the PDF reuses LogbookEntry#certification_statement instead of a copy
- Scripts read js: strings from a JSON tag via app/javascript/i18n.js
- Dates through I18n.l (app, weekday, month_year, long formats); html lang
  follows the locale
- APP_LOCALE picks the language (falls back to English); missing
  translations raise in development and tests
- README: Translating section and APP_LOCALE

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 22:08:20 -05:00

49 lines
1.7 KiB
JavaScript

import { Controller } from "@hotwired/stimulus"
import { t } from "i18n"
const CHOICES = ["system", "light", "dark"]
// Light / dark / follow-the-OS theme toggle. The choice is kept in
// localStorage; the layout's inline script applies it before first paint.
export default class extends Controller {
connect() {
this.media = window.matchMedia("(prefers-color-scheme: dark)")
this.onSystemChange = () => { if (this.choice === "system") this.apply() }
this.media.addEventListener("change", this.onSystemChange)
this.apply()
}
disconnect() {
this.media.removeEventListener("change", this.onSystemChange)
}
cycle() {
this.choice = CHOICES[(CHOICES.indexOf(this.choice) + 1) % CHOICES.length]
this.apply()
}
get choice() {
try { return localStorage.getItem("theme") || "system" } catch { return "system" }
}
set choice(value) {
try { localStorage.setItem("theme", value) } catch {}
}
apply() {
const choice = this.choice
const dark = choice === "dark" || (choice === "system" && this.media.matches)
const theme = dark ? "dark" : "light"
const changed = document.documentElement.dataset.theme !== theme
document.documentElement.dataset.theme = theme
this.element.querySelectorAll("[data-theme-icon]").forEach((icon) => {
icon.classList.toggle("hidden", icon.dataset.themeIcon !== choice)
})
const label = choice === "system" ? t("theme.system", { theme: t(`theme.${theme}`) }) : t("theme.label", { choice: t(`theme.${choice}`) })
this.element.title = label
this.element.setAttribute("aria-label", label)
if (changed) window.dispatchEvent(new CustomEvent("theme:change", { detail: { theme } }))
}
}