Sha256: 2f1e9278345bd8e0cbca99e7878e645ed375c0f11f877beccea0a2328ef5aa0b

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

# coding: utf-8
require 'open3'
require 'term/ansicolor'

module I18n
  module Tasks
    module TaskHelpers
      include Term::ANSIColor
      def run_command(*args)
        _in, out, _err = Open3.popen3(*args)
        out.gets nil
      end

      # locale data hash, with locale name as root
      def get_locale_data(locale)
        # todo multiple files, configuration option
        YAML.load_file "config/locales/#{locale}.yml"
      end

      # main locale file path (for writing to)
      def locale_file_path(locale)
        "config/locales/#{locale}.yml"
      end

      # find all keys in the source
      def find_source_keys
        @source_keys ||= begin
          grep_out = run_command 'grep', '-horI', %q{\\bt(\\?\\s*['"]\\([^'"]*\\)['"]}, 'app/'
          used_keys = grep_out.split("\n").map { |r| r.match(/['"](.*?)['"]/)[1] }.uniq.to_set
        end
      end

      def find_source_pattern_keys
        find_source_keys.select { |k| k =~ /\#{.*?}/ || k.ends_with?('.') }
      end

      def find_source_pattern_prefixes
        find_source_pattern_keys.map { |k| k.split(/\.?#/)[0] }
      end

      # traverse hash, yielding with full key and value
      def traverse(path = '', hash, &block)
        hash.each do |k, v|
          if v.is_a?(Hash)
            traverse("#{path}.#{k}", v, &block)
          else
            block.call("#{path}.#{k}"[1..-1], v)
          end
        end
      end

      def t(hash, key)
        key.split('.').inject(hash) { |r, seg| r.try(:[], seg) }
      end

      def base_locale
        I18n.default_locale.to_s
      end

      def base
        @base ||= get_locale_data(base_locale)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
i18n-tasks-0.0.4 lib/i18n/tasks/task_helpers.rb