~ubuntu-branches/ubuntu/trusty/zonecheck/trusty-proposed

« back to all changes in this revision

Viewing changes to zc/msgcat.rb

  • Committer: Bazaar Package Importer
  • Author(s): Stephane Bortzmeyer
  • Date: 2004-03-10 14:08:05 UTC
  • Revision ID: james.westby@ubuntu.com-20040310140805-ij55fso1e23bk8ye
Tags: upstream-2.0.3
ImportĀ upstreamĀ versionĀ 2.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# $Id: msgcat.rb,v 1.44 2004/01/15 17:58:53 sdalu Exp $
 
2
 
 
3
 
4
# CONTACT     : zonecheck@nic.fr
 
5
# AUTHOR      : Stephane D'Alu <sdalu@nic.fr>
 
6
#
 
7
# CREATED     : 2002/08/02 13:58:17
 
8
# REVISION    : $Revision: 1.44 $ 
 
9
# DATE        : $Date: 2004/01/15 17:58:53 $
 
10
#
 
11
# CONTRIBUTORS: (see also CREDITS file)
 
12
#
 
13
#
 
14
# LICENSE     : GPL v2 (or MIT/X11-like after agreement)
 
15
# COPYRIGHT   : AFNIC (c) 2003
 
16
#
 
17
# This file is part of ZoneCheck.
 
18
#
 
19
# ZoneCheck is free software; you can redistribute it and/or modify it
 
20
# under the terms of the GNU General Public License as published by
 
21
# the Free Software Foundation; either version 2 of the License, or
 
22
# (at your option) any later version.
 
23
 
24
# ZoneCheck is distributed in the hope that it will be useful, but
 
25
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
26
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
27
# General Public License for more details.
 
28
#
 
29
# You should have received a copy of the GNU General Public License
 
30
# along with ZoneCheck; if not, write to the Free Software Foundation,
 
31
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
32
#
 
33
 
 
34
require 'ext/myxml'
 
35
require 'dbg'
 
36
 
 
37
 
 
38
##
 
39
## Message catalog for I18N/L10N
 
40
##
 
41
##
 
42
## WARN: this file is not localized
 
43
##
 
44
## BUGFIX:
 
45
##  - method readfile: inode is always 0 on Windows
 
46
##      we replace the inode number by the filename if the inode is 0
 
47
##
 
48
 
 
49
# BUG: @lang / @language / @country?
 
50
class MsgCat
 
51
    TAG         = 'tag'
 
52
    CHECK       = 'check'
 
53
    TEST        = 'test'
 
54
 
 
55
    NAME        = 'name'
 
56
    FAILURE     = 'failure'
 
57
    SUCCESS     = 'success'
 
58
    EXPLANATION = 'explanation'
 
59
    DETAILS     = 'details'
 
60
 
 
61
 
 
62
    ##
 
63
    ## Exception: the corresponding message catalog is not installed
 
64
    ##
 
65
    class NoCatalogFound < StandardError
 
66
    end
 
67
 
 
68
 
 
69
    ##
 
70
    ## Exception: Syntax error, while parsing the file
 
71
    ##
 
72
    class SyntaxError < StandardError
 
73
    end
 
74
 
 
75
 
 
76
    ##
 
77
    ## Exception: no message for the 'tag'
 
78
    ##
 
79
    class EntryNotFound < StandardError
 
80
    end
 
81
 
 
82
 
 
83
    #
 
84
    # Initializer
 
85
    #
 
86
    def initialize(directory, dfltlang)
 
87
        $dbg.msg(DBG::LOCALE) { 'creating message catalogue' }
 
88
        $dbg.msg(DBG::LOCALE) {"fallback for language is set to '#{dfltlang}'"}
 
89
        @dfltlang       = dfltlang
 
90
        @directory      = directory
 
91
        @loaded         = {}
 
92
        @catfiles       = []
 
93
        @language       = nil
 
94
        @country        = nil
 
95
        clear
 
96
    end
 
97
    
 
98
    attr_writer :language, :country
 
99
 
 
100
 
 
101
    def clear
 
102
        @tag            = {}
 
103
        @check          = {}
 
104
        @test           = {}
 
105
        @shortcut       = { EXPLANATION => {}, DETAILS => {} }
 
106
    end
 
107
 
 
108
    #
 
109
    # Test if a file catalog is available
 
110
    #
 
111
    def available?(where)
 
112
        filepath(where).each { |fp| return true if File::readable?(fp) }
 
113
        false
 
114
    end
 
115
 
 
116
 
 
117
 
 
118
    #
 
119
    # Read catalog (from the template filename)
 
120
    #  (the occurence of %s is replaced by the language name)
 
121
    #
 
122
    def read(where)
 
123
        filepath(where).each { |fp|
 
124
            if File::readable?(fp)
 
125
                res = readfile(fp)
 
126
                @catfiles << where unless res.nil?
 
127
                return res
 
128
            end
 
129
        }
 
130
        raise NoCatalogFound, "No valid catalog found for #{@lang}"
 
131
    end
 
132
        
 
133
 
 
134
    #
 
135
    # Get message associated with the 'tag'
 
136
    #
 
137
    def get(tag, type=TAG, subtype=nil)
 
138
        $dbg.msg(DBG::LOCALE) { 
 
139
            category = type != TAG ? " (#{type}/#{subtype})" : ''
 
140
            "requesting locale for: #{tag}#{category}"
 
141
        }
 
142
        sameas = nil
 
143
        begin
 
144
            case type
 
145
            when TAG
 
146
                @tag.fetch(tag)
 
147
            when CHECK
 
148
                res = @check.fetch(tag)[subtype]
 
149
                if res && (sameas = res['sameas'])
 
150
                    res = case sameas
 
151
                          when /^shortcut:(.*)$/
 
152
                              @shortcut.fetch(subtype).fetch($1)
 
153
                          else
 
154
                              @check.fetch(sameas).fetch(subtype)
 
155
                          end
 
156
                end
 
157
                res
 
158
            when TEST
 
159
                @test.fetch(tag)[subtype]
 
160
            end
 
161
        rescue IndexError
 
162
            category = type != TAG ? " (#{type}/#{subtype})" : ''
 
163
            xcp = if sameas.nil?
 
164
                "Entity '#{tag}'#{category} has not been defined/localized"
 
165
                  else
 
166
                "Entity '#{tag}'#{category} doesn't have a link to '#{sameas}'"
 
167
                  end
 
168
            raise EntryNotFound, xcp
 
169
        end
 
170
    end
 
171
 
 
172
 
 
173
 
 
174
    #
 
175
    # Reload the message catalogs
 
176
    #  (allowing to take into account a new locale)
 
177
    #
 
178
    def reload
 
179
        $dbg.msg(DBG::LOCALE, 'reloading message catalogue')
 
180
        clear
 
181
        @loaded         = {}
 
182
        @catfiles.each { |where| 
 
183
            catch(:loaded) {
 
184
                filepath(where).each { |fp|
 
185
                    if File::readable?(fp)
 
186
                        readfile(fp) ; throw :loaded
 
187
                    end
 
188
                }
 
189
                raise NoCatalogFound, "No valid catalog found for #{@lang}"
 
190
            }
 
191
        }
 
192
    end
 
193
 
 
194
    ## PRIVATE ##
 
195
    private
 
196
 
 
197
    #
 
198
    # Establish the possible filepaths from the template file
 
199
    #  - %s is replace by lang
 
200
    #  - if not fullpath the default directory is prepend
 
201
    # 
 
202
    # WARN: An array is returned has for exemple we could need to
 
203
    #       test for fr_CA and next fr
 
204
    #
 
205
    def filepath(where)
 
206
        where = "#{@directory}/#{where}"  unless where[0] == ?/
 
207
 
 
208
        fp = []
 
209
        if @language
 
210
            fp << "#{@language}_#{@country}" if @country
 
211
            fp << @language
 
212
        end
 
213
        fp << @dfltlang
 
214
        fp.collect { |x| where % x }
 
215
    end
 
216
 
 
217
 
 
218
    #
 
219
    # Read catalog file
 
220
    #  (return false if the file was already loaded, true otherwise)
 
221
    #
 
222
    def readfile(msgfile)
 
223
        # Check for already loaded catalog
 
224
        file_stat = File::stat(msgfile)
 
225
        file_id   = [ file_stat.dev, file_stat.ino != 0 ? file_stat.ino \
 
226
                                                        : msgfile ]
 
227
        if @loaded.has_key?(file_id)
 
228
            $dbg.msg(DBG::LOCALE) { "file already loaded: #{msgfile}" }
 
229
            return false
 
230
        end
 
231
 
 
232
 
 
233
        # Read message catalogue
 
234
        $dbg.msg(DBG::LOCALE, "reading file: #{msgfile}")
 
235
 
 
236
        prefix   = nil
 
237
        lineno   = 0
 
238
 
 
239
        File::open(msgfile) { |io|
 
240
            doc = MyXML::Document::new(io)
 
241
            root = doc.root
 
242
 
 
243
            # Tag
 
244
            root.each('//tag')  { |element| 
 
245
                # create prefix from parent sections
 
246
                prefix = ''
 
247
                xmlsection = element.parent
 
248
                while xmlsection.name == 'section'
 
249
                    prefix = xmlsection['name'] + ':' + prefix
 
250
                    xmlsection = xmlsection.parent
 
251
                end
 
252
 
 
253
                name = prefix + element['name']
 
254
                $dbg.msg(DBG::LOCALE) { "locale tag: #{name}" }
 
255
                @tag[name] = element.text
 
256
            }
 
257
 
 
258
            # Shortcut
 
259
            root.each('shortcut')  { |shortcut|
 
260
                shortcut.each { |element|
 
261
                    name        = element['name']
 
262
                    @shortcut[element.name][name] = element
 
263
                }
 
264
            }
 
265
 
 
266
            # Check
 
267
            root.each("check")  { |element|
 
268
                checkname       = element['name']
 
269
                name            = element.child(NAME)
 
270
                success         = element.child(SUCCESS)
 
271
                failure         = element.child(FAILURE)
 
272
                explanation     = element.child(EXPLANATION)
 
273
                details         = element.child(DETAILS)
 
274
 
 
275
                if explanation['sameas'].nil?
 
276
                    explanation = nil unless explanation.empty?
 
277
                end
 
278
                
 
279
                if details['sameas'].nil?
 
280
                    details     = nil unless details.empty?
 
281
                end
 
282
 
 
283
                @check[checkname] = {
 
284
                    NAME        => name,
 
285
                    SUCCESS     => success,     FAILURE => failure,
 
286
                    EXPLANATION => explanation, DETAILS => details }
 
287
            }
 
288
 
 
289
            # Test
 
290
            root.each('test')  { |element|
 
291
                testname        = element['name']
 
292
                name            = element.child(NAME)
 
293
 
 
294
                @test[testname] = {
 
295
                    NAME        => name }
 
296
            }
 
297
        }
 
298
        # Consider the file loaded
 
299
        @loaded[file_id] = true
 
300
 
 
301
        return true
 
302
    end
 
303
end
 
304
 
 
305
 
 
306
#
 
307
# Include the 'with_msgcat' facility in every objects
 
308
#
 
309
def with_msgcat(*msgcat_list)
 
310
    return unless $mc && $mc.kind_of?(MsgCat)
 
311
    msgcat_list.each { |msgcat| $mc.read(msgcat) }
 
312
end