Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bugfix in Translate::Keys#missing_keys - making sure it checks not on…

…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...
commit 93c06de8ee57a68330e0a771fd60c372e1b9672a 1 parent c534d27
Peter Marklund authored
Showing with 50 additions and 3 deletions.
  1. +26 −2 lib/translate/keys.rb
  2. +24 −1 spec/keys_spec.rb
View
28 lib/translate/keys.rb
@@ -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:
#
View
25 spec/keys_spec.rb
@@ -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"
+ }
}
}
})
@@ -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"
})
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.