Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add interpolation variable matcher #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/i18n-spec/matchers/have_interpolation_keys_of_matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
RSpec::Matchers.define :have_interpolation_keys_of do |default_locale_filepath|
extend I18nSpec::FailureMessage

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 = default_translations.keys.reject do |key|
default_value = default_translations[key]
test_value = test_translations[key]

test_value.nil? ||
I18nSpec::Parse.for_interpolation_variables(default_value) == I18nSpec::Parse.for_interpolation_variables(test_value)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this feature, I am declaring it a pass ✅ when:

A) the test locale string completely is empty / blank
OR
B) the test locale & the default locale have exactly the same interpolation variables

Is this ok?

end

@superset.empty?
end

failure_for_should do |filepath|
"Expected #{filepath} interpolation variables to match #{default_locale_filepath}. \nProblems in:\n- " << @superset.join("\n- ")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed the pattern in another matcher file for failure_for_should

end
end
22 changes: 22 additions & 0 deletions lib/i18n-spec/models/locale_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def flattened_translations
@flattened_translations ||= flatten_tree(translations.values.first)
end

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

def locale
@locale ||= ISO::Tag.new(locale_code)
end
Expand Down Expand Up @@ -110,6 +114,24 @@ def pluralization_data?(data)
keys.any? {|k| PLURALIZATION_KEYS.include?(k) }
end

def build_interpolations_only_hash(data, prefix = '', result = {})
data.each_pair do |key, value|
current_prefix = prefix.empty? ? key.to_s : "#{prefix}.#{key}"
if !value.is_a? Hash
result[current_prefix] = value #if contains_variables?(value)
else
result = build_interpolations_only_hash(value, current_prefix, result)
end
end

result
end

def contains_variables?(str)
return false if str.nil?
!I18nSpec::Parse.for_interpolation_variables(str).empty?
end

def yaml_load_content
if defined?(Psych) and defined?(Psych::VERSION)
Psych.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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a Parse class because this method didn't quite feel like it belonged in the LocaleFile class.
Let me know what you think


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

end
end
4 changes: 3 additions & 1 deletion spec/fixtures/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ en:
itteh:
bitteh:
ceiling:
kitteh: 'is watching you'
kitteh: 'is watching you'
group:
joined: "%{user_name} successfully joined %{group}"
4 changes: 3 additions & 1 deletion spec/fixtures/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ es:
itteh:
bitteh:
ceiling:
kitteh: 'te mira'
kitteh: 'te mira'
group:
joined: "%{user_name} unido correctamente %{group}"
4 changes: 3 additions & 1 deletion spec/fixtures/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ en:
itteh:
bitteh:
ceiling:
kitteh: 'te regarde'
kitteh: 'te regarde'
group:
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}"
4 changes: 4 additions & 0 deletions spec/lib/i18n-spec/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@
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/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

describe "Translated files" do
describe 'spec/fixtures/fr.yml' do
it { is_expected.to be_a_subset_of 'spec/fixtures/en.yml' }
it { is_expected.to have_interpolation_keys_of 'spec/fixtures/en.yml' }
it { is_expected.to be_a_complete_translation_of 'spec/fixtures/en.yml' }
end

describe 'spec/fixtures/es.yml' do
it { is_expected.to be_a_subset_of 'spec/fixtures/en.yml' }
it { is_expected.to have_interpolation_keys_of 'spec/fixtures/en.yml' }
it { is_expected.not_to be_a_complete_translation_of 'spec/fixtures/en.yml'}
end
end
6 changes: 3 additions & 3 deletions spec/lib/i18n-spec/models/locale_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def locale_file_with_content(content)
end

describe "#missing_pluralization_keys" do
it "returns the parents that containts missing pluralizations in with the english rules" do
it "returns the parents that contains missing pluralizations in with the english rules" do
content = "en:
cats:
one: one
Expand All @@ -123,7 +123,7 @@ def locale_file_with_content(content)
expect(locale_file.errors[:missing_pluralization_keys]).not_to be_nil
end

it "returns the parents that containts missing pluralizations in with the russian rules" do
it "returns the parents that contains missing pluralizations in with the russian rules" do
content = "ru:
cats:
one: one
Expand All @@ -146,7 +146,7 @@ def locale_file_with_content(content)
expect(locale_file.errors[:missing_pluralization_keys]).not_to be_nil
end

it "returns the parents that containts missing pluralizations in with the japanese rules" do
it "returns the parents that contains missing pluralizations in with the japanese rules" do
content = "ja:
cats:
one: one
Expand Down