Sha256: 5b0ef83f8ca7b4e589f7ee7ad590db8244b21275b1d26571062ed0fd49ac5732

Contents?: true

Size: 1.62 KB

Versions: 4

Compression:

Stored size: 1.62 KB

Contents

require 'fileutils'

class TranslationEngine::Downloader

  def self.etag?(etag)
    response = @current_etag == etag
    Rails.logger.info { "Translations are up to date" } if response
    @current_etag = etag
    response
  end

  def update
    return nil if self.class.etag?(connection.get_translations_head[:etag])
    I18n.backend.reload!
  end

  def store
    if (yml_data = receive_translations)
      Rails.logger.info { "Storing translations to #{locale_file}" }
      write(locale_file, yml_data)
      add_translation_file_to_i18n
      I18n.backend.reload!
    end
  end

  def store_release(release)
    locale = release.split('_')[0...-1].join('_')
    filename = releases_dir.join("#{locale.downcase}.yml")
    if (yml_data = connection.get_release(release).body)
      Rails.logger.info { "Storing release #{release} to #{filename}" }
      write(filename, yml_data)
    end
  end

  private

  def add_translation_file_to_i18n
    unless I18n.load_path.include?(locale_file)
      I18n.load_path += [locale_file]
    end
  end

  def connection
    @connection ||= TranslationEngine::Connection.new
  end

  def locale_dir
    Rails.root.join('config', 'locales')
  end

  def locale_file
    locale_dir.join('z_translation_engine.yml')
  end

  def receive_translations
    return if self.class.etag?(connection.get_translations_head[:etag])

    connection.get_translations.body
  end

  def releases_dir
    dir = Rails.root.join('config', 'locales', 'z_releases')
    FileUtils.mkdir_p(dir) unless File.exists?(dir)
    dir
  end

  def write(filename, data)
    File.open(filename, 'wb') do |f|
      f.write data
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
translation_engine-0.0.4 app/models/translation_engine/downloader.rb
translation_engine-0.0.3 app/models/translation_engine/downloader.rb
translation_engine-0.0.2 app/models/translation_engine/downloader.rb
translation_engine-0.0.1 app/models/translation_engine/downloader.rb