module Ripl::I18n

Constants

VERSION

Attributes

locales[RW]

Public Class Methods

default_locale() click to toggle source
# File lib/ripl/i18n.rb, line 34
def default_locale
  ( lang = ENV["LANG"] && ENV["LANG"][0,2] ) &&
    File.exists?( File.dirname(__FILE__) + "/i18n/locales/#{lang}.yml" ) ?
    lang.to_sym : :en
end
init() click to toggle source
# File lib/ripl/i18n.rb, line 21
def init
  load Dir["#{File.dirname(__FILE__)}/i18n/locales/*.yml"]
  internationalize
end
internationalize() click to toggle source
# File lib/ripl/i18n.rb, line 40
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) click to toggle source
# File lib/ripl/i18n.rb, line 26
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) click to toggle source
# File lib/ripl/i18n.rb, line 15
def load_file(file)
  YAML.load_file(file)
rescue StandardError, SyntaxError
  warn "Error while loading locale from #{file}:\n#{$!}"
end
locale() click to toggle source
# File lib/ripl/i18n.rb, line 9
def locale; Ripl.config[:i18n_locale]; end
translate(str) click to toggle source
# File lib/ripl/i18n.rb, line 11
def translate(str)
  (locales[locale] ||= {})[str] || "Translation missing: #{locale}.#{str}"
end