class I18n::CsvTranslation::Exporter

Public Class Methods

new(col_sep: ";") click to toggle source
# File lib/i18n/csv_translation/exporter.rb, line 6
def initialize col_sep: ";"
  @col_sep = col_sep
end

Public Instance Methods

export(path:, files: "*.yml", output:, &block) click to toggle source
# File lib/i18n/csv_translation/exporter.rb, line 10
def export path:, files: "*.yml", output:, &block
  @path   = path
  @files  = files
  @output = output

  translations = load_translations_into_hash &block
  save_translations_to_csv translations
end

Private Instance Methods

current_path() click to toggle source
# File lib/i18n/csv_translation/exporter.rb, line 70
def current_path
  Pathname.new(File.dirname(__FILE__))
end
flat_translation_hash(translations, parent=[]) click to toggle source
# File lib/i18n/csv_translation/exporter.rb, line 54
def flat_translation_hash translations, parent=[]
  result = {}

  translations.each do |key, values|
    current_key = parent.dup << key

    if values.is_a?(Hash)
      result.merge! flat_translation_hash(values, current_key)
    else
      result[current_key.join(".")] = values
    end
  end

  result
end
load_translations_into_hash() { |file| ... } click to toggle source
# File lib/i18n/csv_translation/exporter.rb, line 21
def load_translations_into_hash
  translations = {}

  Dir[current_path.join(@path, "**", @files)].sort.each do |file|
    process_file =  if block_given?
                      yield(file)
                    else
                      true
                    end

    if process_file
      ymls = YAML.load_file file
      translations.merge! output_filename(file, yml_locale(ymls)) => flat_translation_hash(ymls.values.first)
    end
  end

  translations
end
output_filename(file, old_locale) click to toggle source
# File lib/i18n/csv_translation/exporter.rb, line 74
def output_filename file, old_locale
  File.basename(file).gsub("#{old_locale}.", "")
end
save_translations_to_csv(translations) click to toggle source
# File lib/i18n/csv_translation/exporter.rb, line 40
def save_translations_to_csv translations
  CSV.open(@output, "w", col_sep: @col_sep) do |csv|
    translations.each do |key, value|
      if value.is_a?(Hash)
        value.each do |inner_key, inner_value|
          csv << [key, inner_key, inner_value]
        end
      else
        csv << [key, value]
      end
    end
  end
end
yml_locale(yml) click to toggle source
# File lib/i18n/csv_translation/exporter.rb, line 78
def yml_locale yml
  yml.keys.first
end