Module: Trestle::I18nHelper

Defined in:
app/helpers/trestle/i18n_helper.rb

Overview

Internal

Constant Summary collapse

FLATPICKR_LOCALE_CONVERSIONS =
{ ca: "cat", el: "gr", nb: "no", vi: "vn" }.stringify_keys

Instance Method Summary collapse

Instance Method Details

#flatpickr_locale(locale) ⇒ Object

Returns the Flatpickr locale code corresponding to the given locale, as some of their codes are different to the Rails’ counterparts.



49
50
51
# File 'app/helpers/trestle/i18n_helper.rb', line 49

def flatpickr_locale(locale)
  FLATPICKR_LOCALE_CONVERSIONS.fetch(locale.to_s) { locale }
end

#i18n_fallbacks(locale = I18n.locale) ⇒ Object

Returns the I18n fallbacks for the given locale.

This is used to determine which locale files to include.

Examples

i18n_fallbacks("pt-BR") => ["pt-BR", "pt"]
i18n_fallbacks("ca") => ["ca", "es-ES", "es"] %>

Returns an array of locale Strings.



16
17
18
19
20
21
22
23
24
25
# File 'app/helpers/trestle/i18n_helper.rb', line 16

def i18n_fallbacks(locale=I18n.locale)
  if I18n.respond_to?(:fallbacks)
    I18n.fallbacks[locale].map(&:to_s)
  elsif locale.to_s.include?("-")
    fallback = locale.to_s.split("-").first
    [locale, fallback]
  else
    [locale]
  end
end

#i18n_javascript_translations(keys = Trestle.config.javascript_i18n_keys) ⇒ Object

Returns a nested hash of I18n key/value pairs for passing to JS.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/helpers/trestle/i18n_helper.rb', line 28

def i18n_javascript_translations(keys=Trestle.config.javascript_i18n_keys)
  result = {}

  keys.each do |key|
    if translation = I18n.t(key, default: nil)
      *parts, last = key.split(".")

      # Create nesting from period-separated i18n key
      target = parts.inject(result) { |hash, part|
        hash[part] ||= {}
      }

      target[last] = translation
    end
  end

  result
end