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

Instance Method Details

#locale_available?(locale) ⇒ Boolean

Check if a locale is available

Parameters:

  • locale (Symbol, String)

    Locale code

Returns:

  • (Boolean)

    True if 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

Parameters:

  • milestone (Hash)

    Milestone configuration

Returns:

  • (String)

    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

Parameters:

  • milestone (Hash)

    Milestone configuration

Returns:

  • (String)

    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

Parameters:

  • step (Hash)

    Step configuration

Returns:

  • (String)

    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

Parameters:

  • step (Hash)

    Step configuration

Returns:

  • (String)

    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_localesArray<Symbol>

Get available locales for onboarding

Returns:

  • (Array<Symbol>)

    Available locale codes



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, **options)
  t_onboarding("actions.#{key}", **options)
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, **options)
  t_onboarding("common.#{key}", **options)
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, **options)
  t_onboarding("errors.#{key}", **options)
end

#t_message(key, **options) ⇒ Object

Translate message text



25
26
27
# File 'app/helpers/rails_onboarding/i18n_helper.rb', line 25

def t_message(key, **options)
  t_onboarding("messages.#{key}", **options)
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, **options)
  t_onboarding("milestones.#{key}", **options)
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, **options)
  t_onboarding("navigation.#{key}", **options)
end

#t_onboarding(key, **options) ⇒ String

Translate a key with the rails_onboarding scope

Parameters:

  • key (String, Symbol)

    Translation key

  • options (Hash)

    Translation options

Returns:

  • (String)

    Translated string



10
11
12
# File 'app/helpers/rails_onboarding/i18n_helper.rb', line 10

def t_onboarding(key, **options)
  I18n.t("rails_onboarding.#{key}", **options)
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, **options)
  t_onboarding("progress.#{key}", **options)
end

#user_locale(user) ⇒ Symbol

Get user's preferred locale

Parameters:

  • user (User)

    User object

Returns:

  • (Symbol)

    Locale code



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

Parameters:

  • user (User)

    User object

Yields:

  • Block to execute 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