class PutText::Rails::Extractor

Public Instance Methods

extract() click to toggle source

Extract string from the Rails project this gem is included in. @return [POFile] a POFile object containing the extracted strings.

# File lib/puttext-rails/extractor.rb, line 11
def extract
  puttext_extractor = PutText::Extractor.new
  po_file = puttext_extractor.extract(root_path)

  errors_file = extract_errors
  po_file.merge(errors_file)

  po_file
end

Private Instance Methods

create_entry_from_translation(translation) click to toggle source
# File lib/puttext-rails/extractor.rb, line 50
def create_entry_from_translation(translation)
  case translation
  when String
    POEntry.new(msgctxt: PutText::Rails::MSG_CONTEXT, msgid: translation)
  when Hash
    POEntry.new(
      msgctxt: PutText::Rails::MSG_CONTEXT,
      msgid: translation[:one] || translation[:other],
      msgid_plural: translation[:other]
    )
  end
end
extract_errors() click to toggle source
# File lib/puttext-rails/extractor.rb, line 27
def extract_errors
  entries = i18n_error_messages.map do |_key, value|
    create_entry_from_translation(value)
  end

  POFile.new(entries)
end
i18n_error_messages() click to toggle source
# File lib/puttext-rails/extractor.rb, line 35
def i18n_error_messages
  i18n_backend = I18n.backend

  unless i18n_backend.is_a? I18n::Backend::Simple
    raise 'only I18n::Backend::Simple is supported'
  end

  i18n_backend.available_locales # Force I18n to load translations

  translations = i18n_backend.send(:translations)
  translations.fetch(I18n.default_locale, {})
              .fetch(:errors, {})
              .fetch(:messages, {})
end
root_path() click to toggle source
# File lib/puttext-rails/extractor.rb, line 23
def root_path
  ::Rails.root.relative_path_from(Pathname.new(Dir.pwd))
end