Module: Cldr::Export

Defined in:
lib/cldr/export.rb,
lib/cldr/export/code.rb,
lib/cldr/export/data.rb,
lib/cldr/export/ruby.rb,
lib/cldr/export/yaml.rb,
lib/cldr/export/data/base.rb,
lib/cldr/export/data/rbnf.rb,
lib/cldr/export/data/lists.rb,
lib/cldr/export/data/units.rb,
lib/cldr/export/data/layout.rb,
lib/cldr/export/data/numbers.rb,
lib/cldr/export/data/plurals.rb,
lib/cldr/export/data/calendars.rb,
lib/cldr/export/data/languages.rb,
lib/cldr/export/data/metazones.rb,
lib/cldr/export/data/rbnf_root.rb,
lib/cldr/export/data/timezones.rb,
lib/cldr/export/data/currencies.rb,
lib/cldr/export/data/delimiters.rb,
lib/cldr/export/data/territories.rb,
lib/cldr/export/data/plurals/rules.rb,
lib/cldr/export/data/segments_root.rb,
lib/cldr/export/data/windows_zones.rb,
lib/cldr/export/data/plurals/grammar.rb,
lib/cldr/export/data/numbering_systems.rb,
lib/cldr/export/data/calendars/gregorian.rb,
lib/cldr/export/data/currency_digits_and_rounding.rb

Defined Under Namespace

Modules: Code, Data Classes: Ruby, Yaml

Constant Summary collapse

SHARED_COMPONENTS =
[
  'CurrencyDigitsAndRounding',
  'RbnfRoot',
  'Metazones',
  'WindowsZones',
  'NumberingSystems',
  'SegmentsRoot'
]

Class Method Summary collapse

Class Method Details

.base_pathObject



25
26
27
# File 'lib/cldr/export.rb', line 25

def base_path
  @@base_path ||= File.expand_path('./data')
end

.base_path=(base_path) ⇒ Object



29
30
31
# File 'lib/cldr/export.rb', line 29

def base_path=(base_path)
  @@base_path = base_path
end

.component_should_merge_root?(component) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/cldr/export.rb', line 119

def component_should_merge_root?(component)
  component != "Rbnf"
end

.data(component, locale, options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cldr/export.rb', line 59

def data(component, locale, options = {})
  case component.to_s
    when 'Plurals'
      plural_data(component, locale, options)
    else
      if is_shared_component?(component)
        shared_data(component, options)
      else
        locale_based_data(component, locale, options)
      end
  end
end

.export(options = {}, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cldr/export.rb', line 33

def export(options = {}, &block)
  locales        = options[:locales]    || Data.locales
  components     = (options[:components] || Data.components).map { |c| c.to_s.camelize }
  self.base_path = options[:target] if options[:target]

  shared_components, locale_components = components.partition do |component|
    is_shared_component?(component)
  end

  shared_components.each do |component|
    ex = exporter(component, options[:format])
    ex.export('', component, options, &block)
  end

  locales.each do |locale|
    locale_components.each do |component|
      exporter(component, options[:format]).export(locale, component, options, &block)
    end
  end
end

.exporter(component, format) ⇒ Object



54
55
56
57
# File 'lib/cldr/export.rb', line 54

def exporter(component, format)
  name = format ? format : component.to_s == 'Plurals' ? 'ruby' : 'yaml'
  const_get(name.to_s.camelize).new
end

.is_shared_component?(component) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/cldr/export.rb', line 115

def is_shared_component?(component)
  SHARED_COMPONENTS.include?(component)
end

.locale_based_data(component, locale, options = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cldr/export.rb', line 72

def locale_based_data(component, locale, options = {})
  data = locales(locale, component, options).inject({}) do |result, locale|
    data = Data.const_get(component.to_s.camelize).new(locale)
    if data
      data.is_a?(Hash) ? data.deep_merge(result) : data
    else
      result
    end
  end

  # data = resolve_links if options[:merge] TODO!!
  data
end

.locales(locale, component, options) ⇒ Object



95
96
97
98
99
100
# File 'lib/cldr/export.rb', line 95

def locales(locale, component, options)
  locale = locale.to_s.gsub('_', '-')
  locales = options[:merge] ? I18n::Locale::Fallbacks.new[locale.to_sym] : [locale.to_sym]
  locales << :root if component_should_merge_root?(component)
  locales
end

.path(locale, component, extension) ⇒ Object



108
109
110
111
112
113
# File 'lib/cldr/export.rb', line 108

def path(locale, component, extension)
  path = [Export.base_path]
  path << locale.to_s.gsub('_', '-') unless is_shared_component?(component)
  path << "#{component.to_s.underscore}.#{extension}"
  File.join(*path)
end

.plural_data(component, locale, options = {}) ⇒ Object



86
87
88
89
# File 'lib/cldr/export.rb', line 86

def plural_data(component, locale, options = {})
  data = locale_based_data(component, locale, options)
  "{ :'#{locale}' => { :i18n => { :plural => { :keys => #{data[:keys].inspect}, :rule => #{data[:rule]} } } } }"
end

.shared_data(component, options = {}) ⇒ Object



91
92
93
# File 'lib/cldr/export.rb', line 91

def shared_data(component, options = {})
  Data.const_get(component.to_s.camelize).new
end

.write(path, data) ⇒ Object



102
103
104
105
106
# File 'lib/cldr/export.rb', line 102

def write(path, data)
  FileUtils.rm(path) if File.exists?(path)
  FileUtils.mkdir_p(File.dirname(path))
  File.open(path, 'w+') { |f| f.write(data) }
end