Class: BitClust::MessageCatalog

Inherits:
Object
  • Object
show all
Defined in:
lib/bitclust/messagecatalog.rb

Overview

Tiny implementation of message catalog (I18N) see data/bitclust/catalog/ja_JP.UTF-8

FIXME: support automatic encoding-conversion

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msgs, locale) ⇒ MessageCatalog

Returns a new instance of MessageCatalog.



84
85
86
87
# File 'lib/bitclust/messagecatalog.rb', line 84

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

Class Method Details

.encoding2locale(enc) ⇒ Object

FIXME: support non ja_JP locales



45
46
47
48
# File 'lib/bitclust/messagecatalog.rb', line 45

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

.load(prefix) ⇒ Object



50
51
52
# File 'lib/bitclust/messagecatalog.rb', line 50

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

.load_file(path, locale) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/bitclust/messagecatalog.rb', line 74

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

.load_with_locales(prefix, locales) ⇒ Object



54
55
56
57
# File 'lib/bitclust/messagecatalog.rb', line 54

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

Instance Method Details

#inspectObject



89
90
91
# File 'lib/bitclust/messagecatalog.rb', line 89

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

#translate(key, *args) ⇒ Object



93
94
95
96
# File 'lib/bitclust/messagecatalog.rb', line 93

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