Module: Workarea::I18n

Defined in:
lib/workarea/i18n.rb

Defined Under Namespace

Modules: DefaultUrlOptions

Class Method Summary collapse

Class Method Details

.configured_localesArray<Symbol>

Returns an array representing the locales configured by the host Rails application. This is distinctly different from ::I18n.available_locales so that we can ignore locales that gems add. Usually the host app does not intend to use locales added by gems.

Returns:

  • (Array<Symbol>)


18
19
20
21
# File 'lib/workarea/i18n.rb', line 18

def configured_locales
  Rails.application.config.i18n.available_locales ||
    [Rails.application.config.i18n.default_locale]
end

.for_each_localeObject

Execute the block passed for each configured available locale. It uses the available locales from the Rails config over the I18n module because gems may add to the I18n.available_locales which the app doesn’t intend to use.



28
29
30
31
32
33
34
# File 'lib/workarea/i18n.rb', line 28

def for_each_locale
  configured_locales.each do |locale|
    ::I18n.with_locale(locale) do
      yield(locale)
    end
  end
end

.method_missing(method, *args, &block) ⇒ Object

Delegate all other methods to the global I18n.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/workarea/i18n.rb', line 54

def method_missing(method, *args, &block)
  if ::I18n.respond_to?(method)
    self.class.send(:define_method, method) do |*arguments, &blok|
      ::I18n.send(method, *arguments, &blok)
    end

    send(method, *args, &block)
  else
    super
  end
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/workarea/i18n.rb', line 66

def respond_to_missing?(method_name, include_private = false)
  super || ::I18n.respond_to?(method_name)
end

.routes_constraintHash

Returns the value for the constraint of the :locale param in routing, with respect to the current env and currently configured locales.

When testing, we don’t want any real constraints so we can freely configure and test that i18n features work correctly.

Returns:

  • (Hash)


44
45
46
47
48
49
50
# File 'lib/workarea/i18n.rb', line 44

def routes_constraint
  if Rails.env.test?
    { locale: /\w{2}/ }
  else
    { locale: Regexp.new(available_locales.join('|')) }
  end
end