Class: CMDx::I18nProxy
- Inherits:
-
Object
- Object
- CMDx::I18nProxy
- Defined in:
- lib/cmdx/i18n_proxy.rb
Overview
Translation façade used internally for coercion, validator, and output
error messages. Delegates to I18n.translate when the i18n gem is
available; otherwise loads CMDx's bundled YAML locale file and performs
percent-interpolation on the string itself. Results are memoized.
Class Method Summary collapse
-
.locale_paths ⇒ Array<String>
Directories searched (in order) for bundled locale YAMLs.
-
.register(path) ⇒ Array<String>
Register an additional directory containing locale YAML files.
-
.t(key, **options) ⇒ String, Object
The translated string (or the raw default value).
-
.tr(reason) ⇒ String
Resolves a reason string through translation, falling back to either the literal reason (when present) or the
cmdx.reasons.unspecifieddefault (when nil). -
.translate(key, **options) ⇒ String, Object
The translated string (or the raw default value).
Instance Method Summary collapse
-
#translate(key, **options) ⇒ String, Object
(also: #t)
The translated/interpolated message.
Class Method Details
.locale_paths ⇒ Array<String>
Returns directories searched (in order) for bundled locale YAMLs.
13 14 15 |
# File 'lib/cmdx/i18n_proxy.rb', line 13 def locale_paths @locale_paths ||= [File.("../locales", __dir__)] end |
.register(path) ⇒ Array<String>
Register an additional directory containing locale YAML files. Later registrations take precedence over earlier ones (the most recently registered path's values win during deep merge). Resets the memoized proxy so subsequent lookups see the new path.
41 42 43 44 45 |
# File 'lib/cmdx/i18n_proxy.rb', line 41 def register(path) locale_paths.push(path) unless locale_paths.include?(path) @proxy = nil locale_paths end |
.t(key, **options) ⇒ String, Object
Returns the translated string (or the raw default value).
30 31 32 |
# File 'lib/cmdx/i18n_proxy.rb', line 30 def t(key, **) translate(key, **) end |
.tr(reason) ⇒ String
Resolves a reason string through translation, falling back to either
the literal reason (when present) or the cmdx.reasons.unspecified
default (when nil).
53 54 55 |
# File 'lib/cmdx/i18n_proxy.rb', line 53 def tr(reason) translate(reason || "cmdx.reasons.unspecified", default: reason) end |
.translate(key, **options) ⇒ String, Object
Returns the translated string (or the raw default value).
22 23 24 25 |
# File 'lib/cmdx/i18n_proxy.rb', line 22 def translate(key, **) @proxy ||= new @proxy.translate(key, **) end |
Instance Method Details
#translate(key, **options) ⇒ String, Object Also known as: t
Returns the translated/interpolated message.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/cmdx/i18n_proxy.rb', line 64 def translate(key, **) return ::I18n.translate(key, **) if defined?(::I18n) = translation_default(key) || [:default] case when String % when NilClass "Translation missing: #{key}" else end end |