Sha256: a20b39b2cf4b2e884a8345f65ec593c873070517678c6d632fcc8e9eeb1b12a7

Contents?: true

Size: 1.96 KB

Versions: 2

Compression:

Stored size: 1.96 KB

Contents

# -*- coding: utf-8 -*-
require 'yaml'

module ReVIEW
  class I18n
    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(*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.locale = locale
      else
        I18n.setup(locale)
      end
    end

    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
        user_i18n.each do |key, values|
          raise KeyError, "Invalid locale file: #{path}" unless values.kind_of? Hash
          @store[key].merge!(values)
        end
      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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
review-1.6.0 lib/review/i18n.rb
review-2.0.0.beta1 lib/review/i18n.rb