Skip to content

Commit

Permalink
Added simple variable checking when validating translations.
Browse files Browse the repository at this point in the history
  • Loading branch information
wvanbergen authored and josh committed Jun 14, 2010
1 parent 08dc20b commit 0b75ce7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app/models/tolk/translation.rb
Expand Up @@ -8,6 +8,7 @@ class Translation < ActiveRecord::Base

serialize :text
validates_presence_of :text, :if => proc {|r| r.primary.blank? && !r.explicit_nil }
validate :check_matching_variables, :if => proc { |tr| tr.primary_translation.present? }

validates_uniqueness_of :phrase_id, :scope => :locale_id

Expand Down Expand Up @@ -53,6 +54,23 @@ def value
end
end

def self.detect_variables(search_in)
case search_in
when String then Set.new(search_in.scan(/\{\{(\w+)\}\}/).flatten)
when Array then search_in.inject(Set[]) { |carry, item| carry + detect_variables(item) }
when Hash then search_in.values.inject(Set[]) { |carry, item| carry + detect_variables(item) }
else Set[]
end
end

def variables
self.class.detect_variables(text)
end

def variables_match?
self.variables == primary_translation.variables
end

private

def set_explicit_nil
Expand Down Expand Up @@ -89,5 +107,14 @@ def set_previous_text
true
end

def check_matching_variables
unless variables_match?
if primary_translation.variables.empty?
self.errors.add(:text, "The original does not contain variables, so they should not be included.")
else
self.errors.add(:text, "The translation should contain the variables #{variables.to_a.to_sentence}.")
end
end
end
end
end
5 changes: 5 additions & 0 deletions test/locales/formats/en.yml
@@ -1,6 +1,11 @@
en:
number: 1
string: I am just a stupid string :(
variables: "String with variables {{hello}} and {{world}}"
variables_in_struct:
zero: "Without variable"
one: "With {{variables}}"
other: "With even {{more}} {{variables}}"
number_array: [1, 2, 3]
string_array: ['sun', 'moon']
pluralization:
Expand Down
17 changes: 17 additions & 0 deletions test/unit/format_test.rb
Expand Up @@ -60,6 +60,23 @@ def test_creating_translations_fails_on_mismatch_with_primary_translation
success.save!
assert_equal [1, 2], success.text
end

def test_creating_translations_fails_with_unmatching_variables
# Check that variable detection works correctly
assert_equal Set['hello', 'world'], ph('variables').translations.primary.variables
assert_equal Set['more', 'variables'], ph('variables_in_struct').translations.primary.variables

# Allow different ordering and multiple occurences of variables
assert @spanish.translations.build(:text => '{{world}} y {{hello}} y {{hello}} y {{world}}', :phrase => ph('variables')).valid?

# Do not allow missing or wrong variables
assert_raises(ActiveRecord::RecordInvalid) { @spanish.translations.create!(:text => 'Hola', :phrase => ph('variables')) }
assert_raises(ActiveRecord::RecordInvalid) { @spanish.translations.create!(:text => '{{other}} variable', :phrase => ph('variables')) }

# Do not allow variables if the origin does not contain any
assert_equal Set[], ph('string').translations.primary.variables
assert_raises(ActiveRecord::RecordInvalid) { @spanish.translations.create!(:text => 'Hola {{mundo}}', :phrase => ph('string')) }
end

def test_creating_translations_with_nil_values
# implicit nil value
Expand Down

0 comments on commit 0b75ce7

Please sign in to comment.