class TranslationIO::Client::SyncOperation::ApplyYamlSourceEditsStep

Public Class Methods

new(yaml_file_paths, source_locale) click to toggle source
# File lib/translation_io/client/sync_operation/apply_yaml_source_edits_step.rb, line 5
def initialize(yaml_file_paths, source_locale)
  @yaml_file_paths = yaml_file_paths
  @source_locale   = source_locale
end

Public Instance Methods

run(params) click to toggle source
# File lib/translation_io/client/sync_operation/apply_yaml_source_edits_step.rb, line 10
def run(params)
  TranslationIO.info "Downloading YAML source editions."

  params.merge!({ :timestamp => metadata_timestamp })
  parsed_response = perform_source_edits_request(params)
  source_edits    = parsed_response['source_edits'].to_a

  TranslationIO.info "Applying YAML source editions."

  source_edits.each do |source_edit|
    applied = false

    reload_or_reuse_yaml_sources

    @yaml_sources.each do |yaml_source|
      yaml_file_path = yaml_source[:yaml_file_path]
      yaml_flat_hash = yaml_source[:yaml_flat_hash]

      yaml_flat_hash.each do |full_key, value|
        if full_key == "#{@source_locale}.#{source_edit['key']}"
          apply_source_edit(source_edit, yaml_file_path, yaml_flat_hash)
          applied = true
          break
        end
      end

      break if applied
    end
  end

  update_metadata_timestamp
end

Private Instance Methods

apply_application_source_edit(source_edit, yaml_file_path, yaml_flat_hash) click to toggle source
# File lib/translation_io/client/sync_operation/apply_yaml_source_edits_step.rb, line 92
def apply_application_source_edit(source_edit, yaml_file_path, yaml_flat_hash)
  full_key                 = "#{@source_locale}.#{source_edit['key']}"
  yaml_flat_hash[full_key] = source_edit['new_text']
  file_content             = to_hash_to_yaml(yaml_flat_hash)

  File.open(yaml_file_path, 'w') do |f|
    f.write(file_content)
  end
end
apply_gem_source_edit(source_edit) click to toggle source
# File lib/translation_io/client/sync_operation/apply_yaml_source_edits_step.rb, line 102
def apply_gem_source_edit(source_edit)
  # Source yaml file like config/locales/en.yml
  yaml_file_path = File.expand_path(File.join(TranslationIO.config.yaml_locales_path, "#{@source_locale}.yml"))

  if File.exists?(yaml_file_path)
    # Complete existing hash if YAML file already exists
    existing_yaml_source = @yaml_sources.detect { |y_s| normalize_path(y_s[:yaml_file_path]) == normalize_path(yaml_file_path) }
    yaml_flat_hash       = existing_yaml_source[:yaml_flat_hash]
  else
    # Create new hash if YAML file doesn't exist yet
    FileUtils::mkdir_p File.dirname(yaml_file_path)
    yaml_flat_hash = {}
    @yaml_file_paths.push(yaml_file_path) # Application YAML are at the end of the list
  end

  apply_application_source_edit(source_edit, yaml_file_path, yaml_flat_hash)
end
apply_source_edit(source_edit, yaml_file_path, yaml_flat_hash) click to toggle source
# File lib/translation_io/client/sync_operation/apply_yaml_source_edits_step.rb, line 76
def apply_source_edit(source_edit, yaml_file_path, yaml_flat_hash)
  full_key = "#{@source_locale}.#{source_edit['key']}"

  if yaml_flat_hash[full_key] == source_edit['old_text']
    TranslationIO.info "#{source_edit['key']} | #{source_edit['old_text']} -> #{source_edit['new_text']}", 2, 2

    if locale_file_path_in_project?(yaml_file_path)
      apply_application_source_edit(source_edit, yaml_file_path, yaml_flat_hash)
    else # Override source text of gem inside the app
      apply_gem_source_edit(source_edit)
    end
  else
    TranslationIO.info "#{source_edit['key']} | #{source_edit['old_text']} -> #{source_edit['new_text']} | Ignored because translation was also updated in source YAML file", 2, 2
  end
end
locale_file_path_in_project?(locale_file_path) click to toggle source
# File lib/translation_io/client/sync_operation/apply_yaml_source_edits_step.rb, line 158
def locale_file_path_in_project?(locale_file_path)
  normalize_path(locale_file_path).start_with?(
    normalize_path(TranslationIO.config.yaml_locales_path)
  )
end
metadata_timestamp() click to toggle source
# File lib/translation_io/client/sync_operation/apply_yaml_source_edits_step.rb, line 132
def metadata_timestamp
  if File.exist?(TranslationIO.config.metadata_path)
    metadata_content = File.read(TranslationIO.config.metadata_path)

    if metadata_content.include?('>>>>') || metadata_content.include?('<<<<')
      TranslationIO.info "[Error] #{TranslationIO.config.metadata_path} file is corrupted and seems to have unresolved versioning conflicts. Please resolve them and try again."
      exit(false)
    else
      return YAML::load(metadata_content)['timestamp'] rescue 0
    end
  else
    return 0
  end
end
normalize_path(path) click to toggle source
# File lib/translation_io/client/sync_operation/apply_yaml_source_edits_step.rb, line 164
def normalize_path(path)
  TranslationIO.normalize_path(path)
end
perform_source_edits_request(params) click to toggle source
# File lib/translation_io/client/sync_operation/apply_yaml_source_edits_step.rb, line 153
def perform_source_edits_request(params)
  uri             = URI("#{TranslationIO.client.endpoint}/projects/#{TranslationIO.client.api_key}/source_edits")
  parsed_response = BaseOperation.perform_request(uri, params)
end
reload_or_reuse_yaml_sources() click to toggle source
# File lib/translation_io/client/sync_operation/apply_yaml_source_edits_step.rb, line 45
def reload_or_reuse_yaml_sources
  if yaml_sources_reload_needed?
    @yaml_sources = sort_by_project_locales_first(@yaml_file_paths).collect do |yaml_file_path|
      yaml_content   = File.read(yaml_file_path)
      yaml_hash      = YAML::load(yaml_content)
      yaml_flat_hash = FlatHash.to_flat_hash(yaml_hash)

      {
        :yaml_file_path => yaml_file_path,
        :yaml_flat_hash => yaml_flat_hash
      }
    end
  else
    @yaml_sources
  end
end
sort_by_project_locales_first(yaml_file_paths) click to toggle source

Sort YAML file paths by project locales first, gem locales after (to replace “overridden” source first)

# File lib/translation_io/client/sync_operation/apply_yaml_source_edits_step.rb, line 68
def sort_by_project_locales_first(yaml_file_paths)
  yaml_file_paths.sort do |x, y|
    a = locale_file_path_in_project?(x)
    b = locale_file_path_in_project?(y)
    (!a && b) ? 1 : ((a && !b) ? -1 : 0)
  end
end
to_hash_to_yaml(yaml_flat_hash) click to toggle source
# File lib/translation_io/client/sync_operation/apply_yaml_source_edits_step.rb, line 120
def to_hash_to_yaml(yaml_flat_hash)
  yaml_hash = FlatHash.to_hash(yaml_flat_hash)

  if TranslationIO.config.yaml_line_width
    content = yaml_hash.to_yaml(:line_width => TranslationIO.config.yaml_line_width)
  else
    content = yaml_hash.to_yaml
  end

  content.gsub(/ $/, '') # remove trailing spaces
end
update_metadata_timestamp() click to toggle source
# File lib/translation_io/client/sync_operation/apply_yaml_source_edits_step.rb, line 147
def update_metadata_timestamp
  File.open(TranslationIO.config.metadata_path, 'w') do |f|
    f.write({ 'timestamp' => Time.now.utc.to_i }.to_yaml)
  end
end
yaml_sources_reload_needed?() click to toggle source
# File lib/translation_io/client/sync_operation/apply_yaml_source_edits_step.rb, line 62
def yaml_sources_reload_needed?
  @yaml_file_paths.sort != @yaml_sources.to_a.collect { |y_s| y_s[:yaml_file_path] }.sort
end