Module: CLDR::Generator

Defined in:
lib/cldr/generator.rb

Defined Under Namespace

Classes: LocaleData

Class Method Summary collapse

Class Method Details

.analyze(localedata_table) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/cldr/generator.rb', line 410

def analyze(localedata_table)
  # Copy root/parent data to child
  #debugger
  localedata_table.to_a.sort{|a, b| a[0] <=> b[0]}.each do |k, v|
    puts k
    unless k == "root"
      langary = k.split(/_/)
      lang = nil
      new_data = LocaleData.new.update(localedata_table["root"])
      while (langary.size > 0)
        if lang
          lang << "_" + langary.shift
        else
          lang = langary.shift
        end
        new_data.update(localedata_table[lang]) if localedata_table[lang]
      end
      v.update(new_data)
    end
  end

  # Copy alias_source to data
  localedata_table.each{|key, data|
    if data.alias_source
      data.update(localedata_table[data.alias_source])
    end
  }
end

.parse(localedata_table, root_path, out_path = "lib/cldr/resource/") ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/cldr/generator.rb', line 323

def parse(localedata_table, root_path, out_path = "lib/cldr/resource/")
  require 'ruby-debug'
  #debugger
  supplemental = parse_supplemental(root_path)
  Dir.glob(File.join(root_path, "common/main/*.xml")).sort.each do |v|
    puts v
    locale = File.basename(v, ".xml")
    localedata_table[locale] = LocaleData.new(v, out_path)
    langary = locale.split(/_/)
    if supplemental[langary.first.upcase]
      supplemental[langary.first.upcase].each do |k, v|
        localedata_table[locale].send("#{k}=", v)
      end
    end
  end
end

.parse_supplemental(root_path) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/cldr/generator.rb', line 359

def parse_supplemental(root_path)
  supplemental = {}
  xml_file = File.join(root_path, "common/supplemental/supplementalData.xml")
  f = File.open(xml_file) do |f|
  
    doc = REXML::Document.new(f)


    doc.elements.each("supplementalData/calendarPreferenceData/calendarPreference") do |calendar_preference|
      set_territory_values(supplemental, calendar_preference, 'ordering', :calendar_preference){ |val| val.split(' ').first }
    end
    doc.elements.each("supplementalData/weekData/*") do |el|
      if el.name == 'minDays'
        set_territory_values(supplemental, el, 'count', :h_week_data)
      else
        set_territory_values(supplemental, el, 'day', :h_week_data)
      end
    end
    doc.elements.each("supplementalData/measurementData/*") do |measurement_data|
      set_territory_values(supplemental, measurement_data, 'type', :h_measurement_data)
    end
    groups = {}
    doc.elements.each("supplementalData/territoryContainment/group") do |group|
      groups[group.attributes['type']] = group.attributes['contains'].split(' ')
    end
    expand_data = lambda do |group|
      if group =~ /\d\d\d/
        groups[group].map{ |g| expand_data.call(g) }
      else
        [group]
      end
    end
    groups.keys.each do |k|
      groups[k] = groups[k].map(&expand_data).flatten
    end
    supplemental.keys.each do |terr|
      if terr =~ /\d\d\d/
        val = supplemental.delete(terr)
        groups[terr].each do |g|
          supplemental[g] ||= {}
          val.each do |k, v|
            supplemental[g][k] ||= {}
            supplemental[g][k].merge!(v)
          end
        end
      end
    end
    supplemental
  end
end


439
440
441
442
443
444
# File 'lib/cldr/generator.rb', line 439

def print(localedata_table)
  localedata_table.to_a.sort{|a, b| a[0] <=> b[0]}.each do |k, v|
    puts k
    v.print
  end
end

.run(root_path) ⇒ Object



446
447
448
449
450
451
452
453
454
# File 'lib/cldr/generator.rb', line 446

def run(root_path)
  puts "Parsing ..."
  localedata_table = {}
  parse(localedata_table, root_path)
  puts "Analyzing ..."
  analyze(localedata_table)
  puts "Outputting ..."
  print(localedata_table)
end

.set_territory_values(supplemental, el, attribute, key, &proc) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/cldr/generator.rb', line 340

def set_territory_values(supplemental, el, attribute, key, &proc)
  hash_key = nil
  if key.to_s =~ /h_(.+)/
    hash_key = $1
  end
  territories = el.attributes["territories"].split(' ')
  territories.each do |t|
    supplemental[t] ||= {}
    val = el.attributes[attribute]
    val = proc.call(val) if proc
    if hash_key
      supplemental[t][key] ||= {}
      supplemental[t][key]["#{key}|#{el.name}"] = val
    else
      supplemental[t][key] = {key.to_s => val}
    end
  end
end