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

Returns a new instance of Messages.



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

def initialize(deplate)
    @deplate = deplate
    encoding = prop('encoding')
    if encoding
        vencoding = @deplate.variables['encoding']
        if vencoding
            encoder = @deplate.formatter || Deplate::Encoding
            if encoder.canonic_enc_name(vencoding) != encoder.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



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

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

.def_prop(key, val) ⇒ Object

Define a “property” like lang, latex_cmd etc.



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

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

.has_property?(key) ⇒ Boolean

was property key defined?

Returns:

  • (Boolean)


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

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 #.



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

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



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

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)



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

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



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

def setup(lang)
    @properties ||= {}
    @catalog    ||= {}
    Deplate::Core.module_eval <<-CODE
        @@messages['#{lang}'] = #{self.name}
        @@messages_last = #{self.name}
    CODE
end

Instance Method Details

#msg(key) ⇒ Object

See Deplate::Messages.msg



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

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

#prop(key, fmt = nil) ⇒ Object

See Deplate::Messages.prop



134
135
136
137
# File 'lib/deplate/messages.rb', line 134

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