summaryrefslogtreecommitdiffstats
path: root/vendor/plugins/gloc-1.1.0/lib/gloc-dev.rb
blob: cb12b4cb30ea5bbff03cc9c36b40631005691c86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Copyright (c) 2005-2006 David Barri

puts "GLoc v#{GLoc::VERSION} running in development mode. Strings can be modified at runtime."

module GLoc
  class << self
  
    alias :actual_add_localized_strings :add_localized_strings
    def add_localized_strings(lang, symbol_hash, override=true, strings_charset=nil)
      _verbose_msg {"dev::add_localized_strings #{lang}, [#{symbol_hash.size}], #{override}, #{strings_charset ? strings_charset : 'nil'}"}
      STATE.push [:hash, lang, {}.merge(symbol_hash), override, strings_charset]
      _force_refresh
    end
    
    alias :actual_load_localized_strings :load_localized_strings
    def load_localized_strings(dir=nil, override=true)
      _verbose_msg {"dev::load_localized_strings #{dir ? dir : 'nil'}, #{override}"}
      STATE.push [:dir, dir, override]
      _get_lang_file_list(dir).each {|filename| FILES[filename]= nil}
    end
  
    alias :actual_clear_strings :clear_strings
    def clear_strings(*languages)
      _verbose_msg {"dev::clear_strings #{languages.map{|l|l.to_s}.join(', ')}"}
      STATE.push [:clear, languages.clone]
      _force_refresh
    end
    
    alias :actual_clear_strings_except :clear_strings_except
    def clear_strings_except(*languages)
      _verbose_msg {"dev::clear_strings_except #{languages.map{|l|l.to_s}.join(', ')}"}
      STATE.push [:clear_except, languages.clone]
      _force_refresh
    end
    
    # Replace methods
    [:_l, :_l_rule, :_l_has_string?, :similar_language, :valid_languages, :valid_language?].each do |m|
      class_eval <<-EOB
        alias :actual_#{m} :#{m}
        def #{m}(*args)
          _assert_gloc_strings_up_to_date
          actual_#{m}(*args)
        end
      EOB
    end
    
    #-------------------------------------------------------------------------
    private
    
    STATE= []
    FILES= {}
  
    def _assert_gloc_strings_up_to_date
      changed= @@force_refresh
      
      # Check if any lang files have changed
      unless changed
        FILES.each_pair {|f,mtime|
          changed ||= (File.stat(f).mtime != mtime)
        }
      end

      return unless changed
      puts "GLoc reloading strings..."
      @@force_refresh= false
      
      # Update file timestamps
      FILES.each_key {|f|
        FILES[f]= File.stat(f).mtime
      }
      
      # Reload strings
      actual_clear_strings
      STATE.each {|s|
        case s[0]
        when :dir          then actual_load_localized_strings s[1], s[2]
        when :hash         then actual_add_localized_strings s[1], s[2], s[3], s[4]
        when :clear        then actual_clear_strings(*s[1])
        when :clear_except then actual_clear_strings_except(*s[1])
        else raise "Invalid state id: '#{s[0]}'"
        end
      }
      _verbose_msg :stats
    end
    
    @@force_refresh= false
    def _force_refresh
      @@force_refresh= true
    end
    
    alias :super_get_internal_state_vars :_get_internal_state_vars
    def _get_internal_state_vars
      super_get_internal_state_vars + [ STATE, FILES ]
    end
    
  end
end