Browse files

Merge remote branch 'wvanbergen/master' into rails3

  • Loading branch information...
2 parents 22f34f5 + 7d7ed74 commit 863399b7df4a396549344592022662d0b15a9c5c @lifo lifo committed Jun 8, 2010
Showing with 49 additions and 0 deletions.
  1. +27 −0 app/models/tolk/translation.rb
  2. +5 −0 test/locales/formats/en.yml
  3. +17 −0 test/unit/format_test.rb
View
27 app/models/tolk/translation.rb
@@ -6,6 +6,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
@@ -51,6 +52,23 @@ def value
end
end
+ def self.detect_variables(search_in)
+ case search_in
+ when String then Set.new(search_in.scan(/\{\{(\w+)\}\}/).flatten + 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
@@ -87,5 +105,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
View
5 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:
View
17 test/unit/format_test.rb
@@ -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

0 comments on commit 863399b

Please sign in to comment.