Class: Mentawai::I18N::LocaleManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mentawai/i18n/loc_manager.rb

Instance Method Summary collapse

Constructor Details

#initializeLocaleManager



9
10
11
# File 'lib/mentawai/i18n/loc_manager.rb', line 9

def initialize
  @locales = Array.new
end

Instance Method Details

#add(loc) ⇒ Object



13
14
15
# File 'lib/mentawai/i18n/loc_manager.rb', line 13

def add(loc)
  @locales.add(loc)
end

#defaultObject



21
22
23
24
25
26
27
# File 'lib/mentawai/i18n/loc_manager.rb', line 21

def default
  if @locales.empty?
    'en_US'
  else
    @locales[0]
  end
end

#get(locale, return_default = true) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mentawai/i18n/loc_manager.rb', line 29

def get(locale, return_default = true)
  
  if locale.nil?
    return return_default ? default : nil
  end
  
  # If array, try to match any of them
  if locale.is_a?(Array)
    locale.each do |loc|
      l = get(loc, false) # recursive no default here...
      return l if l
    end
    return return_default ? default : nil 
  end
  
  # First match the whole thing
  @locales.each do |loc|
    return loc if locale == loc 
  end
  # Now match only the language
  l = locale.split(/_/)[0]
  @locales.each do |loc|
    l2 = loc.split(/_/)[0]
    return loc if l2 == l
  end
  # Return default or the given locale
  return_default ? default : nil
end

#localesObject



17
18
19
# File 'lib/mentawai/i18n/loc_manager.rb', line 17

def locales
  @locales
end