lib/review/i18n.rb in review-1.4.0 vs lib/review/i18n.rb in review-1.5.0

- old
+ new

@@ -1,33 +1,93 @@ # -*- coding: utf-8 -*- require 'yaml' module ReVIEW class I18n - def self.setup - lfile = File.expand_path "locale.yml", Dir.pwd - # backward compatibility - lfile = File.expand_path "locale.yaml", Dir.pwd unless File.exist?(lfile) - user_i18n = YAML.load_file(lfile) - I18n.i18n user_i18n["locale"], user_i18n - rescue - I18n.i18n "ja" + def self.setup(locale="ja", ymlfile = "locale.yml") + @i18n = ReVIEW::I18n.new(locale) + + lfile = nil + if ymlfile + lfile = File.expand_path(ymlfile, Dir.pwd) + + # backward compatibility + if !File.exist?(lfile) && (ymlfile == "locale.yml") + lfile = File.expand_path("locale.yaml", Dir.pwd) + end + end + + if lfile && File.file?(lfile) + @i18n.update_localefile(lfile) + end end - def self.i18n(locale, user_i18n = {}) - locale ||= "ja" - i18n_yaml_path = File.expand_path "i18n.yml", File.dirname(__FILE__) - @i18n = YAML.load_file(i18n_yaml_path)[locale] + def self.i18n(*args) + raise NotImplementedError, "I18n.i18n is obsoleted. Please use I18n.setup(locale, [ymlfile])" + end + + def self.t(str, args = nil) + @i18n.t(str, args) + end + + def self.locale=(locale) if @i18n - @i18n.merge!(user_i18n) + @i18n.locale = locale + else + I18n.setup(locale) end end - def self.t(str, args = nil) - @i18n[str] % args + class << self + alias v t ## for EPUBMaker backward compatibility + end + + def self.update(user_i18n, locale = nil) + @i18n.update(user_i18n, locale) + end + + attr_accessor :locale + + def initialize(locale = nil) + @locale = locale + load_default + end + + def load_default + load_file(File.expand_path "i18n.yml", File.dirname(__FILE__)) + end + + def load_file(path) + @store = YAML.load_file(path) + end + + def update_localefile(path) + user_i18n = YAML.load_file(path) + locale = user_i18n["locale"] + if locale + user_i18n.delete("locale") + @store[locale].merge!(user_i18n) + else + key = user_i18n.keys.first + if !user_i18n[key].kind_of? Hash + raise KeyError, "Invalid locale file: #{path}" + end + @store.merge!(user_i18n) + end + end + + def update(user_i18n, locale = nil) + locale ||= @locale + if @store[locale] + @store[locale].merge!(user_i18n) + else + @store[locale] = user_i18n + end + end + + def t(str, args = nil) + @store[@locale][str] % args rescue str end end - - I18n.setup end