mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
- 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>
23 lines
892 B
JavaScript
23 lines
892 B
JavaScript
// Strings for the page's scripts: the "js" keys of config/locales, which the
|
|
// layout puts in <script type="application/json" id="i18n">.
|
|
// t("bulk_upload.uploading", { done: 1, total: 4 })
|
|
// t("offline.sent", { count: 2 }) // one/other plurals pick by count
|
|
let strings
|
|
|
|
function load() {
|
|
if (!strings) {
|
|
try { strings = JSON.parse(document.getElementById("i18n")?.textContent || "{}") } catch { strings = {} }
|
|
}
|
|
return strings
|
|
}
|
|
|
|
export function t(key, vars = {}) {
|
|
let value = key.split(".").reduce((node, part) => node?.[part], load())
|
|
if (value && typeof value === "object") value = value[vars.count === 1 ? "one" : "other"]
|
|
if (typeof value !== "string") return key
|
|
return value.replace(/%\{(\w+)\}/g, (_, name) => vars[name] ?? "")
|
|
}
|
|
|
|
// The page's language, for number and date formatting.
|
|
export const locale = () => document.documentElement.lang || "en"
|