Module: MI18n

Defined in:
lib/merb_babel/m_i18n.rb

Class Method Summary collapse

Class Method Details

.lookup(options) ⇒ Object

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/merb_babel/m_i18n.rb', line 3

def self.lookup(options)
  keys = [options[:keys].map{|key| key.to_s}].flatten
  language = options[:language]
  country = options[:country]

  raise ArgumentError, "You need to pass a language reference" unless language
  raise ArgumentError, "You need to pass a localization key" if keys.empty?
  unless ML10n.localizations[language]
    language = Merb::Plugins.config[:merb_babel][:default_language]
  end
  raise ArgumentError,
    "language: #{language} not found" unless ML10n.localizations[language]
  
  full_location = nil
  full_location = lookup_with_full_locale(keys, language, country) if country
  
  if full_location
    return full_location
  else
    return lookup_with_language(keys, language) || keys.last
  end
end

.lookup_with_full_locale(keys, language, country) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/merb_babel/m_i18n.rb', line 30

def self.lookup_with_full_locale(keys, language, country)
  if ML10n.localizations.has_key?(language)
    ML10n.localizations[language].has_key?(country) ?
      lookup_with_hash(keys, ML10n.localizations[language][country]) : nil 
  else
    nil
  end
end

.lookup_with_hash(keys, l_hash) ⇒ Object



39
40
41
# File 'lib/merb_babel/m_i18n.rb', line 39

def self.lookup_with_hash(keys, l_hash)
  keys.inject(l_hash){|h,k| h[k] rescue nil}
end

.lookup_with_language(keys, language) ⇒ Object



26
27
28
# File 'lib/merb_babel/m_i18n.rb', line 26

def self.lookup_with_language(keys, language)
  lookup_with_hash(keys, ML10n.localizations[language])
end