Sha256: 4d38ad9312badb0895782a8c2317bc7458109121165903f0ae2eeefbaaad2058
Contents?: true
Size: 1.94 KB
Versions: 1
Compression:
Stored size: 1.94 KB
Contents
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 = "") fail 'roots argument is required' if roots.nil? normalized_path = File.expand_path(path) path_root(normalized_path, roots) or fail CommandError.new( "Error scanning #{normalized_path}: cannot resolve relative key \"#{key}\".\nSet search.relative_roots in config/i18n-tasks.yml (currently #{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? controller_name = file_name.sub(/_controller$/, '') "#{controller_name}.#{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.9.0.rc1 | lib/i18n/tasks/scanners/relative_keys.rb |