Module: Ripl::I18n

Defined in:
lib/ripl/i18n/version.rb,
lib/ripl/i18n.rb

Constant Summary collapse

VERSION =
'0.3.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.localesObject

Returns the value of attribute locales.



8
9
10
# File 'lib/ripl/i18n.rb', line 8

def locales
  @locales
end

Class Method Details

.default_localeObject



35
36
37
38
39
# File 'lib/ripl/i18n.rb', line 35

def default_locale
  ( lang = ENV["LANG"] && ENV["LANG"][0,2] ) &&
    File.exists?( File.dirname(__FILE__) + "/i18n/locales/#{lang}.yml" ) ?
    lang.to_sym : :en
end

.initObject



22
23
24
25
# File 'lib/ripl/i18n.rb', line 22

def init
  load Dir["#{File.dirname(__FILE__)}/i18n/locales/*.yml"]
  internationalize
end

.internationalizeObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ripl/i18n.rb', line 41

def internationalize
  Ripl::Runner::OPTIONS.each { |opt, val| locales[:en][opt] = val[1] }
  Ripl::Runner::OPTIONS.instance_eval %[
    def [](v) [dup[v][0], Ripl::I18n.translate(v)] end
    def values() map {|k,v| [v[0], Ripl::I18n.translate(k)] } end
  ]
  [Ripl::Runner::MESSAGES, Ripl::Shell::MESSAGES].each do |obj|
    obj.each { |opt, val| locales[:en][opt] = val }
    obj.instance_eval %[def [](v) Ripl::I18n.translate(v) end]
  end
end

.load(*files) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/ripl/i18n.rb', line 27

def load(*files)
  files.flatten.each do |file|
    lang = File.basename(file)[/^[a-zA-Z]+/]
    next if lang.nil? || lang.empty?
    locales[lang.to_sym] = load_file(file)
  end
end

.load_file(file) ⇒ Object



16
17
18
19
20
# File 'lib/ripl/i18n.rb', line 16

def load_file(file)
  YAML.load_file(file)
rescue StandardError, SyntaxError
  warn "Error while loading locale from #{file}:\n#{$!}"
end

.localeObject



10
# File 'lib/ripl/i18n.rb', line 10

def locale; Ripl.config[:i18n_locale]; end

.translate(str) ⇒ Object



12
13
14
# File 'lib/ripl/i18n.rb', line 12

def translate(str)
  (locales[locale] ||= {})[str] || "Translation missing: #{locale}.#{str}"
end