class RailsTranslationManager::Exporter

Public Class Methods

new(directory, source_locale_path, target_locale_path) click to toggle source
# File lib/rails_translation_manager/exporter.rb, line 9
def initialize(directory, source_locale_path, target_locale_path)
  @source_locale_path = source_locale_path
  @target_locale_path = target_locale_path
  @source_locale = File.basename(target_locale_path).split(".")[0]
  @target_locale = File.basename(target_locale_path).split(".")[0]
  @output_path = File.join(directory, @target_locale + ".csv")

  @keyed_source_data = translation_file_to_keyed_data(source_locale_path, @source_locale)
  @keyed_target_data = translation_file_to_keyed_data(target_locale_path, @target_locale)
end

Public Instance Methods

export() click to toggle source
# File lib/rails_translation_manager/exporter.rb, line 20
def export
  csv = CSV.generate do |csv|
    csv << CSV::Row.new(["key", "source", "translation"], ["key", "source", "translation"], true)
    @keyed_source_data.keys.sort.each do |key|
      if key =~ /^language_names\./
        next unless key =~ /#{@target_locale}$/
      end
      if is_pluralized_key?(key)
        export_pluralization_rows(key, csv)
      else
        csv << export_row(key, @keyed_source_data[key], @keyed_target_data[key])
      end
    end
  end
  File.open(@output_path, "w") { |f| f.write csv.to_s }
end

Private Instance Methods

depluralized_key_for(prefix, key) click to toggle source
# File lib/rails_translation_manager/exporter.rb, line 107
def depluralized_key_for(prefix, key)
  depluralized_prefix = prefix.gsub(/\APLURALIZATION\-KEY\:/, '')
  key_for(depluralized_prefix, key)
end
export_pluralization_rows(key, csv) click to toggle source
# File lib/rails_translation_manager/exporter.rb, line 39
def export_pluralization_rows(key, csv)
  I18n.t('i18n.plural.keys', locale: @target_locale).map(&:to_s).each do |plural_key|
    csv << export_row(depluralized_key_for(key, plural_key), @keyed_source_data.fetch(key, {})[plural_key], @keyed_target_data.fetch(key, {})[plural_key])
  end
end
export_row(key, source_value, target_value) click to toggle source
# File lib/rails_translation_manager/exporter.rb, line 45
def export_row(key, source_value, target_value)
  CSV::Row.new(['key', 'source', 'translation'], [key, source_value, target_value])
end
hash_is_for_pluralization?(hash, locale) click to toggle source

if the hash is only made up of the plural keys for the locale, we assume it's a plualization set. Note that zero is always an option regardless of the keys fetched (see github.com/svenfuchs/i18n/blob/master/lib/i18n/backend/pluralization.rb#L34)

# File lib/rails_translation_manager/exporter.rb, line 78
def hash_is_for_pluralization?(hash, locale)
  plural_keys = I18n.t('i18n.plural.keys', locale: locale)
  raise missing_pluralisations_message(locale) unless plural_keys.is_a?(Array)
  ((hash.keys.map(&:to_s) - plural_keys.map(&:to_s)) - ['zero']).empty?
end
hash_to_keyed_data(prefix, hash, locale) click to toggle source
# File lib/rails_translation_manager/exporter.rb, line 58
def hash_to_keyed_data(prefix, hash, locale)
  if hash_is_for_pluralization?(hash, locale)
    {pluralized_prefix(prefix) => hash.stringify_keys}
  else
    results = {}
    hash.each do |key, value|
      if value.is_a?(Hash)
        results.merge!(hash_to_keyed_data(key_for(prefix, key), value, locale))
      else
        results[key_for(prefix, key)] = value
      end
    end
    results
  end
end
is_pluralized_key?(key) click to toggle source
# File lib/rails_translation_manager/exporter.rb, line 95
def is_pluralized_key?(key)
  key =~ /\APLURALIZATION\-KEY\:/
end
key_for(prefix, key) click to toggle source
# File lib/rails_translation_manager/exporter.rb, line 91
def key_for(prefix, key)
  prefix.blank? ? key.to_s : "#{prefix}.#{key}"
end
missing_pluralisations_message(locale) click to toggle source
# File lib/rails_translation_manager/exporter.rb, line 84
def missing_pluralisations_message(locale)
  "No pluralization forms defined for locale '#{locale}'. " +
  "This probably means that the rails-18n gem does not provide a " +
  "definition of the plural forms for this locale, you may need to " +
  "define them yourself."
end
pluralized_prefix(prefix) click to toggle source
# File lib/rails_translation_manager/exporter.rb, line 99
def pluralized_prefix(prefix)
  if is_pluralized_key?(prefix)
    prefix
  else
    "PLURALIZATION-KEY:#{prefix}"
  end
end
translation_file_to_keyed_data(path, locale) click to toggle source
# File lib/rails_translation_manager/exporter.rb, line 49
def translation_file_to_keyed_data(path, locale)
  if File.exist?(path)
    hash = YAML.load_file(path).values[0]
    hash_to_keyed_data("", hash, locale)
  else
    {}
  end
end