Class: CLDR::Generator::LocaleData

Inherits:
Object
  • Object
show all
Includes:
Parser
Defined in:
lib/cldr/generator.rb

Constant Summary collapse

TARGET_INFO =
[
    # [textdomain, [var, meth, xml path, opts = nil], [variable2, meth, ...], ...]
    [:core, 
[:h_languages, :parse, "localeDisplayNames/languages/language"],
[:h_territories, :parse, "localeDisplayNames/territories/territory"],
[:h_scripts, :parse, "localeDisplayNames/scripts/script"],
[:layout_orientation, :attribute_only, "layout/orientation", "characters"],
[:layout_inlist_casing, :attribute_only, "layout/inList", "casing"],
[:h_character_exemplar_characters, :parse, "characters/exemplarCharacters"],
[:character_mapping, :attribute_plural, "characters/mapping", ["registry"]],
[:delimiter_quotation_start, :parse, "delimiters/quotationStart"],
[:delimiter_quotation_end, :parse, "delimiters/quotationEnd"],
[:delimiter_alternate_quotation_start, :parse, "delimiters/alternateQuotationStart"],
[:delimiter_alternate_quotation_end, :parse, "delimiters/alternateQuotationEnd"],
[:measurement_system, :attribute_only, "localeDisplayNames/measurementSystemNames/measurementSystemName", "type"],
[:measurement_papersize_height, :parse, "measurement/paperSize/height"],
[:measurement_papersize_width, :parse, "measurement/paperSize/width"],
[:h_variants, :parse, "localeDisplayNames/variants/variant"],
[:h_keys, :parse, "localeDisplayNames/keys/key"],
[:h_types, :parse, "localeDisplayNames/types/type"],
[:yesstr, :parse, "posix/messages/yesstr"],
[:nostr, :parse, "posix/messages/nostr"],
[:h_references, :attribute_plural, "references/reference", ["uri"]],
[:h_list_patterns, :parse, "listPatterns/listPattern/listPatternPart"],
    ],
    [:calendar, 
[:h_months, :parse,
  "dates/calendars/calendar", "months/monthContext/monthWidth", "month"],
[:h_days, :parse,
  "dates/calendars/calendar", "days/dayContext/dayWidth", "day"],
[:h_am, :parse, "dates/calendars/calendar", "am"],
[:h_pm, :parse, "dates/calendars/calendar", "pm"],
[:h_era_names, :parse, "dates/calendars/calendar", "eras/eraNames/era"],
[:h_era_abbrs, :parse, "dates/calendars/calendar", "eras/eraAbbr/era"],
[:h_dateformats, :parse, 
  "dates/calendars/calendar", "dateFormats/dateFormatLength", "dateFormat/pattern"],
[:h_dateformat_defaults, :attribute_only, 
  "dates/calendars/calendar", "dateFormats/default", "type"],
[:h_timeformats, :parse, 
  "dates/calendars/calendar", "timeFormats/timeFormatLength", "timeFormat/pattern"],
[:h_timeformat_defaults, :attribute_only, 
  "dates/calendars/calendar", "timeFormats/default", "type"],
[:h_datetimeformats, :parse, 
  "dates/calendars/calendar", "dateTimeFormats/dateTimeFormatLength", "dateTimeFormat/pattern"],
[:h_fields, :parse, 
  "dates/calendars/calendar", "fields/field", "displayName"],
[:h_field_relatives, :parse, 
  "dates/calendars/calendar", "fields/field", "relative"],

    ],
    [:timezone,
[:hourformat, :parse, "dates/timeZoneNames/hourFormat"],
[:regionformat, :parse, "dates/timeZoneNames/regionFormat"],
[:fallbackformat, :parse, "dates/timeZoneNames/fallbackFormat"],
[:singlecountries, :attribute_only, "dates/timeZoneNames/singleCountries", "list"],
[:h_exemplarcities, :parse, "dates/timeZoneNames/zone", "exemplarCity"],
[:h_long_generics, :parse, "dates/timeZoneNames/metazone", "long/generic"],
[:h_long_standards, :parse, "dates/timeZoneNames/metazone", "long/standard"],
[:h_long_daylights, :parse, "dates/timeZoneNames/metazone", "long/daylight"],
[:h_short_generics, :parse, "dates/timeZoneNames/metazone", "short/generic"],
[:h_short_standards, :parse, "dates/timeZoneNames/metazone", "short/standard"],
[:h_short_daylights, :parse, "dates/timeZoneNames/metazone", "short/daylight"],
    ],
    [:number,
[:symbol_decimal, :parse, "numbers/symbols/decimal"],
[:symbol_group, :parse, "numbers/symbols/group"],
[:symbol_list, :parse, "numbers/symbols/list"],
[:symbol_percentsign, :parse, "numbers/symbols/percentSign"],
[:symbol_nativezerodigit, :parse, "numbers/symbols/nativeZeroDigit"],
[:symbol_patterndigit, :parse, "numbers/symbols/patternDigit"],
[:symbol_plussign, :parse, "numbers/symbols/plusSign"],
[:symbol_minussign, :parse, "numbers/symbols/minusSign"],
[:symbol_exponential, :parse, "numbers/symbols/exponential"],
[:symbol_permille, :parse, "numbers/symbols/perMille"],
[:symbol_infinity, :parse, "numbers/symbols/infinity"],
[:symbol_nan, :parse, "numbers/symbols/nan"],
[:h_decimalformats, :parse, "numbers/decimalFormats/decimalFormatLength", "decimalFormat/pattern"],
[:h_scientificformat, :parse, "numbers/scientificFormats/scientificFormatLength", "scientificFormat/pattern"],
[:h_percentformats, :parse, "numbers/percentFormats/percentFormatLength", "percentFormat/pattern"],
    ],
    [:currency,
[:format, :parse, "numbers/currencyFormats/currencyFormatLength/currencyFormat/pattern"],
[:before_match, :parse, 
  "numbers/currencyFormats/currencySpacing/beforeCurrency/currencyMatch"],
[:before_match_surrounding, :parse, 
  "numbers/currencyFormats/currencySpacing/beforeCurrency/surroundingMatch"],
[:before_insertbetween, :parse,
  "numbers/currencyFormats/currencySpacing/beforeCurrency/insertBetween"],
[:after_match, :parse,
  "numbers/currencyFormats/currencySpacing/afterCurrency/currencyMatch"],
[:after_match_surrounding, :parse, 
  "numbers/currencyFormats/currencySpacing/afterCurrency/surroundingMatch"],
[:after_insertbetween, :parse, 
  "numbers/currencyFormats/currencySpacing/afterCurrency/insertBetween"],
[:h_currencies, :parse, "numbers/currencies/currency", "displayName"],
[:h_symbols, :parse, "numbers/currencies/currency", "symbol"],
    ]
]
SUPPLEMENTAL =
[:calendar_preference, :h_week_data, :h_measurement_data]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parser

#attribute, #attribute_only, #attribute_plural, #parse

Constructor Details

#initialize(xml_file = nil, out_dir = nil) ⇒ LocaleData

Returns a new instance of LocaleData.



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
# File 'lib/cldr/generator.rb', line 166

def initialize(xml_file = nil, out_dir = nil)
  TARGET_INFO.each do |textdomain, *infos|
    infos.each do |var, meth, *path|
      instance_eval("@#{var} = {}")
    end
  end

  if xml_file
    @out_dir = out_dir
    File.open(xml_file) {|f|
      @doc = REXML::Document.new(f)
    }
    file_ary = xml_file.split(/\\|\//)
    if file_ary.size > 2
      @xml_file_name = file_ary[(file_ary.size - 3)..(file_ary.size)].join("/")
    else
      @xml_file_name = file_ary[0]
    end
    @version =  /dtd\/(.*)\/ldml.dtd/.match(@doc.doctype.to_s).to_a[1]
    @revision = attribute("/ldml/identity/version", "number").sub(/.Revision: /, "")
    @generation_date = attribute("/ldml/identity/generation", "date").sub(/.Date: /, "")
    language_type = attribute("/ldml/identity/language", "type")
    script_type = attribute("/ldml/identity/script", "type")
    territory_type = attribute("/ldml/identity/territory", "type")

    # locale name uses "language"_"TERITORY"_"Script" style
    @locale_name = language_type
    @locale_name += "_#{territory_type}" if territory_type
    @locale_name += "_#{script_type}" if script_type
    @alias_source = attribute("/ldml/alias", "source")
    @generator = CLDR::Generator

    root = @doc.elements['ldml']
    supplement_alias(root, File.dirname(xml_file))

    TARGET_INFO.each do |textdomain, *infos|
      infos.each do |var, meth, *opts|
        instance_eval("@#{var} = parse(var, meth, root, *opts)")
      end
    end
  else
    @alias_source = nil
    @locale = nil
    @out_dir = nil
  end
end

Instance Attribute Details

#alias_sourceObject (readonly)

Returns the value of attribute alias_source.



24
25
26
# File 'lib/cldr/generator.rb', line 24

def alias_source
  @alias_source
end

#locale_nameObject (readonly)

Returns the value of attribute locale_name.



24
25
26
# File 'lib/cldr/generator.rb', line 24

def locale_name
  @locale_name
end

Instance Method Details

#all_infoObject



162
163
164
# File 'lib/cldr/generator.rb', line 162

def all_info
  inf = TARGET_INFO + ([[:supplemental] + SUPPLEMENTAL.map{ |s| [s, nil, nil]}])
end


299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/cldr/generator.rb', line 299

def print
  FileUtils.mkdir_p(File.join(@out_dir, @locale_name))
  all_info.each do |textdomain, *infos|
    next if textdomain == :supplemental && @locale_name == 'root'
    out = File.open(File.join(@out_dir, @locale_name, "#{textdomain}.rb"), "w")
    attrs = []
    print_header(out)
    out.puts "  private"
    out.puts "  def init_data"
    infos.each do |var, meth, path|
      attrs << print_messages(var, out)
    end
    out.puts "  end"
    out.puts
    out.puts "  public"
    attrs.each do |v|
      out.puts "  attr_reader :#{v}"
    end
    out.close
  end
end


255
256
257
258
# File 'lib/cldr/generator.rb', line 255

def print_aline(out, k, v)
  qv = v.to_s.gsub('"', '\"')
  out.puts %Q!    @#{k} = "#{qv}"!
end


234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/cldr/generator.rb', line 234

def print_header(out)
  out.puts %Q[#
# encoding: UTF-8
#
# a language data file for Ruby/CLDR
#
# Generated by: #{@generator}
#
# CLDR version: #{@version}
#
# Original file name: #{@xml_file_name}
# Original file revision: #{@revision}
#
# Copyright (C) 2006 Masao Mutoh
#
# This file is distributed under the same license as the Ruby/CLDR.
#

  ]
end


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
# File 'lib/cldr/generator.rb', line 260

def print_messages(data, out)
  varname = data.to_s
  target = instance_eval("@#{data}")
  if /^h_(.*)/ =~ data.to_s
    varname = $1
    out.puts "    @#{varname} = {}"
  end
  if target.size == 0
    $stderr.puts "check: #{varname}" if $DEBUG
  else
    entry = {}
    target.to_a.sort{|a, b| a[0] <=> b[0]}.each do |k, v|
      if /^h_(.*)\|(.*)/ =~ k
        key = $2
        var = $1.split(/\|/)
        varname = var.shift
        ivar = varname
        var.each do |i|
          ivar += "[:#{i.gsub(/-/, '_')}]"
          unless entry[ivar]
            entry[ivar] = true
            out.puts "    @#{ivar} = {}"
          end
        end
        print_aline(out, "#{ivar}[\"#{key}\"]", v)
      elsif /^h_(.*)/ =~ k 
        varname = $1
        print_aline(out, "#{$1}[\"default\"]", v)
      else
        raise "error? #{k}" if v == Array
        v = v == ":empty:" ? "" : v
        varname = k
        print_aline(out, k, v)
      end
    end
  end
  varname
end

#supplement_alias(root, xml_dir) ⇒ Object



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
# File 'lib/cldr/generator.rb', line 132

def supplement_alias(root, xml_dir)
  root.get_elements("//alias").each do |v|
    parent = v.parent
    sub_ele = nil
    if v.attributes["source"] == "locale"
      sub_ele = parent.get_elements(v.attributes["path"])[0]
    else
      if v.attributes["path"] == "//ldml"
        target_path = v.attributes["path"]
        parent.elements.each{|old| old.remove}
      elsif v.attributes["path"].nil?
        #ti.xml only
        target_path = parent.dup.xpath
      else
        raise "Not supported."
      end

      filename = File.join(xml_dir, v.attributes["source"] + ".xml")
      File.open(filename){|sub|
        doc = REXML::Document.new(sub)
        sub_ele = doc.elements[target_path]
      }
    end
    sub_ele.elements.each do |child|
      parent.add_element(child.dup)
    end
    v.remove
  end
end

#update(other) ⇒ Object



225
226
227
228
229
230
231
232
# File 'lib/cldr/generator.rb', line 225

def update(other)
  all_info.each do |textdomain, *infos|
    infos.each do |var, meth, path|
      update_real(var, other)
    end
  end
  self
end

#update_real(data_name, other) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
# File 'lib/cldr/generator.rb', line 213

def update_real(data_name, other)
  instance_eval %Q[
    if other.#{data_name}
      if @#{data_name}
        @#{data_name}.update(other.#{data_name})
      else
        @#{data_name} = other.#{data_name}.dup
      end
    end
  ]
end