Module: ML10n

Defined in:
lib/merb_babel/m_l10n.rb

Constant Summary collapse

LANGUAGE_CODE_KEY =
'mloc_language_code'.freeze
COUNTRY_CODE_KEY =
'mloc_country_code'.freeze

Class Method Summary collapse

Class Method Details

.add_localization_dir(dir_path) ⇒ Object

add a dir to look for localizations



35
36
37
38
39
40
41
42
# File 'lib/merb_babel/m_l10n.rb', line 35

def add_localization_dir(dir_path)
  return ML10n.localization_dirs if dir_path.empty?
  unless ML10n.localization_dirs.include?(dir_path)
    ML10n.localization_dirs << dir_path
    ML10n.reload_localization_files! 
  end
  return ML10n.localization_dirs
end

.find_localization_filesObject

go through the localization dirs and find the localization files



62
63
64
65
66
67
68
69
70
# File 'lib/merb_babel/m_l10n.rb', line 62

def find_localization_files
  l_files = []
  ML10n.localization_dirs.map do |l_dir|
    Dir["#{l_dir}/*"].each do |file|
      l_files << file unless l_files.include?(file)
    end
  end
  return l_files
end

.load_localization!Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/merb_babel/m_l10n.rb', line 44

def load_localization!

  # look for localization files directly just in case new files were added
  ML10n.reset_localization_files! 
  ML10n.find_localization_files.each do |l_file|
    begin
      l_hash = YAML.load_file(l_file)#.symbolize_keys
    rescue Exception => e
      # might raise a real error here in the future
      p e.inspect
    else
      load_localization_hash(l_hash)
    end
  end

end

.localization_dirsObject

locations to look for localization files



30
31
32
# File 'lib/merb_babel/m_l10n.rb', line 30

def localization_dirs
  @@localization_dirs ||= Merb::Plugins.config[:merb_babel][:localization_dirs].dup
end

.localization_filesObject

files containing localizations



25
26
27
# File 'lib/merb_babel/m_l10n.rb', line 25

def localization_files
  @@localization_files ||= ML10n.find_localization_files
end

.localizationsObject

all localizations are saved in this class variable localizations are namespaced using the language or locale they belong to

for instance:

ML10n.localizations['en'][:right] => 'right'
ML10n.localizations['en'][:left] => 'left'
ML10n.localizations['en']['US'][:greeting] => 'Howdie'
ML10n.localizations['en']['AU'][:greeting] => "Good'ay"

locales, including languages and countries use string keys while localization keys themselves are symbols



20
21
22
# File 'lib/merb_babel/m_l10n.rb', line 20

def localizations
  @@localizations ||= {}
end

.localize(key, *options) ⇒ Object

localization helper



101
102
103
# File 'lib/merb_babel/m_l10n.rb', line 101

def localize(key, *options)
  MI18n.localize(options.merge({:locale => locale}))
end

.localize_time(object, format, options) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/merb_babel/m_l10n.rb', line 105

def localize_time(object, format, options)
  table_for = proc do |table_name, key, default|
    keys = ["DateFormat", table_name]
    table = MI18n.lookup(options.merge(:keys => keys)) || {}
    case table
    when Hash, Array; table[key] || default
    else default
    end
  end
  format = format.to_s.dup
  format.gsub!(/%a/) do
    table_for["AbbrDayNames", object.wday, "%a"]
  end
  format.gsub!(/%A/) do
    table_for["DayNames", object.wday, "%A"]
  end
  format.gsub!(/%b/) do
    table_for["AbbrMonthNames", object.mon - 1, "%b"]
  end
  format.gsub!(/%B/) do
    table_for["MonthNames", object.mon - 1, "%B"]
  end
  format.gsub!(/%p/) do
    table_for["AmPm", object.hour < 12 ? 0 : 1, "%p"]
  end if object.respond_to?(:hour)
  format.gsub!(/%\{([a-zA-Z]\w*)\}/) do
    object.send($1) rescue $1
  end
  object.strftime(format)
end

.reload_localization_files!Object



95
96
97
98
# File 'lib/merb_babel/m_l10n.rb', line 95

def reload_localization_files!
  ML10n.reset_localization_files!
  ML10n.find_localization_files
end

.reset_localization_dirs!Object



82
83
84
# File 'lib/merb_babel/m_l10n.rb', line 82

def reset_localization_dirs!
  @@localization_dirs = nil
end

.reset_localization_files!Object



86
87
88
89
# File 'lib/merb_babel/m_l10n.rb', line 86

def reset_localization_files!
  @@localization_files = nil
  ML10n.find_localization_files
end

.reset_localization_files_and_dirs!Object

reset the localization dirs and files to the plugin config careful when using this method since it will remove any localization files you loaded after the plugin started

note that it won’t clear the localization already loaded in memory



77
78
79
80
# File 'lib/merb_babel/m_l10n.rb', line 77

def reset_localization_files_and_dirs!
  ML10n.reset_localization_dirs!
  ML10n.reset_localization_files!
end

.reset_localizations!Object



91
92
93
# File 'lib/merb_babel/m_l10n.rb', line 91

def reset_localizations!
  @@localizations = {}
end