Module: Esp::UI
- Defined in:
- lib/esp/ui.rb
Overview
Internal i18n for the *tool’s own* user-facing strings — CLI output and error messages. Distinct from Esp::Mw::I18n, which localizes mod content; this localizes mw itself so the distributed tool can ship in other languages.
Catalogues live at locales/<locale>.yml, resolved relative to this file (they ship with the tool, independent of Esp::ROOT — which points at the user’s mod project). Lookup is dot-pathed with %named interpolation, falling back to en, then to the key itself.
Active locale: ESP_UI_LOCALE (with MW_UI_LOCALE honoured as a one-release deprecation alias), then LANG/LC_ALL (stripped to the language subtag, e.g. “fr_FR.UTF-8” -> “fr”), then en. Tests pin it to en.
Constant Summary collapse
- DEFAULT_LOCALE =
'en'.freeze
- LOCALES_DIR =
File.('../../locales', __dir__)
- KEY_LITERAL =
Quoted dotted literal, e.g. ‘cli.build.done’ or “errors.no_index”. Starts lowercase so it skips “Morrowind.esm” and version numbers.
/(['"])([a-z]\w*(?:\.\w+)+)\1/
Class Attribute Summary collapse
Class Method Summary collapse
-
.audit(lib_dir: File.expand_path('..', __dir__)) ⇒ Object
Audit the catalogues against the code: undefined — a UI-namespaced key referenced in lib but absent from en (a bug: the user would see the raw key) unused — defined in en but referenced nowhere (dead/typo) orphans — per non-en locale, keys not present in en (stale) missing — per non-en locale, en keys it doesn’t translate (falls back to en; informational — partials are fine).
- .t(key, **vars) ⇒ Object
- .with_locale(value) ⇒ Object
Class Attribute Details
.locale ⇒ Object
36 37 38 |
# File 'lib/esp/ui.rb', line 36 def locale @locale ||= detect_locale end |
Class Method Details
.audit(lib_dir: File.expand_path('..', __dir__)) ⇒ Object
Audit the catalogues against the code:
undefined — a UI-namespaced key referenced in lib but absent from
en (a bug: the user would see the raw key)
unused — defined in en but referenced nowhere (dead/typo)
orphans — per non-en locale, keys not present in en (stale)
missing — per non-en locale, en keys it doesn't translate
(falls back to en; informational — partials are fine)
Detection is a literal scan, so it’s robust to call form (ternary args, the ‘t` helper, `Esp::UI.t`). A key counts as referenced if its quoted literal appears anywhere in lib. “undefined” is scoped to the UI namespaces (en’s top-level keys), so mod-content t() keys and doc examples don’t masquerade as missing UI strings.
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/esp/ui.rb', line 67 def audit(lib_dir: File.('..', __dir__)) en = (catalogues[DEFAULT_LOCALE] || {}).keys namespaces = en.map { |k| k.split('.').first }.uniq referenced = referenced_keys(lib_dir) ui_referenced = referenced.select { |k| namespaces.include?(k.split('.').first) } others = catalogues.reject { |loc, _| loc == DEFAULT_LOCALE } { undefined: (ui_referenced - en).sort, unused: (en - referenced).sort, orphans: others.transform_values { |c| (c.keys - en).sort }.reject { |_, v| v.empty? }, missing: others.transform_values { |c| (en - c.keys).sort }.reject { |_, v| v.empty? } } end |
.t(key, **vars) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/esp/ui.rb', line 29 def t(key, **vars) template = lookup(locale, key) || lookup(DEFAULT_LOCALE, key) || key return template if vars.empty? interpolate(template, vars, key) end |
.with_locale(value) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/esp/ui.rb', line 42 def with_locale(value) previous = locale @locale = value yield ensure @locale = previous end |