i18n_backend_database_rails3 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/Gemfile +4 -0
  2. data/LICENSE +21 -0
  3. data/README.textile +125 -0
  4. data/Rakefile +1 -0
  5. data/data/locales.yml +4 -0
  6. data/generators/i18n_backend_database/i18n_backend_database_generator.rb +8 -0
  7. data/generators/i18n_backend_database/templates/migrate/create_i18n_tables.rb +19 -0
  8. data/i18n_backend_database.gemspec +24 -0
  9. data/init.rb +1 -0
  10. data/lib/controllers/locales_controller.rb +86 -0
  11. data/lib/controllers/translations_controller.rb +141 -0
  12. data/lib/ext/i18n.rb +68 -0
  13. data/lib/google_language.rb +30 -0
  14. data/lib/i18n_backend_database.rb +9 -0
  15. data/lib/i18n_backend_database/database.rb +263 -0
  16. data/lib/i18n_backend_database/version.rb +7 -0
  17. data/lib/i18n_util.rb +148 -0
  18. data/lib/models/locale.rb +67 -0
  19. data/lib/models/translation.rb +46 -0
  20. data/lib/models/translation_option.rb +26 -0
  21. data/lib/public/images/custom1_bar.gif +0 -0
  22. data/lib/public/images/custom1_box.gif +0 -0
  23. data/lib/public/images/percentImage.png +0 -0
  24. data/lib/public/images/percentImage_back.png +0 -0
  25. data/lib/public/images/percentImage_back1.png +0 -0
  26. data/lib/public/images/percentImage_back2.png +0 -0
  27. data/lib/public/images/percentImage_back3.png +0 -0
  28. data/lib/public/images/percentImage_back4.png +0 -0
  29. data/lib/public/javascripts/jsProgressBarHandler.js +509 -0
  30. data/lib/routing.rb +15 -0
  31. data/lib/views/layouts/translations.html.haml +16 -0
  32. data/lib/views/locales/edit.html.haml +16 -0
  33. data/lib/views/locales/index.html.haml +14 -0
  34. data/lib/views/locales/new.html.haml +14 -0
  35. data/lib/views/locales/show.html.haml +10 -0
  36. data/lib/views/translations/asset_translations.html.haml +23 -0
  37. data/lib/views/translations/edit.html.haml +24 -0
  38. data/lib/views/translations/index.html.haml +21 -0
  39. data/lib/views/translations/new.html.haml +14 -0
  40. data/lib/views/translations/show.html.haml +21 -0
  41. data/lib/views/translations/translations.html.haml +20 -0
  42. data/lib/views/translations/update.rjs +7 -0
  43. data/routes.rb +3 -0
  44. data/spec/assets/public/es/favicons/favicon1.gif +0 -0
  45. data/spec/assets/public/es/images/icons/icon1.gif +0 -0
  46. data/spec/assets/public/es/images/image1.gif +0 -0
  47. data/spec/assets/public/favicons/favicon1.gif +0 -0
  48. data/spec/assets/public/favicons/favicon2.gif +0 -0
  49. data/spec/assets/public/images/icons/icon1.gif +0 -0
  50. data/spec/assets/public/images/icons/icon2.gif +0 -0
  51. data/spec/assets/public/images/image1.gif +0 -0
  52. data/spec/assets/public/images/image2.gif +0 -0
  53. data/spec/assets/public/images/promo/sfc08_140x400_3.gif +0 -0
  54. data/spec/assets/views/test_view1.html.erb +1 -0
  55. data/spec/assets/views/test_view2.html.erb +30 -0
  56. data/spec/caching_spec.rb +87 -0
  57. data/spec/controllers/locales_controller_spec.rb +173 -0
  58. data/spec/controllers/locales_routing_spec.rb +59 -0
  59. data/spec/controllers/translations_controller_spec.rb +189 -0
  60. data/spec/controllers/translations_routing_spec.rb +59 -0
  61. data/spec/database_spec.rb +199 -0
  62. data/spec/i18n_ext_spec.rb +40 -0
  63. data/spec/localize_spec.rb +66 -0
  64. data/spec/localize_text_spec.rb +76 -0
  65. data/spec/models/locale_spec.rb +111 -0
  66. data/spec/models/translation_spec.rb +44 -0
  67. data/spec/spec_helper.rb +16 -0
  68. data/spec/translate_asset_spec.rb +57 -0
  69. data/spec/translate_spec.rb +546 -0
  70. data/tasks/i18n.rake +60 -0
  71. metadata +143 -0
@@ -0,0 +1,59 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe LocalesController do
4
+ describe "route generation" do
5
+ it "should map #index" do
6
+ route_for(:controller => "locales", :action => "index").should == "/locales"
7
+ end
8
+
9
+ it "should map #new" do
10
+ route_for(:controller => "locales", :action => "new").should == "/locales/new"
11
+ end
12
+
13
+ it "should map #show" do
14
+ route_for(:controller => "locales", :action => "show", :id => "1").should == "/locales/1"
15
+ end
16
+
17
+ it "should map #edit" do
18
+ route_for(:controller => "locales", :action => "edit", :id => "1").should == "/locales/1/edit"
19
+ end
20
+
21
+ it "should map #update" do
22
+ route_for(:controller => "locales", :action => "update", :id => "1").should == {:path => "/locales/1", :method => :put}
23
+ end
24
+
25
+ it "should map #destroy" do
26
+ route_for(:controller => "locales", :action => "destroy", :id => "1").should == {:path => "/locales/1", :method => :delete}
27
+ end
28
+ end
29
+
30
+ describe "route recognition" do
31
+ it "should generate params for #index" do
32
+ params_from(:get, "/locales").should == {:controller => "locales", :action => "index"}
33
+ end
34
+
35
+ it "should generate params for #new" do
36
+ params_from(:get, "/locales/new").should == {:controller => "locales", :action => "new"}
37
+ end
38
+
39
+ it "should generate params for #create" do
40
+ params_from(:post, "/locales").should == {:controller => "locales", :action => "create"}
41
+ end
42
+
43
+ it "should generate params for #show" do
44
+ params_from(:get, "/locales/1").should == {:controller => "locales", :action => "show", :id => "1"}
45
+ end
46
+
47
+ it "should generate params for #edit" do
48
+ params_from(:get, "/locales/1/edit").should == {:controller => "locales", :action => "edit", :id => "1"}
49
+ end
50
+
51
+ it "should generate params for #update" do
52
+ params_from(:put, "/locales/1").should == {:controller => "locales", :action => "update", :id => "1"}
53
+ end
54
+
55
+ it "should generate params for #destroy" do
56
+ params_from(:delete, "/locales/1").should == {:controller => "locales", :action => "destroy", :id => "1"}
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,189 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe TranslationsController do
4
+
5
+ def mock_locale(stubs={})
6
+ stubs = {
7
+ :translations => mock("Array of Translations"),
8
+ :to_param => 'en'
9
+ }.merge(stubs)
10
+
11
+ @mock_locale ||= mock_model(Locale, stubs)
12
+ end
13
+
14
+ def mock_translation(stubs={})
15
+ stubs = {
16
+ :value => nil
17
+ }.merge(stubs)
18
+ @mock_translation ||= mock_model(Translation, stubs)
19
+ end
20
+
21
+ before(:each) do
22
+ Locale.should_receive(:find_by_code).with('en').and_return(mock_locale)
23
+ end
24
+
25
+ describe "responding to GET index" do
26
+
27
+ it "should expose all translations as @translations" do
28
+ mock_locale.translations.should_receive(:find).with(:all, {:order=>"raw_key, pluralization_index"}).and_return([mock_translation])
29
+ get :index, :locale_id => "en"
30
+ assigns[:translations].should == [mock_translation]
31
+ end
32
+
33
+ describe "with mime type of xml" do
34
+
35
+ it "should render all translations as xml" do
36
+ request.env["HTTP_ACCEPT"] = "application/xml"
37
+ mock_locale.translations.should_receive(:find).with(:all, {:order=>"raw_key, pluralization_index"}).and_return(translations = mock("Array of Translations"))
38
+ translations.should_receive(:to_xml).and_return("generated XML")
39
+ get :index, :locale_id => "en"
40
+ response.body.should == "generated XML"
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
47
+ describe "responding to GET show" do
48
+
49
+ it "should expose the requested translation as @translation" do
50
+ mock_locale.translations.should_receive(:find).with("37").and_return(mock_translation)
51
+ get :show, :locale_id => "en", :id => "37"
52
+ assigns[:translation].should equal(mock_translation)
53
+ end
54
+
55
+ describe "with mime type of xml" do
56
+
57
+ it "should render the requested translation as xml" do
58
+ request.env["HTTP_ACCEPT"] = "application/xml"
59
+ mock_locale.translations.should_receive(:find).with("37").and_return(mock_translation)
60
+ mock_translation.should_receive(:to_xml).and_return("generated XML")
61
+ get :show, :locale_id => "en", :id => "37"
62
+ response.body.should == "generated XML"
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+
69
+ describe "responding to GET new" do
70
+
71
+ it "should expose a new translation as @translation" do
72
+ Translation.should_receive(:new).and_return(mock_translation)
73
+ get :new, :locale_id => "en"
74
+ assigns[:translation].should equal(mock_translation)
75
+ end
76
+
77
+ end
78
+
79
+ describe "responding to GET edit" do
80
+
81
+ it "should expose the requested translation as @translation" do
82
+ mock_locale.translations.should_receive(:find).with("37").and_return(mock_translation)
83
+ get :edit, :locale_id => "en", :id => "37"
84
+ assigns[:translation].should equal(mock_translation)
85
+ end
86
+
87
+ end
88
+
89
+ describe "responding to POST create" do
90
+
91
+ describe "with valid params" do
92
+
93
+ it "should expose a newly created translation as @translation" do
94
+ mock_locale.translations.should_receive(:build).with({'these' => 'params'}).and_return(mock_translation(:save => true))
95
+ post :create, :locale_id => "en", :translation => {:these => 'params'}
96
+ assigns(:translation).should equal(mock_translation)
97
+ end
98
+
99
+ it "should redirect to the created translation" do
100
+ mock_locale.translations.stub!(:build).and_return(mock_translation(:save => true))
101
+ post :create, :locale_id => "en", :translation => {}
102
+ response.should redirect_to(locale_translation_url(mock_locale, mock_translation))
103
+ end
104
+
105
+ end
106
+
107
+ describe "with invalid params" do
108
+
109
+ it "should expose a newly created but unsaved translation as @translation" do
110
+ mock_locale.translations.stub!(:build).with({'these' => 'params'}).and_return(mock_translation(:save => false))
111
+ post :create, :locale_id => "en", :translation => {:these => 'params'}
112
+ assigns(:translation).should equal(mock_translation)
113
+ end
114
+
115
+ it "should re-render the 'new' template" do
116
+ mock_locale.translations.stub!(:build).and_return(mock_translation(:save => false))
117
+ post :create, :locale_id => "en", :translation => {}
118
+ response.should render_template('new')
119
+ end
120
+
121
+ end
122
+
123
+ end
124
+
125
+ describe "responding to PUT udpate" do
126
+
127
+ describe "with valid params" do
128
+
129
+ it "should update the requested translation" do
130
+ mock_locale.translations.should_receive(:find).with("37").and_return(mock_translation)
131
+ mock_translation.should_receive(:update_attributes).with({'these' => 'params'})
132
+ put :update, :locale_id => "en", :id => "37", :translation => {:these => 'params'}
133
+ end
134
+
135
+ it "should expose the requested translation as @translation" do
136
+ mock_locale.translations.stub!(:find).and_return(mock_translation(:update_attributes => true))
137
+ put :update, :locale_id => "en", :id => "1"
138
+ assigns(:translation).should equal(mock_translation)
139
+ end
140
+
141
+ it "should redirect to the translation" do
142
+ mock_locale.translations.stub!(:find).and_return(mock_translation(:update_attributes => true))
143
+ put :update, :locale_id => "en", :id => "1"
144
+ response.should redirect_to(locale_translation_url(mock_locale, mock_translation))
145
+ end
146
+
147
+ end
148
+
149
+ describe "with invalid params" do
150
+
151
+ it "should update the requested translation" do
152
+ mock_locale.translations.should_receive(:find).with("37").and_return(mock_translation)
153
+ mock_translation.should_receive(:update_attributes).with({'these' => 'params'})
154
+ put :update, :locale_id => "en", :id => "37", :translation => {:these => 'params'}
155
+ end
156
+
157
+ it "should expose the translation as @translation" do
158
+ mock_locale.translations.stub!(:find).and_return(mock_translation(:update_attributes => false))
159
+ put :update, :locale_id => "en", :id => "1"
160
+ assigns(:translation).should equal(mock_translation)
161
+ end
162
+
163
+ it "should re-render the 'edit' template" do
164
+ mock_locale.translations.stub!(:find).and_return(mock_translation(:update_attributes => false))
165
+ put :update, :locale_id => "en", :id => "1"
166
+ response.should render_template('edit')
167
+ end
168
+
169
+ end
170
+
171
+ end
172
+
173
+ describe "responding to DELETE destroy" do
174
+
175
+ it "should destroy the requested translation" do
176
+ mock_locale.translations.should_receive(:find).with("37").and_return(mock_translation)
177
+ mock_translation.should_receive(:destroy)
178
+ delete :destroy, :locale_id => "en", :id => "37"
179
+ end
180
+
181
+ it "should redirect to the translations list" do
182
+ mock_locale.translations.stub!(:find).and_return(mock_translation(:destroy => true))
183
+ delete :destroy, :locale_id => "en", :id => "1"
184
+ response.should redirect_to(locale_translations_url(mock_locale))
185
+ end
186
+
187
+ end
188
+
189
+ end
@@ -0,0 +1,59 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe TranslationsController do
4
+ describe "route generation" do
5
+ it "should map #index" do
6
+ route_for(:controller => "translations", :action => "index", :locale_id => "en").should == "/locales/en/translations"
7
+ end
8
+
9
+ it "should map #new" do
10
+ route_for(:controller => "translations", :action => "new", :locale_id => "en").should == "/locales/en/translations/new"
11
+ end
12
+
13
+ it "should map #show" do
14
+ route_for(:controller => "translations", :action => "show", :id => "1", :locale_id => "en").should == "/locales/en/translations/1"
15
+ end
16
+
17
+ it "should map #edit" do
18
+ route_for(:controller => "translations", :action => "edit", :id => "1", :locale_id => "en").should == "/locales/en/translations/1/edit"
19
+ end
20
+
21
+ it "should map #update" do
22
+ route_for(:controller => "translations", :action => "update", :id => "1", :locale_id => "en").should == {:path => "/locales/en/translations/1", :method => :put}
23
+ end
24
+
25
+ it "should map #destroy" do
26
+ route_for(:controller => "translations", :action => "destroy", :id => "1", :locale_id => "en").should == {:path => "/locales/en/translations/1", :method => :delete}
27
+ end
28
+ end
29
+
30
+ describe "route recognition" do
31
+ it "should generate params for #index" do
32
+ params_from(:get, "/locales/en/translations").should == {:controller => "translations", :action => "index", :locale_id => "en"}
33
+ end
34
+
35
+ it "should generate params for #new" do
36
+ params_from(:get, "/locales/en/translations/new").should == {:controller => "translations", :action => "new", :locale_id => "en"}
37
+ end
38
+
39
+ it "should generate params for #create" do
40
+ params_from(:post, "/locales/en/translations").should == {:controller => "translations", :action => "create", :locale_id => "en"}
41
+ end
42
+
43
+ it "should generate params for #show" do
44
+ params_from(:get, "/locales/en/translations/1").should == {:controller => "translations", :action => "show", :id => "1", :locale_id => "en"}
45
+ end
46
+
47
+ it "should generate params for #edit" do
48
+ params_from(:get, "/locales/en/translations/1/edit").should == {:controller => "translations", :action => "edit", :id => "1", :locale_id => "en"}
49
+ end
50
+
51
+ it "should generate params for #update" do
52
+ params_from(:put, "/locales/en/translations/1").should == {:controller => "translations", :action => "update", :id => "1", :locale_id => "en"}
53
+ end
54
+
55
+ it "should generate params for #destroy" do
56
+ params_from(:delete, "/locales/en/translations/1").should == {:controller => "translations", :action => "destroy", :id => "1", :locale_id => "en"}
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,199 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe I18n::Backend::Database do
4
+ describe "an instance" do
5
+ before {
6
+ I18n.locale = "es"
7
+ @locales = [:en, :es, :it]
8
+ @locale = mock_model(Locale, { :code => "es" })
9
+ Locale.stub!(:available_locales).and_return(@locales)
10
+ Locale.stub!(:find_by_code).and_return(@locale)
11
+ @database = I18n::Backend::Database.new
12
+ }
13
+
14
+ it "should use the current Rails cache store if none are provided" do
15
+ @database.cache_store.should == Rails.cache
16
+ end
17
+
18
+ it "should use a custom cache store if provided" do
19
+ @database = I18n::Backend::Database.new({:cache_store => :mem_cache_store})
20
+ @database.cache_store.class.should == ActiveSupport::Cache::MemCacheStore
21
+ end
22
+
23
+ it "should have default localize text tag if none provided" do
24
+ @database.localize_text_tag.should == '^^'
25
+ end
26
+
27
+ it "should use custom localize text tag if provided" do
28
+ @database = I18n::Backend::Database.new({:localize_text_tag => '##'})
29
+ @database.localize_text_tag.should == '##'
30
+ end
31
+
32
+ it "should delegate the call to available_locales to the Locale class" do
33
+ Locale.should_receive(:available_locales)
34
+ @database.available_locales
35
+ end
36
+
37
+ it "should return all the available locales on a call to available_locales" do
38
+ @database.available_locales.should == @locales
39
+ end
40
+
41
+ it "should return a cache key of locale:key on call to build_cache_key" do
42
+ hash_key = Translation.hk("hola me amigo!")
43
+ Translation.ck(@locale, "hola me amigo!", 1).should == "es:#{hash_key}"
44
+ end
45
+
46
+ it "should generate a Base64 encoded (minus newline), MD5 encrypted hash, based on the key" do
47
+ encrypted_key = Digest::MD5.hexdigest("aoeuaoeu")
48
+ completed_key = Translation.hk("aoeuaoeu")
49
+ encrypted_key.should == Base64.decode64(completed_key).gsub(/\n/, '')
50
+ end
51
+
52
+ it "should have a nil locale cache by default" do
53
+ @database.locale.should == nil
54
+ end
55
+
56
+ it "should be able to set the locale cache by passing a locale code into locale=" do
57
+ @database.locale = "es"
58
+ @database.locale.should == @locale
59
+ end
60
+ end
61
+
62
+ # describe "omg an aminal" do
63
+ #
64
+ # before(:each) do
65
+ # Locale.instance_variable_set("@validate_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
66
+ # Locale.create!(:code => "en")
67
+ # end
68
+ #
69
+ # it "should contain one 'blank' key in the database" do
70
+ # Locale.validates_presence_of :code
71
+ # l = Locale.new
72
+ # l.valid?
73
+ # Translation.find_by_value("can't be blank").should_not be_nil
74
+ # end
75
+ #
76
+ # it "should contain one 'blank' key and one custom 'blank' key in the database" do
77
+ # Locale.validates_presence_of :code, :message => "ain't blank sucka"
78
+ # l = Locale.new
79
+ # l.valid?
80
+ # Translation.find_by_value("ain't blank sucka").should_not be_nil
81
+ # Translation.find_by_value("can't be blank").should be_nil
82
+ # end
83
+ #
84
+ # it "should use the blank code if a custom code is present, but not enabled" do
85
+ # Locale.validates_presence_of :code, :message => "ain't blank sucka"
86
+ #
87
+ # l = Locale.new
88
+ # l.valid?
89
+ # l.errors_on(:code).should include("ain't blank sucka")
90
+ #
91
+ # Locale.validates_presence_of :code
92
+ #
93
+ # l = Locale.new
94
+ # l.valid?
95
+ # l.errors_on(:code).should include("can't be blank")
96
+ # end
97
+ # end
98
+
99
+ describe "translating a key" do
100
+ describe "for the first time in the default locale" do
101
+ before {
102
+ I18n.locale = "en"
103
+ I18n.default_locale = "en"
104
+ Locale.create({:code => "en", :name => "English"})
105
+ @database = I18n::Backend::Database.new
106
+ @database.translate(:en, "dog")
107
+ }
108
+
109
+ it "should set the value of the translation" do
110
+ Translation.first.value.should == "dog"
111
+ end
112
+ end
113
+
114
+ describe "for the first time in an alternate locale" do
115
+ before {
116
+ I18n.locale = "es"
117
+ I18n.default_locale = "en"
118
+ Locale.create({:code => "en", :name => "English"})
119
+ Locale.create({:code => "es", :name => "Spanish"})
120
+ @database = I18n::Backend::Database.new
121
+ @database.translate(:es, "dog")
122
+ }
123
+
124
+ it "should set the value of the translation to nil" do
125
+ Translation.first.value.should == nil
126
+ end
127
+ end
128
+ end
129
+
130
+ describe "setting a locale in context" do
131
+ before {
132
+ I18n.locale = "es"
133
+ @locale = mock_model(Locale, { :code => "es" })
134
+ @database = I18n::Backend::Database.new
135
+ }
136
+
137
+ describe "on a new instance when the cache locale is nil" do
138
+ before {
139
+ Locale.stub!(:find_by_code).and_return(@locale)
140
+ }
141
+
142
+ it "should return a locale record for the current locale in context" do
143
+ Locale.should_receive(:find_by_code).with(I18n.locale)
144
+ @database.send(:locale_in_context, I18n.locale)
145
+ end
146
+ end
147
+
148
+ describe "when passing in a temporary locale that's different from the local cache" do
149
+ before {
150
+ Locale.stub!(:find_by_code).with("it").and_return(@locale)
151
+ @database.locale = "it"
152
+ }
153
+
154
+ it "should return a locale record for the temporary locale" do
155
+ Locale.should_receive(:find_by_code).with("it")
156
+ @database.send(:locale_in_context, "it")
157
+ end
158
+
159
+ it "should set the locale to the temporary value" do
160
+ @database.send(:locale_in_context, "it").should == @locale
161
+ end
162
+ end
163
+
164
+ describe "when passing in a temporary locale that's the same as the local cache" do
165
+ before {
166
+ Locale.stub!(:find_by_code).with("es").and_return(@locale)
167
+ @database.locale = "es"
168
+ }
169
+
170
+ it "should set the locale to the temporary value" do
171
+ @database.send(:locale_in_context, "es").should == @locale
172
+ end
173
+ end
174
+
175
+ describe "when the locale is the same as the cache" do
176
+ before {
177
+ Locale.stub!(:find_by_code).with("es").and_return(@locale)
178
+ }
179
+
180
+ it "should update the locale cache with the new locale" do
181
+ @database.locale = "es"
182
+ @database.send(:locale_in_context, "es").should == @database.locale
183
+ end
184
+ end
185
+
186
+ describe "when the locale is different than the cache" do
187
+ before {
188
+ Locale.stub!(:find_by_code).with("es").and_return(@locale)
189
+ I18n.locale = "it"
190
+ }
191
+
192
+ it "should update the locale cache with the new locale" do
193
+ @database.locale = "es"
194
+ Locale.should_receive(:find_by_code).with("it")
195
+ @database.send(:locale_in_context, "it")
196
+ end
197
+ end
198
+ end
199
+ end