Skip to content

Commit

Permalink
Revised sync to handle numeric translations correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Davey committed Apr 23, 2010
1 parent d6618ca commit ae6a64c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
8 changes: 8 additions & 0 deletions app/models/tolk/translation.rb
Expand Up @@ -18,6 +18,14 @@ class Translation < ActiveRecord::Base
attr_accessor :primary
before_validation :fix_text_type, :unless => proc {|r| r.primary }

def up_to_date?
not out_of_date?
end

def out_of_date?
primary_updated?
end

def primary_translation
@_primary_translation ||= begin
if locale && !locale.primary?
Expand Down
21 changes: 8 additions & 13 deletions lib/tolk/sync.rb
Expand Up @@ -49,20 +49,16 @@ def sync_phrases(translations)
# Create phrase and primary translation if missing
existing_phrase = phrases.detect {|p| p.key == key} || Tolk::Phrase.create!(:key => key)
translation = existing_phrase.translations.primary || primary_locale.translations.build(:phrase_id => existing_phrase.id)

# Update primary translation if it's been changed
if value && translation.text != value
unless translation.new_record?
# Set the primary updated flag if the translation is not new
secondary_locales.each do |locale|
if existing_translation = existing_phrase.translations.detect {|t| t.locale_id == locale.id }
existing_translation.force_set_primary_update = true
existing_translation.save!
end
translation.text = value

if translation.changed? && !translation.new_record?
# Set the primary updated flag if the primary translation has changed and it is not a new record.
secondary_locales.each do |locale|
if existing_translation = existing_phrase.translations.detect {|t| t.locale_id == locale.id }
existing_translation.force_set_primary_update = true
existing_translation.save!
end
end

translation.text = value
end

translation.primary = true
Expand All @@ -74,6 +70,5 @@ def filter_out_i18n_keys(flat_hash)
flat_hash.reject { |key, value| key.starts_with? "i18n" }
end
end

end
end
28 changes: 28 additions & 0 deletions test/unit/sync_test.rb
Expand Up @@ -57,6 +57,34 @@ def test_sync_sets_primary_updated_for_secondary_translations_on_update
assert t1.primary_updated?
assert ! t2.primary_updated?
end

def test_sync_marks_translations_for_review_when_the_primary_translation_has_changed
Tolk::Locale.create!(:name => 'es')

phrase = Tolk::Phrase.create! :key => 'number.precision'
english_translation = phrase.translations.create!(:text => "1", :locale => Tolk::Locale.find_by_name("en"))
spanish_translation = phrase.translations.create!(:text => "1", :locale => Tolk::Locale.find_by_name("es"))

Tolk::Locale.expects(:load_translations).returns({'number.precision' => "1"})
Tolk::Locale.sync! and spanish_translation.reload
assert spanish_translation.up_to_date?

Tolk::Locale.expects(:load_translations).returns({'number.precision' => "2"})
Tolk::Locale.sync! and spanish_translation.reload
assert spanish_translation.out_of_date?

spanish_translation.text = "2"
spanish_translation.save! and spanish_translation.reload
assert spanish_translation.up_to_date?

Tolk::Locale.expects(:load_translations).returns({'number.precision' => 2})
Tolk::Locale.sync! and spanish_translation.reload
assert spanish_translation.up_to_date?

Tolk::Locale.expects(:load_translations).returns({'number.precision' => 1})
Tolk::Locale.sync! and spanish_translation.reload
assert spanish_translation.out_of_date?
end

def test_sync_creates_locale_phrases_translations
Tolk::Locale.sync!
Expand Down

0 comments on commit ae6a64c

Please sign in to comment.