Module: Account::LocaleHelper

Defined in:
app/helpers/account/locale_helper.rb

Instance Method Summary collapse

Instance Method Details

#account_controller?Boolean

this is a bit scary, no?

Returns:

  • (Boolean)


33
34
35
# File 'app/helpers/account/locale_helper.rb', line 33

def 
  controller.class.name.match(/^Account::/)
end

#current_localeObject



2
3
4
# File 'app/helpers/account/locale_helper.rb', line 2

def current_locale
  current_user.locale || current_team.locale || "en"
end

#model_locales(model) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'app/helpers/account/locale_helper.rb', line 12

def model_locales(model)
  name = model.label_string.presence
  return {} unless name

  hash = {}
  prefix = model.class.name.split("::").last.underscore
  hash[:"#{prefix}_name"] = name
  hash[:"#{prefix.pluralize}_possessive"] = possessive_string(name)

  hash
end

#models_locales(*models) ⇒ Object



24
25
26
27
28
29
30
# File 'app/helpers/account/locale_helper.rb', line 24

def models_locales(*models)
  hash = {}
  models.compact.each do |model|
    hash.merge! model_locales(model)
  end
  hash
end

#ot(key, options = {}) ⇒ Object

like ‘t’, but if the key isn’t found, it returns nil.



82
83
84
85
86
# File 'app/helpers/account/locale_helper.rb', line 82

def ot(key, options = {})
  t(key, options)
rescue I18n::MissingTranslationData => _
  nil
end

#possessive_string(string) ⇒ Object

as of now, we only calculate a possessive version of nouns in english. if you’re aware of another language where we can do this, please don’t hesitate to reach out!



8
9
10
# File 'app/helpers/account/locale_helper.rb', line 8

def possessive_string(string)
  [:en].include?(I18n.locale) ? string.possessive : string
end

#t(key, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/helpers/account/locale_helper.rb', line 37

def t(key, options = {})
  # When bundled Ruby gems provide a lot of translations, it can be difficult to figure out which strings in the
  # application are coming from where. To help with this, you can add `?debug=true` to any URL and we'll output
  # any rendered strings and their translation keys on the console.
  unless Rails.env.production?
    if params.present?
      if params[:log_locales] || params[:show_locales]
        # Often times we're only receiving partial keys like `.section`, so this is a crazy hack to trick I18n.t into
        # telling us what the full key ended up being.
        begin
          super(key + "💣", options.except(:default))
        rescue I18n::MissingTranslationData => exception
          full_key = exception.message.rpartition(" ").last.delete("💣")
        end
      end
    end
  end

  if 
    # Give preference to the options they've passed in.
    options = models_locales(@child_object, @parent_object).merge(options)
  end

  result = super(key, options)

  unless Rails.env.production?
    if params.present?
      if params[:log_locales]
        if result == options[:default]
          puts "🌐 #{full_key}: Not found? Result matched default: \"#{result}\"".yellow
        else
          puts "🌐 #{full_key}: \"#{result}\"".green
        end
      end

      if params[:show_locales]
        return full_key
      end
    end
  end

  result
end