Class: Gonzui::CatalogValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/gonzui/gettext.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_file_name, messages) ⇒ CatalogValidator

Returns a new instance of CatalogValidator.



64
65
66
67
68
# File 'lib/gonzui/gettext.rb', line 64

def initialize(source_file_name, messages)
  @source_file_name  = source_file_name
  @gettext_catalog = messages
  @error_messages = []
end

Instance Attribute Details

#error_messagesObject (readonly)

Returns the value of attribute error_messages.



69
70
71
# File 'lib/gonzui/gettext.rb', line 69

def error_messages
  @error_messages
end

Instance Method Details

#collect_messages(content) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/gonzui/gettext.rb', line 80

def collect_messages(content)
  messages = []
  while content.sub!(/\bN?_\[(\d+)\]\(("(?:\\"|.)*?").*?\)/m, "")
    lineno  = $1.to_i
    message = eval($2)
    messages.push([lineno, message])
  end
  messages
end

#ok?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/gonzui/gettext.rb', line 109

def ok?
  @error_messages.empty?
end

#read_file_with_numbering(file_name) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/gonzui/gettext.rb', line 71

def read_file_with_numbering(file_name)
  content = ''
  File.open(file_name).each_with_index {|line, idx|
    lineno = idx + 1
    content << line.gsub(/\bN?_\(/, "_[#{lineno}](")
  }
  content
end

#validateObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/gonzui/gettext.rb', line 90

def validate
  @gettext_catalog or return
  content = read_file_with_numbering(@source_file_name)
  messages = collect_messages(content)
  messages.each {|lineno, message|
    translated_message = @gettext_catalog[message]
    if not translated_message
      message = 
        sprintf("%s:%d: %s", @source_file_name, lineno, message.inspect)
      @error_messages.push(message)
    elsif message.count("%") != translated_message.count("%")
      message = sprintf("%s:%d: %s => # of %% mismatch.",
                        @source_file_name, 
                        lineno, message.inspect, translated_message)
      @error_messages.push(message)
    end
  }
end