Module: RailsOnboarding::I18nHelper
- Defined in:
- app/helpers/rails_onboarding/i18n_helper.rb
Overview
I18n Helper Module Provides convenient methods for internationalization
Instance Method Summary collapse
-
#locale_available?(locale) ⇒ Boolean
Check if a locale is available.
-
#localized_milestone_description(milestone) ⇒ String
Get localized milestone description.
-
#localized_milestone_title(milestone) ⇒ String
Get localized milestone title.
-
#localized_step_description(step) ⇒ String
Get localized step description.
-
#localized_step_title(step) ⇒ String
Get localized step title.
-
#onboarding_locales ⇒ Array<Symbol>
Get available locales for onboarding.
-
#t_action(key, **options) ⇒ Object
Translate action text.
-
#t_common(key, **options) ⇒ Object
Translate common text.
-
#t_error(key, **options) ⇒ Object
Translate error text.
-
#t_message(key, **options) ⇒ Object
Translate message text.
-
#t_milestone(key, **options) ⇒ Object
Translate milestone text.
-
#t_nav(key, **options) ⇒ Object
Translate navigation text.
-
#t_onboarding(key, **options) ⇒ String
Translate a key with the rails_onboarding scope.
-
#t_progress(key, **options) ⇒ Object
Translate progress text.
-
#user_locale(user) ⇒ Symbol
Get user's preferred locale.
-
#with_user_locale(user) { ... } ⇒ Object
Execute block with user's locale.
Instance Method Details
#locale_available?(locale) ⇒ Boolean
Check if a locale is available
102 103 104 105 |
# File 'app/helpers/rails_onboarding/i18n_helper.rb', line 102 def locale_available?(locale) I18n.available_locales.include?(locale.to_sym) && I18n.exists?("rails_onboarding", locale: locale) end |
#localized_milestone_description(milestone) ⇒ String
Get localized milestone description
83 84 85 86 87 |
# File 'app/helpers/rails_onboarding/i18n_helper.rb', line 83 def localized_milestone_description(milestone) return milestone[:description] unless milestone[:description_key] I18n.t(milestone[:description_key], default: milestone[:description] || "") end |
#localized_milestone_title(milestone) ⇒ String
Get localized milestone title
73 74 75 76 77 |
# File 'app/helpers/rails_onboarding/i18n_helper.rb', line 73 def localized_milestone_title(milestone) return milestone[:title] unless milestone[:title_key] I18n.t(milestone[:title_key], default: milestone[:title]) end |
#localized_step_description(step) ⇒ String
Get localized step description
63 64 65 66 67 |
# File 'app/helpers/rails_onboarding/i18n_helper.rb', line 63 def localized_step_description(step) return step[:description] unless step[:description_key] I18n.t(step[:description_key], default: step[:description] || "") end |
#localized_step_title(step) ⇒ String
Get localized step title
53 54 55 56 57 |
# File 'app/helpers/rails_onboarding/i18n_helper.rb', line 53 def localized_step_title(step) return step[:title] unless step[:title_key] I18n.t(step[:title_key], default: step[:title]) end |
#onboarding_locales ⇒ Array<Symbol>
Get available locales for onboarding
92 93 94 95 96 |
# File 'app/helpers/rails_onboarding/i18n_helper.rb', line 92 def onboarding_locales I18n.available_locales.select do |locale| I18n.exists?("rails_onboarding", locale: locale) end end |
#t_action(key, **options) ⇒ Object
Translate action text
20 21 22 |
# File 'app/helpers/rails_onboarding/i18n_helper.rb', line 20 def t_action(key, **) t_onboarding("actions.#{key}", **) end |
#t_common(key, **options) ⇒ Object
Translate common text
35 36 37 |
# File 'app/helpers/rails_onboarding/i18n_helper.rb', line 35 def t_common(key, **) t_onboarding("common.#{key}", **) end |
#t_error(key, **options) ⇒ Object
Translate error text
30 31 32 |
# File 'app/helpers/rails_onboarding/i18n_helper.rb', line 30 def t_error(key, **) t_onboarding("errors.#{key}", **) end |
#t_message(key, **options) ⇒ Object
Translate message text
25 26 27 |
# File 'app/helpers/rails_onboarding/i18n_helper.rb', line 25 def (key, **) t_onboarding("messages.#{key}", **) end |
#t_milestone(key, **options) ⇒ Object
Translate milestone text
45 46 47 |
# File 'app/helpers/rails_onboarding/i18n_helper.rb', line 45 def t_milestone(key, **) t_onboarding("milestones.#{key}", **) end |
#t_nav(key, **options) ⇒ Object
Translate navigation text
15 16 17 |
# File 'app/helpers/rails_onboarding/i18n_helper.rb', line 15 def t_nav(key, **) t_onboarding("navigation.#{key}", **) end |
#t_onboarding(key, **options) ⇒ String
Translate a key with the rails_onboarding scope
10 11 12 |
# File 'app/helpers/rails_onboarding/i18n_helper.rb', line 10 def t_onboarding(key, **) I18n.t("rails_onboarding.#{key}", **) end |
#t_progress(key, **options) ⇒ Object
Translate progress text
40 41 42 |
# File 'app/helpers/rails_onboarding/i18n_helper.rb', line 40 def t_progress(key, **) t_onboarding("progress.#{key}", **) end |
#user_locale(user) ⇒ Symbol
Get user's preferred locale
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'app/helpers/rails_onboarding/i18n_helper.rb', line 111 def user_locale(user) return I18n.default_locale unless user # Try to get locale from user user_locale = if user.respond_to?(:locale) user.locale elsif user.respond_to?(:language) user.language elsif user.respond_to?(:preferred_language) user.preferred_language end return I18n.default_locale unless user_locale locale_sym = user_locale.to_sym locale_available?(locale_sym) ? locale_sym : I18n.default_locale end |
#with_user_locale(user) { ... } ⇒ Object
Execute block with user's locale
133 134 135 |
# File 'app/helpers/rails_onboarding/i18n_helper.rb', line 133 def with_user_locale(user, &block) I18n.with_locale(user_locale(user), &block) end |