Module: LanguageSwitcher

Included in:
Helpers
Defined in:
lib/language_switcher.rb

Defined Under Namespace

Modules: Helpers

Instance Method Summary collapse

Instance Method Details

#attribute_translation(object_name, field, default = '', interpolation = {}) ⇒ Object



2
3
4
# File 'lib/language_switcher.rb', line 2

def attribute_translation(object_name, field, default='', interpolation={})
  I18n.t field, interpolation.merge({:scope => [:activerecord, :attributes, object_name], :default => default})
end

#each_languageObject



6
7
8
9
10
# File 'lib/language_switcher.rb', line 6

def each_language
  I18n.available_locales.each do |l|
    language(l){ yield l }
  end
end

#language(l) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/language_switcher.rb', line 12

def language(l)
  current = I18n.locale
  if I18n.available_locales.include? l
    I18n.locale = l
    yield
    I18n.locale = current
  else
    raise "I18n.available_locales does not contain #{l}"
  end
end

#t(key, interpolations = {}) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/language_switcher.rb', line 23

def t(key, interpolations={})
  begin
    I18n.translate!(key, interpolations.merge({:scope => [:views, :defaults]}) )
  rescue
    I18n.translate!(key, interpolations.merge({:scope => [:views, controller_name]}) )
  end
end