i18n_backend_database 0.0.1

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.
Files changed (67) hide show
  1. data/LICENSE +29 -0
  2. data/README.textile +125 -0
  3. data/data/locales.yml +4 -0
  4. data/generators/i18n_backend_database/i18n_backend_database_generator.rb +8 -0
  5. data/generators/i18n_backend_database/templates/migrate/create_i18n_tables.rb +25 -0
  6. data/init.rb +1 -0
  7. data/lib/controllers/locales_controller.rb +86 -0
  8. data/lib/controllers/translations_controller.rb +141 -0
  9. data/lib/ext/i18n.rb +68 -0
  10. data/lib/google_language.rb +33 -0
  11. data/lib/i18n_backend_database.rb +9 -0
  12. data/lib/i18n_backend_database/database.rb +263 -0
  13. data/lib/i18n_util.rb +148 -0
  14. data/lib/models/locale.rb +67 -0
  15. data/lib/models/translation.rb +46 -0
  16. data/lib/models/translation_option.rb +26 -0
  17. data/lib/public/images/custom1_bar.gif +0 -0
  18. data/lib/public/images/custom1_box.gif +0 -0
  19. data/lib/public/images/percentImage.png +0 -0
  20. data/lib/public/images/percentImage_back.png +0 -0
  21. data/lib/public/images/percentImage_back1.png +0 -0
  22. data/lib/public/images/percentImage_back2.png +0 -0
  23. data/lib/public/images/percentImage_back3.png +0 -0
  24. data/lib/public/images/percentImage_back4.png +0 -0
  25. data/lib/public/javascripts/jsProgressBarHandler.js +509 -0
  26. data/lib/routing.rb +15 -0
  27. data/lib/views/layouts/translations.html.erb +23 -0
  28. data/lib/views/locales/edit.html.erb +20 -0
  29. data/lib/views/locales/index.html.erb +22 -0
  30. data/lib/views/locales/new.html.erb +19 -0
  31. data/lib/views/locales/show.html.erb +14 -0
  32. data/lib/views/translations/asset_translations.html.erb +32 -0
  33. data/lib/views/translations/edit.html.erb +35 -0
  34. data/lib/views/translations/index.html.erb +27 -0
  35. data/lib/views/translations/new.html.erb +19 -0
  36. data/lib/views/translations/show.html.erb +32 -0
  37. data/lib/views/translations/translations.html.erb +28 -0
  38. data/lib/views/translations/update.rjs +7 -0
  39. data/routes.rb +3 -0
  40. data/spec/assets/public/es/favicons/favicon1.gif +0 -0
  41. data/spec/assets/public/es/images/icons/icon1.gif +0 -0
  42. data/spec/assets/public/es/images/image1.gif +0 -0
  43. data/spec/assets/public/favicons/favicon1.gif +0 -0
  44. data/spec/assets/public/favicons/favicon2.gif +0 -0
  45. data/spec/assets/public/images/icons/icon1.gif +0 -0
  46. data/spec/assets/public/images/icons/icon2.gif +0 -0
  47. data/spec/assets/public/images/image1.gif +0 -0
  48. data/spec/assets/public/images/image2.gif +0 -0
  49. data/spec/assets/public/images/promo/sfc08_140x400_3.gif +0 -0
  50. data/spec/assets/views/test_view1.html.erb +1 -0
  51. data/spec/assets/views/test_view2.html.erb +30 -0
  52. data/spec/caching_spec.rb +87 -0
  53. data/spec/controllers/locales_controller_spec.rb +173 -0
  54. data/spec/controllers/locales_routing_spec.rb +59 -0
  55. data/spec/controllers/translations_controller_spec.rb +189 -0
  56. data/spec/controllers/translations_routing_spec.rb +59 -0
  57. data/spec/database_spec.rb +199 -0
  58. data/spec/i18n_ext_spec.rb +40 -0
  59. data/spec/localize_spec.rb +66 -0
  60. data/spec/localize_text_spec.rb +76 -0
  61. data/spec/models/locale_spec.rb +111 -0
  62. data/spec/models/translation_spec.rb +44 -0
  63. data/spec/spec_helper.rb +16 -0
  64. data/spec/translate_asset_spec.rb +57 -0
  65. data/spec/translate_spec.rb +546 -0
  66. data/tasks/i18n.rake +60 -0
  67. metadata +155 -0
@@ -0,0 +1,44 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Translation do
4
+ before(:each) do
5
+ @valid_attributes = {
6
+ :key => "Hello World",
7
+ :value => "Hello World"
8
+ }
9
+ end
10
+
11
+ it "should create a new instance given valid attributes" do
12
+ Translation.create!(@valid_attributes)
13
+ end
14
+ end
15
+
16
+ describe "English and Spanish Locales with I18n default locale set to English" do
17
+ before(:each) do
18
+ I18n.default_locale = "en"
19
+ @english_locale = Locale.create!(:code => "en")
20
+ @spanish_locale = Locale.create!(:code => "es")
21
+ end
22
+
23
+ describe "with no English translation" do
24
+ it "should have return 'No default locale value' for a new Spanish translation with same key" do
25
+ @spanish_translation = @spanish_locale.translations.create(:key => 'Hello World')
26
+ @spanish_translation.default_locale_value.should == 'No default locale value'
27
+ end
28
+ end
29
+
30
+ describe "with one English translation" do
31
+ before(:each) do
32
+ @english_translation = @english_locale.translations.create(:key => 'Hello World', :value => 'Hello World')
33
+ end
34
+
35
+ it "should have a default locale value" do
36
+ @english_translation.default_locale_value.should == 'Hello World'
37
+ end
38
+
39
+ it "should have a default locale value of the English for a new Spanish translation with same key" do
40
+ @spanish_translation = @spanish_locale.translations.create(:key => 'Hello World')
41
+ @spanish_translation.default_locale_value.should == 'Hello World'
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,16 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+ require File.join(File.dirname(__FILE__), "/../../../../config/environment")
3
+ require 'spec/rails'
4
+ require 'i18n_backend_database'
5
+
6
+ Spec::Runner.configure do |config|
7
+ config.use_transactional_fixtures = true
8
+ config.use_instantiated_fixtures = false
9
+
10
+ config.after(:each) do
11
+ Locale.reset_default_locale
12
+ I18n.locale = "en"
13
+ I18n.default_locale = "en"
14
+ I18n.backend.cache_store.clear
15
+ end
16
+ end
@@ -0,0 +1,57 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe I18n do
4
+
5
+ describe "with default locale en" do
6
+ before(:each) do
7
+ I18n.default_locale = "en"
8
+ ActionView::Helpers::AssetTagHelper.send(:remove_const, :ASSETS_DIR)
9
+ ActionView::Helpers::AssetTagHelper::ASSETS_DIR = "#{RAILS_ROOT}/vendor/plugins/i18n_backend_database/spec/assets/public"
10
+ I18n.send(:remove_const, :APP_DIRECTORY)
11
+ I18n::APP_DIRECTORY = "./vendor/plugins/i18n_backend_database/spec/assets"
12
+ end
13
+
14
+ it "should find my test views" do
15
+ I18n.asset_translations.should == ["promo/sfc08_140x400_3.gif", "image1.gif", "image2.gif", "icons/icon1.gif", "icons/icon2.gif",
16
+ "/favicons/favicon1.gif", "/favicons/favicon2.gif"]
17
+ end
18
+
19
+ it "should return no untranslated assets" do
20
+ I18n.untranslated_assets(:en).should be_empty
21
+ end
22
+
23
+ it "should return untranslated assets" do
24
+ I18n.untranslated_assets(:es).should == ["promo/sfc08_140x400_3.gif", "image2.gif", "icons/icon2.gif", "/favicons/favicon2.gif"]
25
+ end
26
+
27
+ describe "and locale en" do
28
+ before(:each) do
29
+ I18n.locale = "en"
30
+ end
31
+
32
+ it "should return default asset path" do
33
+ I18n.ta("image.gif").should == 'image.gif'
34
+ end
35
+
36
+ it "should return translated asset path for locale passed" do
37
+ I18n.ta("image1.gif", :locale => :es).should == '/es/images/image1.gif'
38
+ end
39
+ end
40
+
41
+ describe "and locale es" do
42
+ before(:each) do
43
+ I18n.locale = "es"
44
+ end
45
+
46
+ it "should return default asset path if no translated asset exists" do
47
+ I18n.ta("image2.gif").should == 'image2.gif'
48
+ end
49
+
50
+ it "should return translated asset path if translatied asset exists" do
51
+ I18n.ta("image1.gif").should == '/es/images/image1.gif'
52
+ end
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,546 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ class MemoryStoreProxy < ActiveSupport::Cache::MemoryStore
4
+ attr_accessor :write_count
5
+
6
+ def initialize
7
+ @write_count = 0
8
+ super
9
+ end
10
+
11
+ def write(key, value, options = nil)
12
+ super(key, value, options)
13
+ @write_count += 1
14
+ end
15
+ end
16
+
17
+ describe I18n::Backend::Database do
18
+ before(:each) do
19
+ @backend = I18n::Backend::Database.new
20
+ end
21
+
22
+ after(:each) do
23
+ @backend.cache_store.clear
24
+ end
25
+
26
+ describe "returning hashes for non-leaves" do
27
+ before do
28
+ #Set up a translation hierarchy
29
+ @locale = Locale.create!(:code => "en")
30
+ I18n.default_locale = "en"
31
+ end
32
+
33
+ it "should return a hash when asked for a non-leaf node" do
34
+ format_hash = {:separator => ".", :precision => 3, :delimiter => ","}
35
+ @locale.translations.create!(:key => "number.format.separator", :value => format_hash[:separator])
36
+ @locale.translations.create!(:key => "number.format.precision", :value => format_hash[:precision])
37
+ @locale.translations.create!(:key => "number.format.delimiter", :value => format_hash[:delimiter])
38
+ @backend.translate("en", "number.format").should == format_hash
39
+ end
40
+
41
+ it "should return a nested hash" do
42
+ nested_hash = {:a => "b", :c => {:d => "e", :f => "g"}}
43
+ @locale.translations.create!(:key => "nested.a", :value => "b")
44
+ @locale.translations.create!(:key => "nested.c.d", :value => "e")
45
+ @locale.translations.create!(:key => "nested.c.f", :value => "g")
46
+ @backend.translate("en", "nested").should == nested_hash
47
+ end
48
+
49
+ it "should not be fooled by SQL injection" do
50
+ #for bad_string in ["number.format%", "number.format\%", "number.format\\"] do
51
+ for bad_string in ["number.format\\"] do
52
+ @backend.translate("en", bad_string).should == bad_string
53
+ end
54
+ end
55
+
56
+ it "should not be fooled by keys ending in a delimiter" do
57
+ @locale.translations.create!(:key => "number.format.", :value => 'this should be a normal leaf')
58
+ @backend.translate('en', 'number.format.').should == 'this should be a normal leaf'
59
+ end
60
+
61
+ end
62
+
63
+ describe "with default locale en" do
64
+ before(:each) do
65
+ I18n.default_locale = "en"
66
+ @english_locale = Locale.create!(:code => "en")
67
+ end
68
+
69
+ describe "and locale en" do
70
+ before(:each) do
71
+ I18n.locale = "en"
72
+ end
73
+
74
+ it "should create a record with the key as the value when the key is a string" do
75
+ @backend.translate("en", "String").should == "String"
76
+ @english_locale.should have(1).translation
77
+ @english_locale.translations.first.key.should == Translation.hk("String")
78
+ @english_locale.translations.first.raw_key.should == "String"
79
+ @english_locale.translations.first.value.should == "String"
80
+ end
81
+
82
+ it "should not write to the cache if the key already exists in the cache" do
83
+ @backend.cache_store = MemoryStoreProxy.new
84
+ @english_locale.translations.create!(:key => 'String', :value => 'Value')
85
+
86
+ @backend.translate("en", "String").should == "Value"
87
+ @backend.cache_store.write_count.should == 1
88
+
89
+ @backend.translate("en", "String").should == "Value"
90
+ @backend.cache_store.write_count.should == 1
91
+ end
92
+
93
+ it "should create a record with the key as the value when the key is a string and cache that record" do
94
+ @backend.translate("en", "status").should == "status"
95
+ @english_locale.should have(1).translation
96
+
97
+ # once cached
98
+ @backend.translate("en", "status").should == "status"
99
+ @english_locale.should have(1).translation
100
+ end
101
+
102
+ it "should find a record with the key as the value when the key is a string" do
103
+ @english_locale.translations.create!(:key => 'String', :value => 'Value')
104
+ @backend.translate("en", "String").should == "Value"
105
+ @english_locale.should have(1).translation
106
+ end
107
+
108
+ it "should support having a record with a nil value" do
109
+ @english_locale.translations.create!(:key => 'date.order')
110
+ @backend.translate("en", :'date.order').should be_nil
111
+ @english_locale.should have(1).translation
112
+ end
113
+
114
+ it "should create a record with a nil value when key is a symbol" do
115
+ @backend.translate("en", :'date.order').should be_nil
116
+ @english_locale.should have(1).translation
117
+ @english_locale.translations.first.key.should == Translation.hk('date.order')
118
+ @english_locale.translations.first.raw_key.should == "date.order"
119
+ @english_locale.translations.first.value.should be_nil
120
+
121
+ # once cached
122
+ @backend.translate("en", :'date.order').should be_nil
123
+ @english_locale.reload.should have(1).translation
124
+ end
125
+
126
+ it "should support storing values as YAML symbols" do
127
+ @english_locale.translations.create!(:key => 'date.order', :value => "--- :year\n", :pluralization_index => 0)
128
+ @english_locale.translations.create!(:key => 'date.order', :value => "--- :month\n", :pluralization_index => 1)
129
+ @english_locale.translations.create!(:key => 'date.order', :value => "--- :day\n", :pluralization_index => 2)
130
+
131
+ @backend.translate("en", :'date.order').should == [:year, :month, :day]
132
+ @english_locale.should have(3).translations
133
+ end
134
+
135
+ it "should find a cached record from a cache key if it exists in the cache" do
136
+ hash_key = Translation.hk("blah")
137
+ @backend.cache_store.write("en:#{hash_key}", 'woot')
138
+ @backend.translate("en", "blah").should == "woot"
139
+ end
140
+
141
+ it "should find a cached record with a nil value from a cache key if it exists in the cache" do
142
+ hash_key = Translation.hk(".date.order")
143
+ @backend.cache_store.write("en:#{hash_key}", nil)
144
+ @backend.translate("en", :'date.order').should be_nil
145
+ end
146
+
147
+ it "should write a cache record to the cache for a newly created translation record" do
148
+ hash_key = Translation.hk("blah")
149
+ @backend.translate("en", "blah")
150
+ @backend.cache_store.read("en:#{hash_key}").should == "blah"
151
+ end
152
+
153
+ it "should write a cache record to the cache for translation record with nil value" do
154
+ @english_locale.translations.create!(:key => '.date.order')
155
+ @backend.translate("en", :'date.order').should be_nil
156
+
157
+ hash_key = Translation.hk(".date.order")
158
+ @backend.cache_store.read("en:#{hash_key}").should be_nil
159
+ end
160
+
161
+ it "should handle active record helper defaults, where default is the object name" do
162
+ options = {:count=>1, :scope=>[:activerecord, :models], :default=>"post"}
163
+ @english_locale.translations.create!(:key => 'activerecord.errors.models.blank', :value => 'post')
164
+ @backend.translate("en", :"models.blank", options).should == 'post'
165
+ end
166
+
167
+ it "should handle translating defaults used by active record attributes" do
168
+ options = {:scope=>[:activerecord, :attributes], :count=>1, :default=>["Content"]}
169
+ @backend.translate("en", :"post.content", options).should == 'Content'
170
+
171
+ # and when cached
172
+ options = {:scope=>[:activerecord, :attributes], :count=>1, :default=>["Content"]}
173
+ @backend.translate("en", :"post.content", options).should == 'Content'
174
+ @english_locale.should have(1).translation
175
+ end
176
+
177
+ it "should handle translating defaults used by active record attributes when the default is blank" do
178
+ options = { :count=>1, :default=> " "}
179
+ @backend.translate("en", "key", options).should == " "
180
+
181
+ # and when cached
182
+ options = { :count=>1, :default=> " "}
183
+ @backend.translate("en", "key", options).should == " "
184
+ @english_locale.should have(1).translation
185
+ end
186
+
187
+ it "should be able to handle interpolated values" do
188
+ options = {:some_value => 'INTERPOLATED'}
189
+ @english_locale.translations.create!(:key => 'Fred', :value => 'Fred has been {{some_value}}!!')
190
+ @backend.translate("en", 'Fred', options).should == 'Fred has been INTERPOLATED!!'
191
+ end
192
+
193
+ it "should be able to handle interpolated values with 'Fred {{some_value}}' also as the key" do
194
+ options = {:some_value => 'INTERPOLATED'}
195
+ @english_locale.translations.create!(:key => 'Fred {{some_value}}', :value => 'Fred {{some_value}}!!')
196
+ @backend.translate("en", 'Fred {{some_value}}', options).should == 'Fred INTERPOLATED!!'
197
+ end
198
+
199
+ it "should be able to handle interpolated count values" do
200
+ options = {:count=>1, :model => ["Cheese"], :scope=>[:activerecord, :errors], :default=>["Locale"]}
201
+ @english_locale.translations.create!(:key => 'activerecord.errors.messages.blank', :value => '{{count}} errors prohibited this {{model}} from being saved')
202
+ @backend.translate("en", :"messages.blank", options).should == '1 errors prohibited this Cheese from being saved'
203
+ end
204
+
205
+ it "should be able to handle the case of scope being passed in as something other than an array" do
206
+ options = {:count=>1, :model => ["Cheese"], :scope=> :activerecord, :default=>["Locale"]}
207
+ @english_locale.translations.create!(:key => 'activerecord.messages.blank', :value => 'dude')
208
+ @backend.translate("en", :"messages.blank", options).should == 'dude'
209
+ end
210
+
211
+ it "should be able to handle pluralization" do
212
+ @english_locale.translations.create!(:key => 'activerecord.errors.template.header', :value => '1 error prohibited this {{model}} from being saved', :pluralization_index => 1)
213
+ @english_locale.translations.create!(:key => 'activerecord.errors.template.header', :value => '{{count}} errors prohibited this {{model}} from being saved', :pluralization_index => 0)
214
+
215
+ options = {:count=>1, :model=>"translation", :scope=>[:activerecord, :errors, :template]}
216
+ @backend.translate("en", :"header", options).should == "1 error prohibited this translation from being saved"
217
+ @english_locale.should have(2).translations
218
+
219
+ options = {:count=>2, :model=>"translation", :scope=>[:activerecord, :errors, :template]}
220
+ @backend.translate("en", :"header", options).should == "2 errors prohibited this translation from being saved"
221
+ @english_locale.should have(2).translations
222
+ end
223
+
224
+ it "should find lowest level translation" do
225
+ @english_locale.translations.create!(:key => 'activerecord.errors.messages.blank', :value => 'is blank moron!')
226
+
227
+ options = {:attribute=>"Locale", :value=>nil,
228
+ :scope=>[:activerecord, :errors], :default=>[:"models.translation.blank", :"messages.blank"], :model=>"Translation"}
229
+
230
+ @backend.translate("en", :"models.translation.attributes.locale.blank", options).should == "is blank moron!"
231
+ end
232
+
233
+ it "should find higher level translation" do
234
+ @english_locale.translations.create!(:key => 'activerecord.errors.messages.blank', :value => 'is blank moron!')
235
+ @english_locale.translations.create!(:key => 'activerecord.errors.models.translation.blank', :value => 'translation blank')
236
+
237
+ options = {:attribute=>"Locale", :value=>nil,
238
+ :scope=>[:activerecord, :errors], :default=>[:"models.translation.blank", :"messages.blank"], :model=>"Translation"}
239
+
240
+ @backend.translate("en", :"models.translation.attributes.locale.blank", options).should == "translation blank"
241
+ end
242
+
243
+ it "should find highest level translation" do
244
+ @english_locale.translations.create!(:key => 'activerecord.errors.messages.blank', :value => 'is blank moron!')
245
+ @english_locale.translations.create!(:key => 'activerecord.errors.models.translation.blank', :value => 'translation blank')
246
+ @english_locale.translations.create!(:key => 'activerecord.errors.models.translation.attributes.locale.blank', :value => 'translation locale blank')
247
+
248
+ options = {:attribute=>"Locale", :value=>nil,
249
+ :scope=>[:activerecord, :errors], :default=>[:"models.translation.blank", :"messages.blank"], :model=>"Translation"}
250
+
251
+ @backend.translate("en", :"models.translation.attributes.locale.blank", options).should == "translation locale blank"
252
+ end
253
+
254
+ it "should create the translation for custom message" do
255
+ @english_locale.translations.create!(:key => 'activerecord.errors.messages.blank', :value => 'is blank moron!')
256
+
257
+ options = {:attribute=>"Locale", :value=>nil,
258
+ :scope=>[:activerecord, :errors], :default=>[:"models.translation.blank", "This is a custom message!", :"messages.blank"], :model=>"Translation"}
259
+
260
+ @backend.translate("en", :"models.translation.attributes.locale.blank", options).should == "This is a custom message!"
261
+ @english_locale.should have(2).translations
262
+ @english_locale.translations.find_by_key(Translation.hk("This is a custom message!")).value.should == 'This is a custom message!'
263
+ end
264
+
265
+ it "should find the translation for custom message" do
266
+ @english_locale.translations.create!(:key => 'activerecord.errors.messages.blank', :value => 'is blank moron!')
267
+ @english_locale.translations.create!(:key => 'This is a custom message!', :value => 'This is a custom message!')
268
+
269
+ options = {:attribute=>"Locale", :value=>nil,
270
+ :scope=>[:activerecord, :errors], :default=>[:"models.translation.blank", "This is a custom message!", :"messages.blank"], :model=>"Translation"}
271
+
272
+ @backend.translate("en", :"models.translation.attributes.locale.blank", options).should == "This is a custom message!"
273
+ end
274
+
275
+ it "should NOT find the translation for custom message" do
276
+ @english_locale.translations.create!(:key => 'activerecord.errors.messages.blank', :value => 'is blank moron!')
277
+ @english_locale.translations.create!(:key => 'This is a custom message!', :value => 'This is a custom message!')
278
+
279
+ options = {:attribute=>"Locale", :value=>nil,
280
+ :scope=>[:activerecord, :errors], :default=>[:"models.translation.blank", :"messages.blank"], :model=>"Translation"}
281
+
282
+ @backend.translate("en", :"models.translation.attributes.locale.blank", options).should == "is blank moron!"
283
+ end
284
+
285
+ it "should return an array" do
286
+ @english_locale.translations.create!(:key => 'date.month_names', :value => 'January', :pluralization_index => 1)
287
+ @english_locale.translations.create!(:key => 'date.month_names', :value => 'February', :pluralization_index => 2)
288
+ @english_locale.translations.create!(:key => 'date.month_names', :value => 'March', :pluralization_index => 3)
289
+ @backend.translate("en", :"date.month_names").should == [nil, 'January', 'February', 'March']
290
+
291
+ # once cached
292
+ @backend.translate("en", :"date.month_names").should == [nil, 'January', 'February', 'March']
293
+ @english_locale.reload.should have(3).translation
294
+ end
295
+
296
+ it "should return an unfrozen array" do
297
+ @english_locale.translations.create!(:key => 'date.month_names', :value => 'January', :pluralization_index => 1)
298
+ @english_locale.translations.create!(:key => 'date.month_names', :value => 'February', :pluralization_index => 2)
299
+ @english_locale.translations.create!(:key => 'date.month_names', :value => 'March', :pluralization_index => 3)
300
+ @backend.translate("en", :"date.month_names").should_not be_frozen
301
+
302
+ # once cached
303
+ @backend.translate("en", :"date.month_names").should_not be_frozen
304
+ end
305
+
306
+ it "should return an array of days" do
307
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Sunday', :pluralization_index => 0)
308
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Monday', :pluralization_index => 1)
309
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Tuesday', :pluralization_index => 2)
310
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Wednesday', :pluralization_index => 3)
311
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Thursday', :pluralization_index => 4)
312
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Friday', :pluralization_index => 5)
313
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Saturday', :pluralization_index => 6)
314
+
315
+ # once cached
316
+ @backend.translate("en", :"date.day_names").should == ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
317
+ @english_locale.reload.should have(7).translation
318
+ end
319
+
320
+ end
321
+
322
+ describe "and locale es" do
323
+ before(:each) do
324
+ I18n.locale = "es"
325
+ @spanish_locale = Locale.create!(:code => 'es')
326
+ end
327
+
328
+ it "should create a record with a nil value when the key is a string" do
329
+ @backend.translate("es", "String").should == "String"
330
+ @spanish_locale.should have(1).translation
331
+ @spanish_locale.translations.first.key.should == Translation.hk("String")
332
+ @spanish_locale.translations.first.value.should be_nil
333
+ end
334
+
335
+ it "should not write to the cache if the key already exists in the cache" do
336
+ @backend.cache_store = MemoryStoreProxy.new
337
+ @english_locale.translations.create!(:key => 'Message Center', :value => 'Message Center')
338
+ @spanish_locale.translations.create!(:key => 'Message Center', :value => nil)
339
+
340
+ @backend.translate("es", "Message Center").should == "Message Center"
341
+ @backend.cache_store.write_count.should == 1
342
+
343
+ @backend.translate("es", "Message Center").should == "Message Center"
344
+ @backend.cache_store.write_count.should == 1
345
+ end
346
+
347
+ it "should handle basic workflow" do
348
+ @english_locale.translations.create!(:key => 'Message Center', :value => 'Message Center')
349
+ @spanish_locale.translations.create!(:key => 'Message Center', :value => nil)
350
+
351
+ @backend.translate("en", "Message Center").should == "Message Center"
352
+
353
+ @english_locale.should have(1).translation
354
+ @english_locale.translations.first.key.should == Translation.hk("Message Center")
355
+ @english_locale.translations.first.raw_key.should == "Message Center"
356
+ @english_locale.translations.first.value.should == "Message Center"
357
+
358
+ @backend.translate("es", "Message Center").should == "Message Center"
359
+
360
+ @spanish_locale.should have(1).translation
361
+ @spanish_locale.translations.first.key.should == Translation.hk("Message Center")
362
+ @spanish_locale.translations.first.value.should be_nil
363
+
364
+ @backend.translate("es", "Message Center").should == "Message Center"
365
+ @spanish_locale.should have(1).translation
366
+ end
367
+
368
+ it "should return default locale (en) value and create the spanish record" do
369
+ @english_locale.translations.create!(:key => 'String', :value => 'English String')
370
+ @backend.translate("es", "String").should == "English String"
371
+ @spanish_locale.should have(1).translation
372
+ end
373
+
374
+ it "should return just the passed in value when no translated record and no default translation" do
375
+ @backend.translate("es", "String").should == "String"
376
+ @spanish_locale.should have(1).translation
377
+ end
378
+
379
+ it "should support having a default locale record with a nil value" do
380
+ @english_locale.translations.create!(:key => 'date.order')
381
+ @backend.translate("es", :'date.order').should be_nil
382
+
383
+ @spanish_locale.should have(1).translation
384
+ @spanish_locale.translations.first.key.should == Translation.hk('date.order')
385
+ @spanish_locale.translations.first.value.should be_nil
386
+ end
387
+
388
+ it "should be able to handle interpolated values" do
389
+ options = {:some_value => 'INTERPOLATED'}
390
+ @english_locale.translations.create!(:key => 'Fred', :value => 'Fred has been {{some_value}}!!')
391
+ @spanish_locale.translations.create!(:key => 'Fred', :value => 'Fred ha sido {{some_value}}!!')
392
+ @backend.translate("es", 'Fred', options).should == 'Fred ha sido INTERPOLATED!!'
393
+ end
394
+
395
+ it "should be able to handle interpolated count values" do
396
+ options = {:count=>1, :model => ["Cheese"], :scope=>[:activerecord, :errors], :default=>["Locale"]}
397
+ @english_locale.translations.create!(:key => 'activerecord.errors.messages.blank', :value => '{{count}} error prohibited this {{model}} from being saved')
398
+ @backend.translate("es", :"messages.blank", options).should == '1 error prohibited this Cheese from being saved'
399
+ end
400
+
401
+ it "should be able to handle pluralization" do
402
+ @english_locale.translations.create!(:key => 'activerecord.errors.template.header', :value => '1 error prohibited this {{model}} from being saved', :pluralization_index => 1)
403
+ @english_locale.translations.create!(:key => 'activerecord.errors.template.header', :value => '{{count}} errors prohibited this {{model}} from being saved', :pluralization_index => 0)
404
+ options = {:count=>1, :model=>"translation", :scope=>[:activerecord, :errors, :template]}
405
+ @backend.translate("es", :"header", options).should == "1 error prohibited this translation from being saved"
406
+ @spanish_locale.should have(2).translations
407
+
408
+ options = {:count=>2, :model=>"translation", :scope=>[:activerecord, :errors, :template]}
409
+ @backend.translate("es", :"header", options).should == "2 errors prohibited this translation from being saved"
410
+ @spanish_locale.reload.should have(2).translations
411
+ end
412
+
413
+ it "should find lowest level translation" do
414
+ @english_locale.translations.create!(:key => 'activerecord.errors.messages.blank', :value => 'is blank moron!')
415
+
416
+ options = {:attribute=>"Locale", :value=>nil,
417
+ :scope=>[:activerecord, :errors], :default=>[:"models.translation.blank", :"messages.blank"], :model=>"Translation"}
418
+
419
+ @backend.translate("es", :"models.translation.attributes.locale.blank", options).should == "is blank moron!"
420
+
421
+ @spanish_locale.should have(1).translation
422
+ @spanish_locale.translations.first.key.should == Translation.hk("activerecord.errors.messages.blank")
423
+ @spanish_locale.translations.first.value.should be_nil
424
+ end
425
+
426
+ it "should find higher level translation" do
427
+ @english_locale.translations.create!(:key => 'activerecord.errors.messages.blank', :value => 'is blank moron!')
428
+ @english_locale.translations.create!(:key => 'activerecord.errors.models.translation.blank', :value => 'translation blank')
429
+
430
+ options = {:attribute=>"Locale", :value=>nil,
431
+ :scope=>[:activerecord, :errors], :default=>[:"models.translation.blank", :"messages.blank"], :model=>"Translation"}
432
+
433
+ @backend.translate("es", :"models.translation.attributes.locale.blank", options).should == "translation blank"
434
+ @spanish_locale.should have(1).translation
435
+ @spanish_locale.translations.first.key.should == Translation.hk("activerecord.errors.models.translation.blank")
436
+ @spanish_locale.translations.first.value.should be_nil
437
+ @backend.cache_store.read("es:activerecord.errors.models.translation.blank", "translation blank")
438
+ end
439
+
440
+ it "should find highest level translation" do
441
+ @english_locale.translations.create!(:key => 'activerecord.errors.messages.blank', :value => 'is blank moron!')
442
+ @english_locale.translations.create!(:key => 'activerecord.errors.models.translation.blank', :value => 'translation blank')
443
+ @english_locale.translations.create!(:key => 'activerecord.errors.models.translation.attributes.locale.blank', :value => 'translation locale blank')
444
+
445
+ options = {:attribute=>"Locale", :value=>nil,
446
+ :scope=>[:activerecord, :errors], :default=>[:"models.translation.blank", :"messages.blank"], :model=>"Translation"}
447
+
448
+ @backend.translate("es", :"models.translation.attributes.locale.blank", options).should == "translation locale blank"
449
+ @spanish_locale.should have(1).translation
450
+ @spanish_locale.translations.first.key.should == Translation.hk("activerecord.errors.models.translation.attributes.locale.blank")
451
+ @spanish_locale.translations.first.value.should be_nil
452
+ end
453
+
454
+ it "should find the translation for custom message" do
455
+ @english_locale.translations.create!(:key => 'activerecord.errors.messages.blank', :value => 'is blank moron!')
456
+ @english_locale.translations.create!(:key => 'This is a custom message!', :value => 'This is a custom message!')
457
+
458
+ options = {:attribute=>"Locale", :value=>nil,
459
+ :scope=>[:activerecord, :errors], :default=>[:"models.translation.blank", "This is a custom message!", :"messages.blank"], :model=>"Translation"}
460
+
461
+ @backend.translate("es", :"models.translation.attributes.locale.blank", options).should == "This is a custom message!"
462
+ @spanish_locale.should have(1).translation
463
+ @spanish_locale.translations.first.key.should == Translation.hk("This is a custom message!")
464
+ @spanish_locale.translations.first.value.should be_nil
465
+ end
466
+
467
+ it "should return an array from spanish locale" do
468
+ @english_locale.translations.create!(:key => 'date.month_names', :value => 'January', :pluralization_index => 1)
469
+ @english_locale.translations.create!(:key => 'date.month_names', :value => 'February', :pluralization_index => 2)
470
+ @english_locale.translations.create!(:key => 'date.month_names', :value => 'March', :pluralization_index => 3)
471
+ @spanish_locale.translations.create!(:key => 'date.month_names', :value => 'Enero', :pluralization_index => 1)
472
+ @spanish_locale.translations.create!(:key => 'date.month_names', :value => 'Febrero', :pluralization_index => 2)
473
+ @spanish_locale.translations.create!(:key => 'date.month_names', :value => 'Marzo', :pluralization_index => 3)
474
+
475
+ @backend.translate("es", :"date.month_names").should == [nil, 'Enero', 'Febrero', 'Marzo']
476
+
477
+ # once cached
478
+ @backend.translate("es", :"date.month_names").should == [nil, 'Enero', 'Febrero', 'Marzo']
479
+ @english_locale.reload.should have(3).translation
480
+ @spanish_locale.reload.should have(3).translation
481
+ end
482
+
483
+ it "should return an array from default locale" do
484
+ @english_locale.translations.create!(:key => 'date.month_names', :value => 'January', :pluralization_index => 1)
485
+ @english_locale.translations.create!(:key => 'date.month_names', :value => 'February', :pluralization_index => 2)
486
+ @english_locale.translations.create!(:key => 'date.month_names', :value => 'March', :pluralization_index => 3)
487
+ @backend.translate("es", :"date.month_names").should == [nil, 'January', 'February', 'March']
488
+
489
+ # once cached
490
+ @backend.translate("es", :"date.month_names").should == [nil, 'January', 'February', 'March']
491
+ @english_locale.reload.should have(3).translation
492
+ @spanish_locale.reload.should have(3).translation
493
+ end
494
+
495
+ it "should return an array of days" do
496
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Sunday', :pluralization_index => 0)
497
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Monday', :pluralization_index => 1)
498
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Tuesday', :pluralization_index => 2)
499
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Wednesday', :pluralization_index => 3)
500
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Thursday', :pluralization_index => 4)
501
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Friday', :pluralization_index => 5)
502
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Saturday', :pluralization_index => 6)
503
+ @backend.translate("es", :"date.day_names").should == ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
504
+
505
+ # once cached
506
+ @backend.translate("es", :"date.day_names").should == ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
507
+ @english_locale.reload.should have(7).translation
508
+ @spanish_locale.reload.should have(7).translation
509
+ end
510
+
511
+ it "should return an array of days" do
512
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Sunday', :pluralization_index => 0)
513
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Monday', :pluralization_index => 1)
514
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Tuesday', :pluralization_index => 2)
515
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Wednesday', :pluralization_index => 3)
516
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Thursday', :pluralization_index => 4)
517
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Friday', :pluralization_index => 5)
518
+ @english_locale.translations.create!(:key => 'date.day_names', :value => 'Saturday', :pluralization_index => 6)
519
+ @spanish_locale.translations.create!(:key => 'date.day_names', :value => nil, :pluralization_index => 0)
520
+ @spanish_locale.translations.create!(:key => 'date.day_names', :value => nil, :pluralization_index => 1)
521
+ @spanish_locale.translations.create!(:key => 'date.day_names', :value => nil, :pluralization_index => 2)
522
+ @spanish_locale.translations.create!(:key => 'date.day_names', :value => nil, :pluralization_index => 3)
523
+ @spanish_locale.translations.create!(:key => 'date.day_names', :value => nil, :pluralization_index => 4)
524
+ @spanish_locale.translations.create!(:key => 'date.day_names', :value => nil, :pluralization_index => 5)
525
+ @spanish_locale.translations.create!(:key => 'date.day_names', :value => nil, :pluralization_index => 6)
526
+ @backend.translate("es", :"date.day_names").should == ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
527
+
528
+ # once cached
529
+ @backend.translate("es", :"date.day_names").should == ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
530
+ @english_locale.reload.should have(7).translation
531
+ @spanish_locale.reload.should have(7).translation
532
+ end
533
+
534
+ it "should support storing values as YAML symbols" do
535
+ @english_locale.translations.create!(:key => 'date.order', :value => "--- :year\n", :pluralization_index => 0)
536
+ @english_locale.translations.create!(:key => 'date.order', :value => "--- :month\n", :pluralization_index => 1)
537
+ @english_locale.translations.create!(:key => 'date.order', :value => "--- :day\n", :pluralization_index => 2)
538
+
539
+ @backend.translate("es", :'date.order').should == [:year, :month, :day]
540
+ @english_locale.should have(3).translations
541
+ @spanish_locale.should have(3).translations
542
+ end
543
+
544
+ end
545
+ end
546
+ end