lib/missing_t.rb in missing_t-0.3.1 vs lib/missing_t.rb in missing_t-0.3.2

- old
+ new

@@ -2,19 +2,10 @@ require "optparse" require "ostruct" require "forwardable" class Hash - def has_nested_key?(key) - h = self - key.to_s.split('.').each do |segment| - return false unless h.key?(segment) - h = h[segment] - end - true - end - # idea snatched from deep_merge in Rails source code def deep_safe_merge(other_hash) self.merge(other_hash) do |key, oldval, newval| oldval = oldval.to_hash if oldval.respond_to?(:to_hash) newval = newval.to_hash if newval.respond_to?(:to_hash) @@ -50,20 +41,24 @@ end class MissingT - VERSION = "0.3.1" + class FileReader + def read(file) + open(File.expand_path(file), "r") do |f| + yield f.read + end + end + end + VERSION = "0.3.2" + include Helpers - extend Forwardable - def_delegators :@translations, :[] - # attr_reader :translations - - def initialize - @translations = Hash.new + def initialize(reader) + @reader = reader end def parse_options(args) @options = OpenStruct.new @options.prefix = nil @@ -86,96 +81,86 @@ end opts.parse!(args) end - # NOTE: this method is needed - # because attr_reader :translations - # does not seem to be stubbable - def translations - @translations - end - - def add_translations(trs) - translations.deep_safe_merge!(trs) - end - - def collect_translations + def translation_keys locales_pathes = ["config/locales/**/*.yml", "vendor/plugins/**/config/locales/**/*yml", "vendor/plugins/**/locale/**/*yml"] - locales_pathes.each do |path| + locales_pathes.each_with_object({}) do |path, translations| Dir.glob(path) do |file| - add_translations(translations_in_file(file)) + t = open(file) { |f| YAML.load(f.read) } + translations.deep_safe_merge!(t) end end end - def translations_in_file(yaml_file) - open(yaml_file) { |f| YAML.load(f.read) } - end - def files_with_i18n_queries if path = @options.path path = path[0...-1] if path[-1..-1] == '/' - [ Dir.glob("#{path}/**/*.erb"), Dir.glob("#{path}/**/*.rb") ] + [ + Dir.glob("#{path}/**/*.erb"), + Dir.glob("#{path}/**/*.haml"), + Dir.glob("#{path}/**/*.rb") + ] else - [ Dir.glob("app/**/*.erb"), - Dir.glob("app/**/controllers/**/*.rb"), - Dir.glob("app/**/helpers/**/*.rb")] + [ + Dir.glob("app/**/*.erb"), + Dir.glob("app/**/*.haml"), + Dir.glob("app/**/models/**/*.rb"), + Dir.glob("app/**/controllers/**/*.rb"), + Dir.glob("app/**/helpers/**/*.rb") + ] end.flatten end - def get_content_of_file_with_i18n_queries(file) - f = open(File.expand_path(file), "r") - content = f.read() - f.close() - content - end - def extract_i18n_queries(file) i18n_query_pattern = /[^\w]+(?:I18n\.translate|I18n\.t|translate|t)\s*\((.*?)[,\)]/ i18n_query_no_parens_pattern = /[^\w]+(?:I18n\.translate|I18n\.t|translate|t)\s+(['"])(.*?)\1/ - file_content = get_content_of_file_with_i18n_queries(file) - file_content.scan(i18n_query_pattern).map { |match| match.first.gsub(/['"\s]/, '') }. - concat(file_content.scan(i18n_query_no_parens_pattern).map { |match| match[1].gsub(/['"\s]/, '') }) + + @reader.read(file) do |content| + ([]).tap do |i18n_message_strings| + i18n_message_strings << content.scan(i18n_query_pattern).map { |match| match[0].gsub(/['"\s]/, '') } + i18n_message_strings << content.scan(i18n_query_no_parens_pattern).map { |match| match[1].gsub(/['"\s]/, '') } + end.flatten + end end - def collect_translation_queries + def translation_queries files_with_i18n_queries.each_with_object({}) do |file, queries| queries_in_file = extract_i18n_queries(file) if queries_in_file.any? queries[file] = queries_in_file end end #TODO: remove duplicate queries across files end - def has_translation?(lang, query) - t = translations + def has_translation?(keys, lang, query) i18n_label(lang, query).split('.').each do |segment| - return false unless segment =~ /#\{.*\}/ or (t.respond_to?(:key?) and t.key?(segment)) - t = t[segment] + return false unless segment =~ /#\{.*\}/ or (keys.respond_to?(:key?) and keys.key?(segment)) + keys = keys[segment] end true end - def get_missing_translations(queries, languages) + def get_missing_translations(keys, queries, languages) languages.each_with_object({}) do |lang, missing| - get_missing_translations_for_lang(queries, lang).each do |file, queries| + get_missing_translations_for_lang(keys, queries, lang).each do |file, queries| missing[file] ||= [] missing[file].concat(queries).uniq! end end end def find_missing_translations(lang=nil) - collect_translations - get_missing_translations(collect_translation_queries, lang ? [lang] : translations.keys) + ts = translation_keys + get_missing_translations(translation_keys, translation_queries, lang ? [lang] : ts.keys) end private - def get_missing_translations_for_lang(queries, lang) + def get_missing_translations_for_lang(keys, queries, lang) queries.map do |file, queries_in_file| - queries_with_no_translation = queries_in_file.select { |q| !has_translation?(lang, q) } + queries_with_no_translation = queries_in_file.select { |q| !has_translation?(keys, lang, q) } if queries_with_no_translation.empty? nil else [file, queries_with_no_translation.map { |q| i18n_label(lang, q) }] end