Module: SuperSettings::MiniI18n
- Defined in:
- lib/super_settings/mini_i18n.rb
Overview
Internationalization support for the web UI. Translations are stored as
JSON files in app/locales/ with one file per locale (e.g. en.json).
Ruby templates use #t to look up a dotted key. JavaScript receives the
full translation hash inlined via a <script> tag so that the same JSON
file drives both server-side and client-side strings.
Constant Summary collapse
- DEFAULT_LOCALE =
"en"
Class Method Summary collapse
-
.available_locales ⇒ Array<String>
Return the list of available locale codes derived from the JSON files present in
app/locales/. -
.clear_cache! ⇒ void
Clear the translation cache.
-
.t(key, locale: DEFAULT_LOCALE) ⇒ String
Look up a translation by dotted key for the given locale.
-
.text_direction(locale = DEFAULT_LOCALE) ⇒ String
Return the text direction for the given locale.
-
.translations_for(locale) ⇒ Hash<String, String>
Return the full translation hash for a locale.
Class Method Details
.available_locales ⇒ Array<String>
Return the list of available locale codes derived from the JSON files
present in app/locales/.
23 24 25 |
# File 'lib/super_settings/mini_i18n.rb', line 23 def available_locales load_all_locales.keys.sort end |
.clear_cache! ⇒ void
This method returns an undefined value.
Clear the translation cache. Called automatically in development mode.
61 62 63 |
# File 'lib/super_settings/mini_i18n.rb', line 61 def clear_cache! @mutex.synchronize { @cache = {} } end |
.t(key, locale: DEFAULT_LOCALE) ⇒ String
Look up a translation by dotted key for the given locale. Falls back to the default locale when the key is missing, and ultimately returns the key itself if no translation is found.
34 35 36 37 |
# File 'lib/super_settings/mini_i18n.rb', line 34 def t(key, locale: DEFAULT_LOCALE) translations = translations_for(locale) translations[key] || translations_for(DEFAULT_LOCALE)[key] || key end |
.text_direction(locale = DEFAULT_LOCALE) ⇒ String
Return the text direction for the given locale. Reads the "dir" key
from the locale's translations hash, falling back to "ltr" when not set.
53 54 55 56 |
# File 'lib/super_settings/mini_i18n.rb', line 53 def text_direction(locale = DEFAULT_LOCALE) dir = translations_for(locale)["dir"] (dir == "rtl") ? "rtl" : "ltr" end |
.translations_for(locale) ⇒ Hash<String, String>
Return the full translation hash for a locale. Used to inline translations into the HTML page for JavaScript consumption.
44 45 46 |
# File 'lib/super_settings/mini_i18n.rb', line 44 def translations_for(locale) load_all_locales[normalize_locale(locale)] || load_all_locales[DEFAULT_LOCALE] || {} end |