Class: ReVIEW::I18n

Inherits:
Object show all
Defined in:
lib/review/i18n.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale = nil) ⇒ I18n

Returns a new instance of I18n.



50
51
52
53
# File 'lib/review/i18n.rb', line 50

def initialize(locale = nil)
  @locale = locale
  load_default
end

Instance Attribute Details

#localeObject

Returns the value of attribute locale.



48
49
50
# File 'lib/review/i18n.rb', line 48

def locale
  @locale
end

Class Method Details

.i18n(*args) ⇒ Object

Raises:

  • (NotImplementedError)


24
25
26
# File 'lib/review/i18n.rb', line 24

def self.i18n(*args)
  raise NotImplementedError, "I18n.i18n is obsoleted. Please use I18n.setup(locale, [ymlfile])"
end

.locale=(locale) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/review/i18n.rb', line 32

def self.locale=(locale)
  if @i18n
    @i18n.locale = locale
  else
    I18n.setup(locale)
  end
end

.setup(locale = "ja", ymlfile = "locale.yml") ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/review/i18n.rb', line 6

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

.t(str, args = nil) ⇒ Object



28
29
30
# File 'lib/review/i18n.rb', line 28

def self.t(str, args = nil)
  @i18n.t(str, args)
end

.update(user_i18n, locale = nil) ⇒ Object



44
45
46
# File 'lib/review/i18n.rb', line 44

def self.update(user_i18n, locale = nil)
  @i18n.update(user_i18n, locale)
end

.vObject

for EPUBMaker backward compatibility



41
42
43
# File 'lib/review/i18n.rb', line 41

def self.t(str, args = nil)
  @i18n.t(str, args)
end

Instance Method Details

#load_defaultObject



55
56
57
# File 'lib/review/i18n.rb', line 55

def load_default
  load_file(File.expand_path "i18n.yml", File.dirname(__FILE__))
end

#load_file(path) ⇒ Object



59
60
61
# File 'lib/review/i18n.rb', line 59

def load_file(path)
  @store = YAML.load_file(path)
end

#t(str, args = nil) ⇒ Object



90
91
92
93
94
# File 'lib/review/i18n.rb', line 90

def t(str, args = nil)
  @store[@locale][str] % args
rescue
  str
end

#update(user_i18n, locale = nil) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/review/i18n.rb', line 81

def update(user_i18n, locale = nil)
  locale ||= @locale
  if @store[locale]
    @store[locale].merge!(user_i18n)
  else
    @store[locale] = user_i18n
  end
end

#update_localefile(path) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/review/i18n.rb', line 63

def update_localefile(path)
  user_i18n = YAML.load_file(path)
  locale = user_i18n["locale"]
  if locale
    user_i18n.delete("locale")
    if @store[locale]
      @store[locale].merge!(user_i18n)
    else
      @store[locale] = user_i18n
    end
  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