Sha256: 4d1dfc217718fed5959dac4f29e3c816ae347f8f73cecb157c53a10c326aa8b3

Contents?: true

Size: 1.86 KB

Versions: 5

Compression:

Stored size: 1.86 KB

Contents

# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

require 'json'
require 'open-uri'

module TwitterCldr
  module Resources

    class CustomLocalesResourcesImporter

      API_ENDPOINT = "http://translate.twttr.com/api/2/twitter/phrase/%s/translations.json"

      TIME_PERIODS = {
          :day    => 19636,
          :hour   => 19638,
          :minute => 19634,
          :second => 19639
      }

      # Arguments:
      #
      #   output_path - output directory for imported YAML files
      #
      def initialize(output_path)
        @output_path = output_path
      end

      def import
        import_units
      end

      private

      def import_units
        fetch_units_data.each do |locale, data|
          dir_path = File.join(@output_path, locale.to_s)

          FileUtils.mkpath(dir_path)

          File.open(File.join(dir_path, 'units.yml'), 'w:utf-8') do |output|
            output.write(YAML.dump({ locale => data }))
          end
        end
      end

      def fetch_units_data
        TIME_PERIODS.inject({}) do |result, (label, id)|
          api_response = JSON.parse(open(API_ENDPOINT % id).read)

          TwitterCldr.supported_locales.each do |locale|
            twitter_locale = TwitterCldr.twitter_locale(locale).to_s

            next unless api_response[twitter_locale]

            patterns = TwitterCldr::Formatters::Plurals::Rules.all_for(locale).inject({}) do |memo, rule|
              memo[rule] = api_response[twitter_locale].gsub("%{number}", "{0}"); memo
            end

            set_value(result, patterns, locale, :units, label, :abbreviated)
          end

          result
        end
      end

      def set_value(hash, value, *path)
        last = path[0..-2].inject(hash) do |current, level|
          current[level] ||= {}
        end

        last[path.last] = value
      end

    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
twitter_cldr-1.9.1 lib/twitter_cldr/resources/custom_locales_resources_importer.rb
twitter_cldr-1.9.0 lib/twitter_cldr/resources/custom_locales_resources_importer.rb
twitter_cldr-1.8.1 lib/twitter_cldr/resources/custom_locales_resources_importer.rb
twitter_cldr-1.8.0 lib/twitter_cldr/resources/custom_locales_resources_importer.rb
twitter_cldr-1.7.0 lib/twitter_cldr/resources/custom_locales_resources_importer.rb