fluke/devops-client/lib/devops-client/i18n.rb

62 lines
1.3 KiB
Ruby
Raw Normal View History

2014-06-20 12:59:17 +04:00
# encoding: UTF-8
2014-05-08 15:34:26 +04:00
module I18n
@@lang = {}
@@locale = ''
2014-05-08 15:34:26 +04:00
def self.language= locale
@locale = locale
2014-05-08 15:34:26 +04:00
spec = Gem::Specification.find_by_name(DevopsClient::NAME)
gem_root = spec.gem_dir
path = File.join(gem_root, "locales", "#{locale}.yml")
raise ArgumentError.new("Invalid locale '#{locale}'") unless File.exist?(path)
require 'yaml'
begin
@@lang = YAML.load_file(path)[locale]
rescue
raise ArgumentError.new("Invalid file '#{locale}.yml'")
end
end
def self.t label, options={}
path = label.split(".")
buf = @@lang
begin
path.each do |index|
buf = buf[index]
end
raise ArgumentError unless buf.is_a?(String)
rescue
return "Translation missing"
end
2015-11-12 20:18:55 +03:00
buf = buf.dup
2014-05-08 15:34:26 +04:00
options.each do |k,v|
buf.gsub!("%{#{k.to_s}}", v.to_s)
end
buf
end
def self.locales
spec = Gem::Specification.find_by_name(DevopsClient::NAME)
gem_root = spec.gem_dir
path = File.join(gem_root, "locales")
locales = []
Dir.foreach(path) do |item|
next if item.start_with? '.'
if item.end_with? ".yml"
locales.push item.split(".")[0]
end
end
locales
end
def self.lang
@@lang
end
def self.locale
@locale
end
2014-05-08 15:34:26 +04:00
end