Sha256: a0af2041d1d4de922df00b1b5970cd3a17be6469e76bd8dd35dbc0684414839e

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

module AwesomeTranslations::ObjectExtensions
  def t(key, *args, &blk)
    if key.is_a?(String) && key.start_with?(".")
      # Removed any helpful appended input to filepath from stacktrace.
      if match = caller[0].match(/\A(.+):(\d+):/)
        previous_file = match[1]
      else
        raise "Could not get previous file name from: #{caller[0]}"
      end

      # Remove any Rails root.
      removed_root = false
      AwesomeTranslations::ModelInspector.engines.each do |engine|
        root = engine.root.to_s

        if previous_file.starts_with?(root)
          previous_file = previous_file.gsub(/\A#{Regexp.escape(root)}\//, "")
          removed_root = true
          break
        end
      end

      dir = File.dirname(previous_file)
      dir = dir.gsub(/\A#{Regexp.escape(Rails.root.to_s)}\//, "")
      dir = dir.gsub(/\Aapp\//, "") if dir.starts_with?("app/")

      file = File.basename(previous_file, File.extname(previous_file))

      translation_key = dir
      translation_key = translation_key.gsub(/\Aapp\//, "")
      translation_key << "/#{file}"
      translation_key.gsub!("/", ".")
      translation_key << key

      # Change key to full path.
      key = translation_key
    end

    I18n.t(key, *args, &blk)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
awesome_translations-0.0.7 lib/awesome_translations/object_extensions.rb
awesome_translations-0.0.6 lib/awesome_translations/object_extensions.rb