Class: GetText::Reloader

Inherits:
Rack::Reloader
  • Object
show all
Defined in:
lib/rack/gettext_reloader.rb

Defined Under Namespace

Modules: Stat

Instance Method Summary collapse

Constructor Details

#initialize(app, path, cooldown = 10) ⇒ Reloader

Returns a new instance of Reloader.



3
4
5
6
7
8
9
10
11
# File 'lib/rack/gettext_reloader.rb', line 3

def initialize(app, path, cooldown=10)
  @path = path
  @po = Dir.glob(File.join(@path, '**', '*.po'))
  @mo = Dir.glob(File.join(@path, '**', '*.mo'))
  @pot = File.join(@path, 'rbigg.pot') # Used for locking
  # If there are not translations, give up
  cooldown = nil unless @po && @mo && @po.size > 0 && @mo.size > 0
  super(app, cooldown, Stat)
end

Instance Method Details

#reload!(stderr = $stderr) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rack/gettext_reloader.rb', line 20

def reload!(stderr = $stderr)
  @translations = 0
  super
  if @translations > 0
    found, stat = safe_stat(@mo.first)
    if found && stat
      File.open(@pot, 'r') do |f|
        # Ensure we don't produce translation twice
        f.flock(File::LOCK_EX)
        found, mstat = safe_stat(@mo.first)
        if found && mstat && mstat.mtime == stat.mtime
          require 'gettext/tools'
          Dir.chdir @path do
            GetText.create_mofiles(:po_root => '.', :mo_root => '.')
          end
        end
        # Reload whole translation
        FastGettext.add_text_domain(FastGettext.text_domain, :path => @path)
        FastGettext.current_cache = {}
      end
    end
  end
end

#safe_load(file, mtime, stderr = $stderr) ⇒ Object



13
14
15
16
17
18
# File 'lib/rack/gettext_reloader.rb', line 13

def safe_load(file, mtime, stderr = $stderr)
  @translations += 1
  stderr.puts "#{self.class}: reloaded `#{FastGettext.text_domain}' translation `#{file}'"
ensure
  @mtimes[file] = mtime
end