From 5f9b7c67cd07ef78fbad5129285b1916da58a9ac Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Fri, 25 Sep 2015 00:43:23 +0200 Subject: [PATCH] Add tests to ensure correct pluralization --- spec/integrity/i18n_spec.rb | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/spec/integrity/i18n_spec.rb b/spec/integrity/i18n_spec.rb index e7b9c08fec9..5e95b706a72 100644 --- a/spec/integrity/i18n_spec.rb +++ b/spec/integrity/i18n_spec.rb @@ -58,7 +58,7 @@ describe "i18n integrity checks" do end end - describe 'keys in English locale files' do + describe 'English locale file' do locale_files = ['config/locales', 'plugins/**/locales'] .product(['server.en.yml', 'client.en.yml']) .collect { |dir, filename| Dir["#{Rails.root}/#{dir}/#{filename}"] } @@ -85,12 +85,42 @@ describe "i18n integrity checks" do end end + module Pluralizations + def self.load(path) + whitelist = Regexp.union([/messages.restrict_dependent_destroy/]) + + yaml = YAML.load_file("#{Rails.root}/#{path}") + pluralizations = find_pluralizations(yaml['en']) + pluralizations.reject! { |key| key.match(whitelist) } + pluralizations + end + + def self.find_pluralizations(hash, parent_key = '', pluralizations = Hash.new) + hash.each do |key, value| + if value.is_a? Hash + current_key = parent_key.blank? ? key : "#{parent_key}.#{key}" + find_pluralizations(value, current_key, pluralizations) + elsif key == 'one' || key == 'other' + pluralizations[parent_key] = hash + end + end + + pluralizations + end + end + locale_files.each do |path| context path do it 'has no duplicate keys' do duplicates = DuplicateKeyFinder.new.find_duplicates("#{Rails.root}/#{path}") expect(duplicates).to be_empty end + + Pluralizations.load(path).each do |key, values| + it "key '#{key}' has valid pluralizations" do + expect(values.keys).to contain_exactly('one', 'other') + end + end end end end