diff --git a/lib/translate/keys.rb b/lib/translate/keys.rb index 6cb7547..55fea1f 100644 --- a/lib/translate/keys.rb +++ b/lib/translate/keys.rb @@ -23,7 +23,7 @@ def keys def i18n_keys(locale) I18n.backend.send(:init_translations) unless I18n.backend.initialized? - extract_i18n_keys(I18n.backend.send(:translations)[locale.to_sym]).sort + Translate::Keys.to_shallow_hash(I18n.backend.send(:translations)[locale.to_sym]).keys.sort end def untranslated_keys @@ -106,20 +106,6 @@ def self.deep_merge!(hash1, hash2) private - def extract_i18n_keys(hash, parent_keys = []) - hash.inject([]) do |keys, (key, value)| - full_key = parent_keys + [key] - if value.is_a?(Hash) - # Nested hash - keys += extract_i18n_keys(value, full_key) - elsif value.present? - # String leaf node - keys << full_key.join(".") - end - keys - end - end - def extract_files files_to_scan.inject(HashWithIndifferentAccess.new) do |files, file| IO.read(file).scan(i18n_lookup_pattern).flatten.map(&:to_sym).each do |key| diff --git a/spec/keys_spec.rb b/spec/keys_spec.rb index d71d23a..f43cf27 100644 --- a/spec/keys_spec.rb +++ b/spec/keys_spec.rb @@ -28,7 +28,7 @@ it "should return all keys in the I18n backend translations hash" do I18n.backend.should_receive(:translations).and_return(translations) - @keys.i18n_keys(:en).should == ['articles.new.page_title', 'categories.flash.created', 'home.about'] + @keys.i18n_keys(:en).should == ['articles.new.page_title', 'categories.flash.created', 'empty', 'home.about'] end describe "untranslated_keys" do @@ -38,7 +38,7 @@ it "should return a hash with keys with missing translations in each locale" do @keys.untranslated_keys.should == { - :sv => ['articles.new.page_title', 'categories.flash.created'] + :sv => ['articles.new.page_title', 'categories.flash.created', 'empty'] } end end diff --git a/spec/log_spec.rb b/spec/log_spec.rb index 88e1e28..7dc9f54 100644 --- a/spec/log_spec.rb +++ b/spec/log_spec.rb @@ -7,7 +7,7 @@ I18n.locale = :sv I18n.backend.store_translations(:sv, from_texts) keys = Translate::Keys.new - @log = Translate::Log.new(:sv, :en, keys.send(:extract_i18n_keys, from_texts)) + @log = Translate::Log.new(:sv, :en, Translate::Keys.to_shallow_hash(from_texts).keys) @log.stub!(:file_path).and_return(file_path) FileUtils.rm_f file_path end diff --git a/tasks/translate.rake b/tasks/translate.rake index 70a6ef8..e5660b3 100644 --- a/tasks/translate.rake +++ b/tasks/translate.rake @@ -82,7 +82,7 @@ namespace :translate do locale = new_translations.keys.first overwrites = false - Translate::Keys.new.send(:extract_i18n_keys, new_translations[locale]).each do |key| + Translate::Keys.to_shallow_hash(new_translations[locale]).keys.each do |key| new_text = key.split(".").inject(new_translations[locale]) { |hash, sub_key| hash[sub_key] } existing_text = I18n.backend.send(:lookup, locale.to_sym, key) if existing_text && new_text != existing_text