air18n 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,7 +8,13 @@ module Air18n
8
8
  class Backend
9
9
  include I18n::Backend::Base
10
10
 
11
- attr_accessor :translation_data, :initialized, :phrase_screenshots
11
+ # - translation_data: map of locale to key to translation
12
+ # - phrase_screenshots: map of phrase key to the routes context that we
13
+ # have screenshots for
14
+ # - default_text_change_observer: set this to an implementation of
15
+ # DefaultTextChangeObserver to be alerted to changes of default text, and
16
+ # be able to guard against flipflops.
17
+ attr_accessor :translation_data, :phrase_screenshots, :default_text_change_observer
12
18
 
13
19
  # Stores translations for a given locale.
14
20
  def store_translations locale, data, options = {}
@@ -157,27 +163,34 @@ module Air18n
157
163
  if locale == I18n.default_locale && default && result != default
158
164
  # If it doesn't, we need to update the 'phrases' table so that
159
165
  # the 'value' column reflects the latest English default text.
166
+ store_default = true
160
167
  if result.present?
161
- LoggingHelper.info "Default #{I18n.default_locale} text for key '#{key}' changed! Old default '#{result}' != new default '#{default}'. Route context: #{options[:routes_context]}"
168
+ LoggingHelper.info "Default #{locale} text for key '#{key}' changed! Old default '#{result}' != new default '#{default}'. Route context: #{options[:routes_context]}"
169
+ if @default_text_change_observer.present?
170
+ @default_text_change_observer.default_text_changed(locale, key, result, default)
171
+ store_default = @default_text_change_observer.allow_default_text_change?(locale, key, result, default)
172
+ end
162
173
  end
163
- phrase = Phrase.find_or_create_by_key(key)
164
- if phrase
165
- if (overrides_previous_default && phrase.value != default) ||
166
- (!overrides_previous_default && phrase.value != default && phrase.value.blank?)
167
- phrase.value = default
168
-
169
- begin
170
- phrase.save
171
- rescue Exception => e
172
- # If many requests happen simultaneously for a page with a new
173
- # phrase, a "duplicate entry" exception will happen naturally.
174
- # And if phrase creation fails for some other reason, we will try
175
- # again to create it next time automatically.
174
+ if store_default
175
+ phrase = Phrase.find_or_create_by_key(key)
176
+ if phrase
177
+ if (overrides_previous_default && phrase.value != default) ||
178
+ (!overrides_previous_default && phrase.value != default && phrase.value.blank?)
179
+ phrase.value = default
180
+
181
+ begin
182
+ phrase.save
183
+ rescue Exception => e
184
+ # If many requests happen simultaneously for a page with a new
185
+ # phrase, a "duplicate entry" exception will happen naturally.
186
+ # And if phrase creation fails for some other reason, we will try
187
+ # again to create it next time automatically.
188
+ end
176
189
  end
177
190
  end
178
- end
179
191
 
180
- @translation_data[locale][key] = default
192
+ @translation_data[locale][key] = default
193
+ end
181
194
  result = default
182
195
  end
183
196
 
@@ -178,6 +178,10 @@ module Air18n
178
178
  @air18n_backend.rescreenshot(routes_context)
179
179
  end
180
180
 
181
+ def set_default_text_change_observer(observer)
182
+ @air18n_backend.default_text_change_observer = observer
183
+ end
184
+
181
185
  def currency=(c)
182
186
  @currency = c
183
187
  @distance_unit = self::MILES_CURRENCIES.include?(c) ? "MI" : "KM"
@@ -1,3 +1,3 @@
1
1
  module Air18n
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
data/lib/air18n.rb CHANGED
@@ -5,6 +5,7 @@ require 'active_record'
5
5
  require 'air18n/class_methods'
6
6
  require 'air18n/backend'
7
7
  require 'air18n/priority'
8
+ require 'air18n/default_text_change_observer'
8
9
 
9
10
  require 'air18n/reflection'
10
11
 
data/make_gem CHANGED
@@ -1,5 +1,5 @@
1
1
  # Run this to push the new version of the gem to rubygems!
2
- v=0.1.3
2
+ v=0.1.4
3
3
  gem build air18n.gemspec
4
4
  gem push air18n-${v}.gem
5
5
 
@@ -4,6 +4,19 @@ require 'air18n/mock_priority'
4
4
 
5
5
  describe Air18n::Backend do
6
6
 
7
+ class PessimisticRecordingDefaultTextChangeObserver
8
+ extend Air18n::DefaultTextChangeObserver
9
+ attr_accessor :changes
10
+ def default_text_changed(locale, key, old_default, new_default)
11
+ @changes ||= []
12
+ @changes << [locale, key, old_default, new_default]
13
+ end
14
+
15
+ def allow_default_text_change?(locale, key, old_default, new_default)
16
+ false
17
+ end
18
+ end
19
+
7
20
  before do
8
21
  @scope = []
9
22
  end
@@ -31,6 +44,16 @@ describe Air18n::Backend do
31
44
  @backend.lookup(I18n.default_locale, phrase.key, @scope, :default => 'new_value', :default_is_low_priority => true).should == 'new_value'
32
45
  phrase.reload.value.should == 'original_value'
33
46
  end
47
+ it 'Should not override DB value when default text observer says not to' do
48
+ phrase = FactoryGirl.create(:phrase, :key => 'default text change, key 1', :value => 'original_value')
49
+ @backend.init_translations(I18n.default_locale)
50
+ default_text_change_observer = PessimisticRecordingDefaultTextChangeObserver.new
51
+ @backend.default_text_change_observer = default_text_change_observer
52
+ @backend.lookup(I18n.default_locale, phrase.key, @scope, :default => 'new_value').should == 'new_value'
53
+
54
+ default_text_change_observer.changes.should == [[:en, 'default text change, key 1', 'original_value', 'new_value']]
55
+ phrase.reload.value.should == 'original_value'
56
+ end
34
57
  end
35
58
 
36
59
  context 'Robustness to various input formats' do
@@ -30,9 +30,6 @@ describe Air18n::PhraseTranslation do
30
30
  translation.value = 'too looooong'
31
31
  translation.save.should be_false
32
32
  translation.errors.should == { :value => ["Translation has 12 characters; maximum length 10 characters."] }
33
-
34
- translation = FactoryGirl.create(:phrase_translation, :key => 'facebook description maxlength:0', :value => 'short')
35
- validated = translation.save
36
33
  end
37
34
  end
38
35
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: air18n
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: