Sha256: 38c0ceca7fc75d6a7cef6a3b12911e45398cba00137592ae10e7f6d3cbc0478d

Contents?: true

Size: 1.91 KB

Versions: 12

Compression:

Stored size: 1.91 KB

Contents

#
# bitclust/messagecatalog.rb
#
# Copyright (c) 2006-2008 Minero Aoki
#
# This program is free software.
# You can distribute/modify this program under the Ruby License.
#

module BitClust

  module Translatable

    private

    def init_message_catalog(catalog)
      @__message_catalog = catalog
    end

    def message_catalog
      @__message_catalog
    end

    def _(key, *args)
      @__message_catalog.translate(key, *args)
    end

  end

  # FIXME: support automatic encoding-conversion
  class MessageCatalog

    ENCODING_MAP = {
      'utf-8' => 'UTF-8',
      'euc-jp' => 'EUC-JP',
      'shift_jis' => 'Shift_JIS'
    }

    # FIXME: support non ja_JP locales
    def MessageCatalog.encoding2locale(enc)
      newenc = ENCODING_MAP[enc.downcase]
      newenc ? "ja_JP.#{newenc}" : "C"
    end

    def MessageCatalog.load(prefix)
      load_with_locales(prefix, env_locales())
    end

    def MessageCatalog.load_with_locales(prefix, locales)
      path, loc = find_catalog(prefix, locales)
      path ? load_file(path, loc) : new({}, 'C')
    end

    def MessageCatalog.env_locales
      [ENV['LC_MESSAGES'], ENV['LC_ALL'], ENV['LANG'], 'C']\
          .compact.uniq.reject {|loc| loc.empty? }
    end
    private_class_method :env_locales

    def MessageCatalog.find_catalog(prefix, locales)
      locales.each do |locale|
        path = "#{prefix}/#{locale}"
        return path, locale if File.file?(path)
      end
      nil
    end
    private_class_method :find_catalog

    def MessageCatalog.load_file(path, locale)
      h = {}
      fopen(path, 'r:UTF-8') {|f|
        f.each do |key|
          h[key.chomp] = f.gets.chomp
        end
      }
      new(h, locale)
    end

    def initialize(msgs, locale)
      @msgs = msgs
      @locale = locale
    end

    def inspect
      "\#<#{self.class} #{@locale}>"
    end

    def translate(key, *args)
      str = @msgs[key] || key
      sprintf(str, *args)
    end

  end

end

Version data entries

12 entries across 8 versions & 1 rubygems

Version Path
bitclust-core-0.7.0 lib/bitclust/messagecatalog.rb
bitclust-core-0.6.0 lib/bitclust/messagecatalog.rb
bitclust-core-0.5.5 lib/bitclust/messagecatalog.rb
bitclust-core-0.5.4 lib/bitclust/messagecatalog.rb
bitclust-core-0.5.3 vendor/bundle/ruby/1.9.1/gems/bitclust-core-0.5.3/vendor/bundle/ruby/1.9.1/gems/bitclust-core-0.5.1/lib/bitclust/messagecatalog.rb
bitclust-core-0.5.3 vendor/bundle/ruby/1.9.1/gems/bitclust-core-0.5.1/lib/bitclust/messagecatalog.rb
bitclust-core-0.5.3 lib/bitclust/messagecatalog.rb
bitclust-core-0.5.3 vendor/bundle/ruby/1.9.1/gems/bitclust-core-0.5.3/lib/bitclust/messagecatalog.rb
bitclust-core-0.5.2 lib/bitclust/messagecatalog.rb
bitclust-core-0.5.2 vendor/bundle/ruby/1.9.1/gems/bitclust-core-0.5.1/lib/bitclust/messagecatalog.rb
bitclust-core-0.5.1 lib/bitclust/messagecatalog.rb
bitclust-core-0.5.0 lib/bitclust/messagecatalog.rb