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/fields.rb,
lib/cldr/export/data/layout.rb,
lib/cldr/export/data/aliases.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/variables.rb,
lib/cldr/export/data/characters.rb,
lib/cldr/export/data/currencies.rb,
lib/cldr/export/data/delimiters.rb,
lib/cldr/export/data/transforms.rb,
lib/cldr/export/data/territories.rb,
lib/cldr/export/data/plural_rules.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/likely_subtags.rb,
lib/cldr/export/data/parent_locales.rb,
lib/cldr/export/data/plurals/grammar.rb,
lib/cldr/export/data/numbering_systems.rb,
lib/cldr/export/data/region_currencies.rb,
lib/cldr/export/data/calendars/gregorian.rb,
lib/cldr/export/data/territories_containment.rb,
lib/cldr/export/data/currency_digits_and_rounding.rb
Defined Under Namespace
Modules: Code, Data Classes: Ruby, Yaml
Constant Summary collapse
- SHARED_COMPONENTS =
%w[ CurrencyDigitsAndRounding Metazones NumberingSystems RbnfRoot SegmentsRoot TerritoriesContainment WindowsZones Transforms LikelySubtags Variables Aliases RegionCurrencies ]
Class Method Summary collapse
- .base_path ⇒ Object
- .base_path=(base_path) ⇒ Object
- .component_should_merge_root?(component) ⇒ Boolean
- .data(component, locale, options = {}) ⇒ Object
- .export(options = {}, &block) ⇒ Object
- .exporter(component, format) ⇒ Object
- .from_i18n(locale) ⇒ Object
- .is_shared_component?(component) ⇒ Boolean
- .locale_based_data(component, locale, options = {}) ⇒ Object
- .locales(locale, component, options) ⇒ Object
- .path(locale, component, extension) ⇒ Object
- .plural_data(component, locale, options = {}) ⇒ Object
- .shared_data(component, options = {}) ⇒ Object
- .to_i18n(locale) ⇒ Object
- .write(path, data) ⇒ Object
Class Method Details
.base_path ⇒ Object
32 33 34 |
# File 'lib/cldr/export.rb', line 32 def base_path @@base_path ||= File.('./data') end |
.base_path=(base_path) ⇒ Object
36 37 38 |
# File 'lib/cldr/export.rb', line 36 def base_path=(base_path) @@base_path = base_path end |
.component_should_merge_root?(component) ⇒ Boolean
173 174 175 |
# File 'lib/cldr/export.rb', line 173 def component_should_merge_root?(component) !%w(Rbnf Fields).include?(component) end |
.data(component, locale, options = {}) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/cldr/export.rb', line 81 def data(component, locale, = {}) case component.to_s when 'Plurals' plural_data(component, locale, ) else if is_shared_component?(component) shared_data(component, ) else locale_based_data(component, locale, ) end end end |
.export(options = {}, &block) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/cldr/export.rb', line 40 def export( = {}, &block) locales = [:locales] || Data.locales components = ([:components] || Data.components).map { |c| c.to_s.camelize } self.base_path = [:target] if [:target] shared_components, locale_components = components.partition do |component| is_shared_component?(component) end shared_components.each do |component| case component when "Transforms" yaml_exporter = Cldr::Export::Yaml.new Dir.glob("#{Cldr::Export::Data.dir}/transforms/**.xml").each do |transform_file| data = Data::Transforms.new(transform_file) source = data[:transforms].first[:source] target = data[:transforms].first[:target] variant = data[:transforms].first[:variant] file_name = [source, target, variant].compact.join('-') output_path = File.join(base_path, "transforms", "#{file_name}.yml") write(output_path, yaml_exporter.yaml(data)) yield component, nil, output_path if block_given? end else ex = exporter(component, [:format]) ex.export('', component, , &block) end end locales.each do |locale| locale_components.each do |component| exporter(component, [:format]).export(locale, component, , &block) end end end |
.exporter(component, format) ⇒ Object
76 77 78 79 |
# File 'lib/cldr/export.rb', line 76 def exporter(component, format) name = format ? format : component.to_s == 'Plurals' ? 'ruby' : 'yaml' const_get(name.to_s.camelize).new end |
.from_i18n(locale) ⇒ Object
126 127 128 |
# File 'lib/cldr/export.rb', line 126 def from_i18n(locale) return locale.to_s.gsub('-', '_') end |
.is_shared_component?(component) ⇒ Boolean
169 170 171 |
# File 'lib/cldr/export.rb', line 169 def is_shared_component?(component) SHARED_COMPONENTS.include?(component) end |
.locale_based_data(component, locale, options = {}) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/cldr/export.rb', line 94 def locale_based_data(component, locale, = {}) data = locales(locale, component, ).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
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/cldr/export.rb', line 130 def locales(locale, component, ) locale = to_i18n(locale) locales = if [:merge] defined_parents = Cldr::Export::Data::ParentLocales.new ancestry = [locale] loop do if defined_parents[from_i18n(ancestry.last)] ancestry << to_i18n(defined_parents[from_i18n(ancestry.last)]) elsif I18n::Locale::Tag.tag(ancestry.last).self_and_parents.count > 1 ancestry << I18n::Locale::Tag.tag(ancestry.last).self_and_parents.last.to_sym else break end end ancestry << :root if component_should_merge_root?(component) ancestry else [locale] end locales end |
.path(locale, component, extension) ⇒ Object
162 163 164 165 166 167 |
# File 'lib/cldr/export.rb', line 162 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
108 109 110 111 |
# File 'lib/cldr/export.rb', line 108 def plural_data(component, locale, = {}) data = locale_based_data(component, locale, ) "{ :'#{locale}' => { :i18n => { :plural => { :keys => #{data[:keys].inspect}, :rule => #{data[:rule]} } } } }" end |
.shared_data(component, options = {}) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/cldr/export.rb', line 113 def shared_data(component, = {}) case component.to_s when 'Transforms' # do nothing, this has to be handled separately else Data.const_get(component.to_s.camelize).new end end |
.to_i18n(locale) ⇒ Object
122 123 124 |
# File 'lib/cldr/export.rb', line 122 def to_i18n(locale) return locale.to_s.gsub('_', '-').to_sym end |
.write(path, data) ⇒ Object
156 157 158 159 160 |
# File 'lib/cldr/export.rb', line 156 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 |