Skip to content

Commit

Permalink
Add to LocaleFile#error when a file contains invalid pluralization keys
Browse files Browse the repository at this point in the history
  • Loading branch information
tigrish committed Dec 31, 2011
1 parent 58ad0d8 commit b4fe590
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -43,3 +43,13 @@ If you need to test that all translations have been completed :
describe "config/locales/fr.yml" do
it { should be_a_complete_translation_of 'config/locales/en.yml' }
end

## Rake tasks

You can check a locale file with the following task :

rake i18n:check FILEPATH

or check a whole directory :

rake i18n:check DIRECTORY
@@ -1,11 +1,10 @@
RSpec::Matchers.define :have_valid_pluralization_keys do |expected|
match do |actual|
locale_file = I18nSpec::LocaleFile.new(actual)
@invalid_keys = locale_file.invalid_pluralization_keys
@invalid_keys.empty?
@locale_file = I18nSpec::LocaleFile.new(actual)
@locale_file.invalid_pluralization_keys.empty?
end

failure_message_for_should do |filepath|
"expected #{filepath} to not include invalid pluralization keys :\n- " << @invalid_keys.join("\n- ")
"expected #{filepath} to not include invalid pluralization keys :\n- " << @locale_file.errors[:invalid_pluralization_keys].join("\n- ")
end
end
1 change: 1 addition & 0 deletions lib/i18n-spec/models/locale_file.rb
Expand Up @@ -35,6 +35,7 @@ def invalid_pluralization_keys
invalid << [parent, key].join('.') unless PLURALIZATION_KEYS.include?(key)
end
end
@errors[:invalid_pluralization_keys] = invalid unless invalid.empty?
invalid
end

Expand Down
4 changes: 2 additions & 2 deletions lib/i18n-spec/tasks/checker.rb
Expand Up @@ -29,8 +29,8 @@
break
end

unless (details = locale_file.invalid_pluralization_keys).empty?
log :error, 'invalid pluralization keys', format_array(details)
unless locale_file.invalid_pluralization_keys.empty?
log :error, 'invalid pluralization keys', format_array(locale_file.errors[:invalid_pluralization_keys])
errors += 1
end

Expand Down
7 changes: 6 additions & 1 deletion spec/lib/i18n-spec/models/locale_file_spec.rb
Expand Up @@ -71,10 +71,15 @@ def locale_file_with_content(content)
birds:
zero: zero
other: other"}
let(:locale_file) { locale_file_with_content(content) }

it "returns pluralization keys where key is not in PLURALIZATION_KEYS" do
locale_file = locale_file_with_content(content)
locale_file.invalid_pluralization_keys.should == ['en.cats.tommy', 'en.cats.tabby', 'en.dogs.some']
end

it "adds a :invalid_pluralization_keys error with each invalid key" do
locale_file.invalid_pluralization_keys
locale_file.errors[:invalid_pluralization_keys].should == ['en.cats.tommy', 'en.cats.tabby', 'en.dogs.some']
end
end
end

0 comments on commit b4fe590

Please sign in to comment.