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/subdivisions.rb,
lib/cldr/export/data/country_codes.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[ CountryCodes CurrencyDigitsAndRounding Metazones NumberingSystems RbnfRoot SegmentsRoot TerritoriesContainment WindowsZones Transforms LikelySubtags Variables Aliases RegionCurrencies ]
Class Method Summary collapse
- .base_path ⇒ Object
- .base_path=(base_path) ⇒ Object
- .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
- .should_merge_root?(locale, component, options) ⇒ Boolean
- .to_i18n(locale) ⇒ Object
- .write(path, data) ⇒ Object
Class Method Details
.base_path ⇒ Object
33 34 35 |
# File 'lib/cldr/export.rb', line 33 def base_path @@base_path ||= File.('./data') end |
.base_path=(base_path) ⇒ Object
37 38 39 |
# File 'lib/cldr/export.rb', line 37 def base_path=(base_path) @@base_path = base_path end |
.data(component, locale, options = {}) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/cldr/export.rb', line 82 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
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 75 |
# File 'lib/cldr/export.rb', line 41 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
77 78 79 80 |
# File 'lib/cldr/export.rb', line 77 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
127 128 129 |
# File 'lib/cldr/export.rb', line 127 def from_i18n(locale) return locale.to_s.gsub('-', '_') end |
.is_shared_component?(component) ⇒ Boolean
170 171 172 |
# File 'lib/cldr/export.rb', line 170 def is_shared_component?(component) SHARED_COMPONENTS.include?(component) end |
.locale_based_data(component, locale, options = {}) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/cldr/export.rb', line 95 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
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/cldr/export.rb', line 131 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 else [locale] end locales << :root if should_merge_root?(locale, component, ) locales end |
.path(locale, component, extension) ⇒ Object
163 164 165 166 167 168 |
# File 'lib/cldr/export.rb', line 163 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
109 110 111 112 |
# File 'lib/cldr/export.rb', line 109 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
114 115 116 117 118 119 120 121 |
# File 'lib/cldr/export.rb', line 114 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 |
.should_merge_root?(locale, component, options) ⇒ Boolean
174 175 176 177 178 |
# File 'lib/cldr/export.rb', line 174 def should_merge_root?(locale, component, ) return false if %w(Rbnf Fields).include?(component) return true if [:merge] locale == :en end |
.to_i18n(locale) ⇒ Object
123 124 125 |
# File 'lib/cldr/export.rb', line 123 def to_i18n(locale) return locale.to_s.gsub('_', '-').to_sym end |
.write(path, data) ⇒ Object
157 158 159 160 161 |
# File 'lib/cldr/export.rb', line 157 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 |