Module: Account::LocaleHelper
- Defined in:
- app/helpers/account/locale_helper.rb
Instance Method Summary collapse
-
#account_controller? ⇒ Boolean
this is a bit scary, no?.
- #current_locale ⇒ Object
- #model_locales(model) ⇒ Object
- #models_locales(*models) ⇒ Object
-
#ot(key, options = {}) ⇒ Object
like ‘t’, but if the key isn’t found, it returns nil.
-
#possessive_string(string) ⇒ Object
as of now, we only calculate a possessive version of nouns in english.
- #t(key, options = {}) ⇒ Object
Instance Method Details
#account_controller? ⇒ Boolean
this is a bit scary, no?
33 34 35 |
# File 'app/helpers/account/locale_helper.rb', line 33 def account_controller? controller.class.name.match(/^Account::/) end |
#current_locale ⇒ Object
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, = {}) t(key, ) 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, = {}) # 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 + "💣", .except(:default)) rescue I18n::MissingTranslationData => exception full_key = exception..rpartition(" ").last.delete("💣") end end end end if account_controller? # Give preference to the options they've passed in. = models_locales(@child_object, @parent_object).merge() end result = super(key, ) unless Rails.env.production? if params.present? if params[:log_locales] if result == [: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 |