class TranslationIO::Client::InitOperation::CleanupYamlFilesStep

Public Class Methods

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

Public Instance Methods

run() click to toggle source
# File lib/translation_io/client/init_operation/cleanup_yaml_files_step.rb, line 12
def run
  @yaml_file_paths.each do |locale_file_path|
    if locale_file_removable?(locale_file_path)
      if File.exist?(locale_file_path)
        content_hash        = YAML::load(File.read(locale_file_path)) || {}
        source_content_hash = content_hash.select { |k| k.to_s == @source_locale.to_s }

        if source_content_hash.empty?
          TranslationIO.info "Removing #{locale_file_path}", 2, 2
          FileUtils.rm(locale_file_path)
        elsif content_hash != source_content_hash # in case of mixed languages in source YAML file
          TranslationIO.info "Rewriting #{locale_file_path}", 2, 2

          if TranslationIO.config.yaml_line_width
            file_content = source_content_hash.to_yaml(:line_width => TranslationIO.config.yaml_line_width)
          else
            file_content = source_content_hash.to_yaml
          end

          file_content = file_content.gsub(/ $/, '') # remove trailing spaces

          File.open(locale_file_path, 'wb') do |file|
            file.write(file_content)
          end
        else
          # don't touch source
        end
      end
    end
  end
end

Private Instance Methods

locale_file_path_in_project?(locale_file_path) click to toggle source
# File lib/translation_io/client/init_operation/cleanup_yaml_files_step.rb, line 61
def locale_file_path_in_project?(locale_file_path)
  TranslationIO.normalize_path(locale_file_path).start_with?(
    TranslationIO.normalize_path(@yaml_locales_path)
  )
end
locale_file_removable?(locale_file_path) click to toggle source
# File lib/translation_io/client/init_operation/cleanup_yaml_files_step.rb, line 46
def locale_file_removable?(locale_file_path)
  in_project = locale_file_path_in_project?(locale_file_path)

  protected_file = @target_locales.any? do |target_locale|
    paths = [
      TranslationIO.normalize_path(File.join(@yaml_locales_path, "translation.#{target_locale}.yml" ).to_s),
      TranslationIO.normalize_path(File.join(@yaml_locales_path, "localization.#{target_locale}.yml").to_s)
    ]

    paths.include?(TranslationIO.normalize_path(locale_file_path))
  end

  in_project && !protected_file
end