Module: Spider::I18n::JavascriptParser

Defined in:
lib/spiderfw/i18n/javascript_parser.rb

Constant Summary collapse

GettextRegexp =
/\W_\(['"]([^\)'"]+)['"](,\s*[^\)]+\s*)*\)/

Class Method Summary collapse

Class Method Details

.parse(file, ary) ⇒ Object



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
# File 'lib/spiderfw/i18n/javascript_parser.rb', line 18

def parse(file, ary)
    f = File.new(file)
    cnt = 0
    my_ary = []
    f.each_line do |line|
        cnt += 1
        scanner = ::StringScanner.new(line)
        while scanner.scan_until(GettextRegexp)
            str = scanner.matched
            str =~ GettextRegexp
            found = false
            (ary+my_ary).each do |msg|
                if (msg[0] == $1)
                    msg << "#{file}:#{cnt}"
                    found = true
                    break
                end
            end
            my_ary << [$1, "#{file}:#{cnt}"] unless found
        end
    end
    f.close
    unless my_ary.empty?
        dir = File.dirname(file)
        name = File.basename(file, '.js')
        i18n_file = File.join(dir, "#{name}.i18n.json")
        File.open(i18n_file, 'w') do |f|
            f << my_ary.collect{ |a| a[0] }.to_json
        end
    end
    return ary + my_ary 
end

.target?(file) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/spiderfw/i18n/javascript_parser.rb', line 14

def target?(file)
    File.extname(file) == '.js'
end