Sha256: e5702a8e0e902074dbdd63acd37b9bb972ccb579d6e8efa2643b161ed22ff13d

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

# coding: utf-8
module I18n
  module Tasks
    module Scanners
      module RelativeKeys
        # @param key [String] relative i18n key (starts with a .)
        # @param path [String] path to the file containing the key
        # @return [String] absolute version of the key
        def absolutize_key(key, path, roots = relative_roots, closest_method = "")
          normalized_path = File.expand_path(path)
          path_root(normalized_path, roots) or
            raise CommandError.new(
              "Error scanning #{normalized_path}: cannot resolve relative key
              \"#{key}\".\nSet relative_roots in config/i18n-tasks.yml
              (currently #{relative_roots.inspect})"
            )

          prefix_key_based_on_path(key, normalized_path, roots, closest_method: closest_method)
        end

        private

        # Detect the appropriate relative path root
        # @param [String] path /full/path
        # @param [Array<String>] roots array of full paths
        # @return [String] the closest ancestor root for path
        def path_root(path, roots)
          expanded_relative_roots(roots).sort.reverse_each.detect do |root|
            path.start_with?(root + '/')
          end
        end

        def expanded_relative_roots(roots)
          roots.map { |path| File.expand_path(path) }
        end

        def prefix_key_based_on_path(key, normalized_path, roots, options = {})
          "#{prefix(normalized_path, roots, options)}#{key}"
        end

        def prefix(normalized_path, roots, options = {})
          file_name = normalized_path.gsub(%r(#{path_root(normalized_path, roots)}/|(\.[^/]+)*$), '')

          if options[:closest_method].present?
            "#{file_name.split('_').first}.#{options[:closest_method]}".tr('/', '.')
          else
            file_name.tr('/', '.').gsub(%r(\._), '.')
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
i18n-tasks-0.7.12 lib/i18n/tasks/scanners/relative_keys.rb