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

Class Method Details

.available_localesArray<String>

Return the list of available locale codes derived from the JSON files present in app/locales/.

Returns:

  • (Array<String>)

    locale codes, e.g. ["en"]



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.

Parameters:

  • key (String)

    dotted translation key, e.g. "page.title"

  • locale (String) (defaults to: DEFAULT_LOCALE)

    the locale code (default: DEFAULT_LOCALE)

Returns:

  • (String)

    the translated string



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.

Parameters:

  • locale (String) (defaults to: DEFAULT_LOCALE)

    the locale code

Returns:

  • (String)

    "ltr" or "rtl"



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.

Parameters:

  • locale (String)

    the locale code

Returns:

  • (Hash<String, String>)

    all key/value translations



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