Skip to content

Commit

Permalink
tidyup
Browse files Browse the repository at this point in the history
  • Loading branch information
mixmix committed Aug 30, 2014
1 parent 5670c8c commit 0e9ca6b
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 27 deletions.
16 changes: 8 additions & 8 deletions lib/i18n-spec/matchers/have_interpolation_keys_of_matcher.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
RSpec::Matchers.define :have_interpolation_keys_of do |default_locale_filepath|
extend I18nSpec::FailureMessage

match do |filepath|
locale_file = I18nSpec::LocaleFile.new(filepath)
default_locale = I18nSpec::LocaleFile.new(default_locale_filepath)
match do |test_locale_filepath|
default_translations = I18nSpec::LocaleFile.new(default_locale_filepath).flat_interpolations_only_hash
test_translations = I18nSpec::LocaleFile.new(test_locale_filepath).flat_interpolations_only_hash

@superset = locale_file.flattened_and_interpolation_only.keys.reject do |key|
@superset = default_translations.keys.reject do |key|
default_value = default_translations[key]
test_value = test_translations[key]

default_vars = default_locale.flattened_and_interpolation_only[key]
locale_vars = locale_file.flattened_and_interpolation_only[key]

(locale_vars == default_vars) || locale_vars.empty?
test_value.nil? ||
I18nSpec::Parse.for_interpolation_variables(default_value) == I18nSpec::Parse.for_interpolation_variables(test_value)
end

@superset.empty?
Expand Down
26 changes: 9 additions & 17 deletions lib/i18n-spec/models/locale_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def flattened_translations
@flattened_translations ||= flatten_tree(translations.values.first)
end

def flattened_and_interpolation_only
@flattened_and_interpolation_only ||= build_interpolations_only_tree(translations.values.first)
def flat_interpolations_only_hash
@flat_interpolations_only_hash ||= build_interpolations_only_hash(translations.values.first)
end

def locale
Expand Down Expand Up @@ -114,30 +114,22 @@ def pluralization_data?(data)
keys.any? {|k| PLURALIZATION_KEYS.include?(k) }
end

def build_interpolations_only_tree(data, new_tree = {}, key_trace = '')
def build_interpolations_only_hash(data, prefix = '', result = {})
data.each_pair do |key, value|
extended_key_trace = key_trace + key

if value.is_a? Hash
new_tree = build_interpolations_only_tree(value, new_tree, extended_key_trace + '.')
current_prefix = prefix.empty? ? key.to_s : "#{prefix}.#{key}"
if !value.is_a? Hash
result[current_prefix] = value #if contains_variables?(value)
else
new_tree[extended_key_trace] = parse_for_variables(value) if contains_variables?(value)
result = build_interpolations_only_hash(value, current_prefix, result)
end
end

new_tree
result
end

def contains_variables?(str)
return false if str.nil?

!parse_for_variables(str).empty?
end

def parse_for_variables(str)
return nil if str.nil?

str.scan(/%{[^%{}]*}/)
!I18nSpec::Parse.for_interpolation_variables(str).empty?
end

def yaml_load_content
Expand Down
10 changes: 10 additions & 0 deletions lib/i18n-spec/models/parse.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module I18nSpec
class Parse

def self.for_interpolation_variables(str)
return nil if str.nil?
str.scan(/%{[^%{}]*}/)
end

end
end
2 changes: 1 addition & 1 deletion spec/fixtures/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ en:
ceiling:
kitteh: 'te regarde'
group:
joined: # deliberatley empty
joined: # deliberatley empty translation for interpolation variable checker
11 changes: 11 additions & 0 deletions spec/fixtures/interpolation_missing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
es:
cats:
zero: 'no gatos'
one: 'un gato'
other: 'gatos'
itteh:
bitteh:
ceiling:
kitteh: 'te mira'
group:
joined: "(interpolation should var should be here) unido correctamente"
11 changes: 11 additions & 0 deletions spec/fixtures/interpolation_misspelt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
es:
cats:
zero: 'no gatos'
one: 'un gato'
other: '%{count } gatos'
itteh:
bitteh:
ceiling:
kitteh: 'te mira'
group:
joined: "%{user_name} unido correctamente %{groupo}"
3 changes: 2 additions & 1 deletion spec/lib/i18n-spec/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
it { expect('spec/fixtures/legacy_interpolations.yml').to have_legacy_interpolations }
it { expect('spec/fixtures/invalid_locale.yml').not_to have_a_valid_locale }
it { expect('spec/fixtures/not_subset.yml').not_to be_a_subset_of 'spec/fixtures/en.yml' }
it { expect('spec/fixtures/broken_interpolation_keys.yml').not_to have_interpolation_keys_of 'spec/fixtures/en.yml' }
it { expect('spec/fixtures/interpolation_misspelt.yml').not_to have_interpolation_keys_of 'spec/fixtures/en.yml' }
it { expect('spec/fixtures/interpolation_missing.yml').not_to have_interpolation_keys_of 'spec/fixtures/en.yml' }
it { expect('spec/fixtures/missing_pluralization_keys.yml').to have_missing_pluralization_keys }
end

Expand Down

0 comments on commit 0e9ca6b

Please sign in to comment.