class TwitterCldr::Resources::LocalesResourcesImporter

Constants

BUDDHIST_CALENDAR
LOCALE_COMPONENTS
SHARED_COMPONENTS

transforms are done by the TransformsImporter

Private Instance Methods

add_buddhist_calendar(component, locale, path) click to toggle source

TODO: export buddhist calendar from CLDR data instead of using BUDDHIST_CALENDAR constant.

# File lib/twitter_cldr/resources/locales_resources_importer.rb, line 145
def add_buddhist_calendar(component, locale, path)
  return unless component == 'Calendars' && locale == :th

  data = YAML.load(File.read(path))
  data['th']['calendars']['buddhist'] = BUDDHIST_CALENDAR

  File.open(path, 'w:utf-8') { |output| output.write(YAML.dump(data))}
end
after_prepare() click to toggle source
# File lib/twitter_cldr/resources/locales_resources_importer.rb, line 49
def after_prepare
  Cldr::Export::Data.dir = requirements[:cldr].common_path
end
components_for(locale) click to toggle source
# File lib/twitter_cldr/resources/locales_resources_importer.rb, line 96
def components_for(locale)
  if File.exist?(File.join(requirements[:cldr].source_path, 'common', 'rbnf', "#{locale}.xml"))
    LOCALE_COMPONENTS
  else
    LOCALE_COMPONENTS - ['rbnf']
  end
end
deep_symbolize(path) click to toggle source
# File lib/twitter_cldr/resources/locales_resources_importer.rb, line 104
def deep_symbolize(path)
  return unless File.extname(path) == '.yml'
  data = YAML.load(File.read(path))

  File.open(path, 'w:utf-8') do |output|
    output.write(
      TwitterCldr::Utils::YAML.dump(
        TwitterCldr::Utils.deep_symbolize_keys(data),
        use_natural_symbols: true
      )
    )
  end
end
execute() click to toggle source
# File lib/twitter_cldr/resources/locales_resources_importer.rb, line 45
def execute
  import_components
end
import_components() click to toggle source
# File lib/twitter_cldr/resources/locales_resources_importer.rb, line 57
def import_components
  locales = Set.new

  finish = -> (locale, *) do
    locales.add(locale)
    STDOUT.write "\rImported #{locale}, #{locales.size} of #{params[:locales].size} total"
  end

  Parallel.each(params[:locales], in_processes: Etc.nprocessors, finish: finish) do |locale|
    export_args = {
      locales: [locale],
      components: components_for(locale),
      target: File.join(output_path, 'locales'),
      merge: true  # fill in the gaps, eg fill in sub-locales like en_GB with en
    }

    Cldr::Export.export(export_args) do |component, locale, path|
      add_buddhist_calendar(component, locale, path)
      process_plurals(component, locale, path)
      deep_symbolize(path)
    end
  end

  puts ''

  shared_output_path = File.join(output_path, 'shared')
  FileUtils.mkdir_p(shared_output_path)

  export_args = {
    components: SHARED_COMPONENTS,
    target: shared_output_path,
    merge: true
  }

  Cldr::Export.export(export_args) do |component, locale, path|
    deep_symbolize(path)
  end
end
output_path() click to toggle source
# File lib/twitter_cldr/resources/locales_resources_importer.rb, line 53
def output_path
  params.fetch(:output_path)
end
process_plurals(component, locale, path) click to toggle source
# File lib/twitter_cldr/resources/locales_resources_importer.rb, line 118
def process_plurals(component, locale, path)
  return unless component == 'PluralRules'

  output_file = File.join(File.dirname(path), 'plurals.yml')

  plurals = YAML.load(File.read(path))[locale.to_s].inject({}) do |ret, (rule_type, rule_data)|
    rule_list = CldrPlurals::Compiler::RuleList.new(locale)

    rule_data.each_pair do |name, rule_text|
      rule_list.add_rule(name.to_sym, rule_text) unless name == 'other'
    end

    ret[rule_type.to_sym] = {
      rule: rule_list.to_code(:ruby),
      names: rule_list.rules.map(&:name) + [:other]
    }

    ret
  end

  File.open(output_file, 'w:utf-8') do |output|
    output.write(YAML.dump(locale => plurals))
  end
end