File: msgcat.rb

package info (click to toggle)
zonecheck 2.0.4-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,416 kB
  • ctags: 1,177
  • sloc: ruby: 9,582; xml: 731; sh: 404; makefile: 71
file content (312 lines) | stat: -rw-r--r-- 7,129 bytes parent folder | download | duplicates (3)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# $Id: msgcat.rb,v 1.44 2004/01/15 17:58:53 sdalu Exp $

# 
# CONTACT     : zonecheck@nic.fr
# AUTHOR      : Stephane D'Alu <sdalu@nic.fr>
#
# CREATED     : 2002/08/02 13:58:17
# REVISION    : $Revision: 1.44 $ 
# DATE        : $Date: 2004/01/15 17:58:53 $
#
# CONTRIBUTORS: (see also CREDITS file)
#
#
# LICENSE     : GPL v2 (or MIT/X11-like after agreement)
# COPYRIGHT   : AFNIC (c) 2003
#
# This file is part of ZoneCheck.
#
# ZoneCheck is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# ZoneCheck is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ZoneCheck; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

require 'ext/myxml'
require 'dbg'


##
## Message catalog for I18N/L10N
##
##
## WARN: this file is not localized
##
## BUGFIX:
##  - method readfile: inode is always 0 on Windows
##      we replace the inode number by the filename if the inode is 0
##

# BUG: @lang / @language / @country?
class MsgCat
    TAG		= 'tag'
    CHECK	= 'check'
    TEST	= 'test'

    NAME	= 'name'
    FAILURE	= 'failure'
    SUCCESS	= 'success'
    EXPLANATION	= 'explanation'
    DETAILS	= 'details'


    ##
    ## Exception: the corresponding message catalog is not installed
    ##
    class NoCatalogFound < StandardError
    end


    ##
    ## Exception: Syntax error, while parsing the file
    ##
    class SyntaxError < StandardError
    end


    ##
    ## Exception: no message for the 'tag'
    ##
    class EntryNotFound < StandardError
    end


    #
    # Initializer
    #
    def initialize(directory, dfltlang)
	$dbg.msg(DBG::LOCALE) { 'creating message catalogue' }
	$dbg.msg(DBG::LOCALE) {"fallback for language is set to '#{dfltlang}'"}
	@dfltlang	= dfltlang
	@directory	= directory
	@loaded		= {}
	@catfiles	= []
	@language	= nil
	@country	= nil
	clear
    end
    
    attr_writer :language, :country


    def clear
	@tag		= {}
	@check		= {}
	@test		= {}
	@shortcut	= { EXPLANATION => {}, DETAILS => {} }
    end

    #
    # Test if a file catalog is available
    #
    def available?(where)
        filepath(where).each { |fp| return true if File::readable?(fp) }
        false
    end



    #
    # Read catalog (from the template filename)
    #  (the occurence of %s is replaced by the language name)
    #
    def read(where)
        filepath(where).each { |fp|
	    if File::readable?(fp)
                res = readfile(fp)
		@catfiles << where unless res.nil?
                return res
            end
	}
        raise NoCatalogFound, "No valid catalog found for #{@lang}"
    end
	

    #
    # Get message associated with the 'tag'
    #
    def get(tag, type=TAG, subtype=nil)
	$dbg.msg(DBG::LOCALE) { 
	    category = type != TAG ? " (#{type}/#{subtype})" : ''
	    "requesting locale for: #{tag}#{category}"
	}
	sameas = nil
	begin
	    case type
	    when TAG
		@tag.fetch(tag)
	    when CHECK
		res = @check.fetch(tag)[subtype]
		if res && (sameas = res['sameas'])
		    res = case sameas
			  when /^shortcut:(.*)$/
			      @shortcut.fetch(subtype).fetch($1)
			  else
			      @check.fetch(sameas).fetch(subtype)
			  end
		end
		res
	    when TEST
		@test.fetch(tag)[subtype]
	    end
	rescue IndexError
	    category = type != TAG ? " (#{type}/#{subtype})" : ''
	    xcp = if sameas.nil?
		"Entity '#{tag}'#{category} has not been defined/localized"
		  else
	        "Entity '#{tag}'#{category} doesn't have a link to '#{sameas}'"
		  end
	    raise EntryNotFound, xcp
	end
    end



    #
    # Reload the message catalogs
    #  (allowing to take into account a new locale)
    #
    def reload
	$dbg.msg(DBG::LOCALE, 'reloading message catalogue')
	clear
	@loaded		= {}
	@catfiles.each { |where| 
 	    catch(:loaded) {
		filepath(where).each { |fp|
		    if File::readable?(fp)
			readfile(fp) ; throw :loaded
		    end
		}
		raise NoCatalogFound, "No valid catalog found for #{@lang}"
	    }
        }
    end

    ## PRIVATE ##
    private

    #
    # Establish the possible filepaths from the template file
    #  - %s is replace by lang
    #  - if not fullpath the default directory is prepend
    # 
    # WARN: An array is returned has for exemple we could need to
    #       test for fr_CA and next fr
    #
    def filepath(where)
	where = "#{@directory}/#{where}"  unless where[0] == ?/

	fp = []
	if @language
	    fp << "#{@language}_#{@country}" if @country
	    fp << @language
	end
	fp << @dfltlang
	fp.collect { |x| where % x }
    end


    #
    # Read catalog file
    #  (return false if the file was already loaded, true otherwise)
    #
    def readfile(msgfile)
	# Check for already loaded catalog
	file_stat = File::stat(msgfile)
	file_id   = [ file_stat.dev, file_stat.ino != 0 ? file_stat.ino \
	                                                : msgfile ]
	if @loaded.has_key?(file_id)
	    $dbg.msg(DBG::LOCALE) { "file already loaded: #{msgfile}" }
	    return false
	end


	# Read message catalogue
	$dbg.msg(DBG::LOCALE, "reading file: #{msgfile}")

	prefix   = nil
	lineno   = 0

	File::open(msgfile) { |io|
	    doc = MyXML::Document::new(io)
	    root = doc.root

	    # Tag
	    root.each('//tag')  { |element| 
		# create prefix from parent sections
		prefix = ''
		xmlsection = element.parent
		while xmlsection.name == 'section'
		    prefix = xmlsection['name'] + ':' + prefix
		    xmlsection = xmlsection.parent
		end

		name = prefix + element['name']
		$dbg.msg(DBG::LOCALE) { "locale tag: #{name}" }
		@tag[name] = element.text
	    }

	    # Shortcut
	    root.each('shortcut')  { |shortcut|
		shortcut.each { |element|
		    name	= element['name']
		    @shortcut[element.name][name] = element
		}
	    }

	    # Check
	    root.each("check")  { |element|
		checkname	= element['name']
		name		= element.child(NAME)
		success		= element.child(SUCCESS)
		failure		= element.child(FAILURE)
		explanation	= element.child(EXPLANATION)
		details		= element.child(DETAILS)

		if explanation['sameas'].nil?
		    explanation = nil unless explanation.empty?
		end
		
		if details['sameas'].nil?
		    details     = nil unless details.empty?
		end

		@check[checkname] = {
		    NAME	=> name,
		    SUCCESS	=> success,	FAILURE	=> failure,
		    EXPLANATION => explanation,	DETAILS	=> details }
	    }

	    # Test
	    root.each('test')  { |element|
		testname	= element['name']
		name		= element.child(NAME)

		@test[testname] = {
		    NAME	=> name }
	    }
	}
	# Consider the file loaded
	@loaded[file_id] = true

	return true
    end
end


#
# Include the 'with_msgcat' facility in every objects
#
def with_msgcat(*msgcat_list)
    return unless $mc && $mc.kind_of?(MsgCat)
    msgcat_list.each { |msgcat| $mc.read(msgcat) }
end