Skip to content
This repository has been archived by the owner on Dec 12, 2019. It is now read-only.

Commit

Permalink
Bugfix in Translate::Keys#missing_keys - making sure it checks not on…
Browse files Browse the repository at this point in the history
…ly the leaf nodes in the I18n nested key hash but all nodes. This error was triggered for us by a key containing the :one and :other pluralization keys falsely being reported as missing by the rake task run by our tests.
  • Loading branch information
Peter Marklund committed Jun 16, 2010
1 parent c534d27 commit 93c06de
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
28 changes: 26 additions & 2 deletions lib/translate/keys.rb
Expand Up @@ -40,13 +40,37 @@ def missing_keys
yaml_keys = Translate::Storage.file_paths(locale).inject({}) do |keys, path|
keys = keys.deep_merge(Translate::File.new(path).read[locale.to_s])
end
yaml_keys = Translate::Keys.to_shallow_hash(yaml_keys)
files.reject { |key, file| !yaml_keys[key.to_s].nil? }
files.reject { |key, file| self.class.contains_key?(yaml_keys, key) }
end

def self.translated_locales
I18n.available_locales.reject { |locale| [:root, I18n.default_locale.to_sym].include?(locale) }
end

# Checks if a nested hash contains the keys in dot separated I18n key.
#
# Example:
#
# hash = {
# :foo => {
# :bar => {
# :baz => 1
# }
# }
# }
#
# contains_key?("foo", key) # => true
# contains_key?("foo.bar", key) # => true
# contains_key?("foo.bar.baz", key) # => true
# contains_key?("foo.bar.baz.bla", key) # => false
#
def self.contains_key?(hash, key)
keys = key.to_s.split(".")
return false if keys.empty?
!keys.inject(HashWithIndifferentAccess.new(hash)) do |memo, key|
memo.is_a?(Hash) ? memo.try(:[], key) : nil
end.nil?
end

# Convert something like:
#
Expand Down
25 changes: 24 additions & 1 deletion spec/keys_spec.rb
Expand Up @@ -49,7 +49,11 @@
Translate::File.new(@file_path).write({
:en => {
:home => {
:page_title => false
:page_title => false,
:intro => {
:one => "intro one",
:other => "intro other"
}
}
}
})
Expand All @@ -62,6 +66,7 @@
it "should return a hash with keys that are not in the locale file" do
@keys.stub!(:files).and_return({
:'home.page_title' => "app/views/home/index.rhtml",
:'home.intro' => 'app/views/home/index.rhtml',
:'home.signup' => "app/views/home/_signup.rhtml",
:'about.index.page_title' => "app/views/about/index.rhtml"
})
Expand All @@ -71,6 +76,24 @@
}
end
end

describe "contains_key?" do
it "works" do
hash = {
:foo => {
:bar => {
:baz => false
}
}
}
Translate::Keys.contains_key?(hash, "").should be_false
Translate::Keys.contains_key?(hash, "foo").should be_true
Translate::Keys.contains_key?(hash, "foo.bar").should be_true
Translate::Keys.contains_key?(hash, "foo.bar.baz").should be_true
Translate::Keys.contains_key?(hash, :"foo.bar.baz").should be_true
Translate::Keys.contains_key?(hash, "foo.bar.baz.bla").should be_false
end
end

describe "translated_locales" do
before(:each) do
Expand Down

0 comments on commit 93c06de

Please sign in to comment.