Class: Deplate::Messages

Inherits:
Object
  • Object
show all
Defined in:
lib/deplate/messages.rb

Overview

The base class for localizations

Direct Known Subclasses

De, En, RuKoi8r, ZhCnGb2312

Defined Under Namespace

Classes: De, En, RuKoi8r, ZhCnGb2312

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(deplate) ⇒ Messages



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/deplate/messages.rb', line 110

def initialize(deplate)
    @deplate = deplate
    if (encoding = prop('encoding'))
        if (vencoding = @deplate.variables['encoding'])
            if @deplate.canonic_enc_name(vencoding) != @deplate.canonic_enc_name(encoding)
                @deplate.log(['Document encoding does not match message encoding', vencoding, encoding], :error)
            end
        else
            @deplate.variables['encoding'] = encoding
        end
    end
    @deplate.options.lang = prop('lang', false)
end

Class Method Details

.def_msg(id, text) ⇒ Object

Obsolete. Messages should be defined in a file in the locale subdirectory



23
24
25
# File 'lib/deplate/messages.rb', line 23

def def_msg(id, text)
    @catalog[id] = text
end

.def_prop(key, val) ⇒ Object

Define a “property” like lang, latex_cmd etc.



28
29
30
# File 'lib/deplate/messages.rb', line 28

def def_prop(key, val)
    @properties[key] = val
end

.has_property?(key) ⇒ Boolean

was property key defined?



45
46
47
# File 'lib/deplate/messages.rb', line 45

def has_property?(key)
    @properties.keys.include?(key)
end

.load_catalog(lang) ⇒ Object

Load the message catalog for lang from the locale subdirectory

The format is simply (repeating): MESSAGE/ID/HEAD/KEY LOCALIZED TEXT <BLANK LINE>

Filter out comments – lines starting with # in the first position. Comments can only occur in the head/key position as there is no message starting with #.



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
# File 'lib/deplate/messages.rb', line 78

def load_catalog(lang)
    langs, catalogs = Deplate::Core.collect_deplate_options('locale', 'locale', 
                                                            :suffix => '')
    fname = catalogs[lang]
    if fname
        cat = []
        File.open(fname) do |io|
            cat = io.readlines
        end
        key  = nil
        text = []
        for e in cat
            e.chomp!
            if key
                if e =~ /^\s*$/
                    @catalog[key] = text.join("\n")
                    key  = nil
                    text = []
                else
                    text << e
                end
            elsif e !~ /^#/ and e !~ /^\s*$/
                key = e
            end
        end
    else
        raise "Unknown language: #{lang} (#{catalogs.keys.join(', ')})"
    end
end

.msg(id) ⇒ Object

Get the localized message with this id



33
34
35
36
37
38
39
40
41
42
# File 'lib/deplate/messages.rb', line 33

def msg(id)
    case id
    when Symbol
        mid = "@#{id}"
    else
        mid = id
    end
    rv = @catalog[mid] || (id.kind_of?(String) ? id : nil)
    rv
end

.prop(key, fmt = nil) ⇒ Object

get property key; check if there exists a specific property for the formatter fmt (e.g., latex_lang instead of lang)



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/deplate/messages.rb', line 51

def prop(key, fmt=nil)
    if fmt
        if fmt.kind_of?(String)
            fmt_names = [fmt]
        else
            fmt_names = fmt.class.formatter_family_members
        end
        for fmt_name in fmt_names
            af = [fmt_name, key].join('_')
            if has_property?(af)
                return @properties[af]
            end
        end
    end
    return @properties[key]
end

.setup(lang) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/deplate/messages.rb', line 12

def setup(lang)
    @properties ||= {}
    @catalog    ||= {}
    Deplate::Core.module_eval "        @@messages['\#{lang}'] = \#{self.name}\n        @@messages_last = \#{self.name}\n    CODE\nend\n"

Instance Method Details

#msg(key) ⇒ Object

See Deplate::Messages.msg



125
126
127
# File 'lib/deplate/messages.rb', line 125

def msg(key)
    self.class.msg(key)
end

#prop(key, fmt = nil) ⇒ Object

See Deplate::Messages.prop



130
131
132
133
# File 'lib/deplate/messages.rb', line 130

def prop(key, fmt=nil)
    fmt = @deplate.formatter if fmt.nil?
    self.class.prop(key, fmt)
end