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.


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

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

Class Method Details

.encoding2locale(enc) ⇒ Object

FIXME: support non ja_JP locales


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

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

.load(prefix) ⇒ Object


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

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

.load_file(path, locale) ⇒ Object


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

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


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

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


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

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

#translate(key, *args) ⇒ Object


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

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