File: RubyXGettext.rb

package info (click to toggle)
liblocale-xgettext-perl 0.7-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 568 kB
  • sloc: perl: 2,143; ansic: 305; java: 129; makefile: 76; ruby: 73; python: 70
file content (143 lines) | stat: -rw-r--r-- 5,011 bytes parent folder | download
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Helper function.  Check whether a Perl scalar evaluates to true.
def ptruthy(str)
    if str == 0 || str == '' || str == '0' || str == nil
        return false
    else
        return true
    end
end

class RubyXGettext
    def initialize(xgettext)
        # The hash xgettext contains 'Proc' objects for every method of
        # the 'Locale::XGettext' API.  The Perl wrapper also injects
        # a Ruby (instance) method into this class for every method
        # available from Perl.  For example, the method 'addEntry()'
        # looks like this:
        #
        # def addEntry()
        #     @xgettext['addEntry'].call(*args)
        # end
        # 
        # As a result, this Ruby class behaves as if it was subclassed
        # directly from the Perl class 'Inline::XGettext'.
        @xgettext = xgettext
    end

    def readFile(filename)
        # You don't have to check that the line is empty.  The
        # PO header gets added after input has been processed.
        lineno = 0
        File.readlines(filename).each do |line|
            lineno = lineno + 1
            reference = "#{filename}:#{lineno}"
            self.addEntry({'msgid': line, 'reference': reference})
        end 
    end

    # Optional methods.
    
    # This method gets called right after all input files have been
    # processed and before the PO entries are sorted.  That means that you
    # can add more entries here.
    #
    # In this example we don't add any strings here but rather abuse the
    # method for showing advanced stuff like getting option values or
    # interpreting keywords.  Invoke the extractor with the option
    # "--test-binding" in order to see this in action.  */
    def extractFromNonFiles()
        if !ptruthy self.option("test_binding")
            return self
        end

        puts "Keywords:"

        keywords = self.keywords
        keywords.each do |keyword, definition|
            puts "function: #{keyword}"
            
            if definition['context'] == nil
                puts "  message context: [none]"
            else
                puts "  message context: argument ##{definition['context']}"
            end

            singular = definition['singular']

            puts "  singular form: #{definition['singular']}"

            if definition['plural'] != 0
                puts "  plural form: #{definition['plural']}"
            else
                puts "  plural form: [none]"
            end
        
            # Try --keyword=hello:1c,2,3,'"Hello, world!"' to see an
            # automatic comment.
            puts "  automatic comment: #{definition['comment']}"
        end
    end

    # Describe the type of input files.
    def fileInformation()
        return "Input files are plain text files and are converted into one PO entry\nfor every non-empty line."
    end

    # Return an array with the default keywords.  This is only used if the
    # method canKeywords() (see below) returns a truth value.  For the lines
    # extractor you would rather return None or an empty hash.
    def defaultKeywords()
        return [
                   'gettext:1', 
                   'ngettext:1,2',
                   'pgettext:1c,2',
                   'npgettext:1c,2,3' 
        ] 
    end

    # You can add more language specific options here.  It is your
    # responsibility that the option names do not conflict with those of the
    # wrapper.
    def languageSpecificOptions()
        return [
                   [
                       # The option specification for Getopt::Long.  If you would
                       # expect a string argument, you would have to specify
                       # "test-binding=s" here, see 
                       # http://search.cpan.org/~jv/Getopt-Long/lib/Getopt/Long.pm 
                       # for details!
                       'test-binding',
                       
                       #  The "name" of the option variable.  This is the argument
                       # to option().
                       'test_binding',
                       
                       # The option as displayed in the usage description.  The
                       # leading four spaces compensate for the missing short
                       # option.
                       '    --test-binding',
                       
                       # The explanation of the option in the usage description.
                       'print additional information for testing the language binding'
            ]
        ]
    end

    # Does the program honor the option -a, --extract-all?  The default
    # implementation returns false.
    def canExtractAll()
        return 0
    end
    
    # Does the program honor the option -k, --keyword?  The default
    # implementation returns true.
    def canKeywords()
        return 1
    end
    
    # Does the program honor the option --flag?  The default
    # implementation returns true.
    def canFlags()
        return 1
    end
end