Module: Tounge::LocaleSetter

Defined in:
lib/tounge/locale_setter.rb

Constant Summary collapse

LOOKUP_CHAIN =
[:params, :user, :session, :http, :default]
@@lookup_chain =
LOOKUP_CHAIN

Class Method Summary collapse

Class Method Details

.by_default(default = I18n.default_locale) ⇒ Object



28
29
30
# File 'lib/tounge/locale_setter.rb', line 28

def self.by_default(default = I18n.default_locale)
  default
end

.by_http(accepts) ⇒ Object



45
46
47
# File 'lib/tounge/locale_setter.rb', line 45

def self.by_http(accepts)
  (accept_list(accepts) & I18n.available_locales).first
end

.by_params(params) ⇒ Object



32
33
34
35
# File 'lib/tounge/locale_setter.rb', line 32

def self.by_params(params)
  locale = params[:locale]
  locale if locale_available?(locale)
end

.by_session(session) ⇒ Object



37
38
39
# File 'lib/tounge/locale_setter.rb', line 37

def self.by_session(session)
  session[:locale]
end

.by_user(user) ⇒ Object



41
42
43
# File 'lib/tounge/locale_setter.rb', line 41

def self.by_user(user)
  user.locale
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



9
10
11
# File 'lib/tounge/locale_setter.rb', line 9

def self.configure
  yield self
end

.locale_available?(locale) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/tounge/locale_setter.rb', line 49

def self.locale_available?(locale)
  I18n.available_locales.map(&:to_s).include?(locale)
end

.lookup_chainObject



13
14
15
# File 'lib/tounge/locale_setter.rb', line 13

def self.lookup_chain
  @@lookup_chain
end

.set_by(inputs) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/tounge/locale_setter.rb', line 17

def self.set_by(inputs)
  locale = nil
  lookup_chain.each do |lookup|
    if inputs[lookup]
      locale = send("by_#{lookup}", inputs[lookup])
      break if locale
    end
  end
  I18n.locale = locale
end