i18n_backend_database 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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,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
@@ -0,0 +1,40 @@
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
+ end
9
+
10
+ describe "and locale en" do
11
+ before(:each) do
12
+ I18n.locale = "en"
13
+ end
14
+
15
+ it "should return empty local segment" do
16
+ I18n.locale_segment.should == ''
17
+ end
18
+
19
+ it "should return empty local segment using alias" do
20
+ I18n.ls.should == ''
21
+ end
22
+ end
23
+
24
+ describe "and locale es" do
25
+ before(:each) do
26
+ I18n.locale = "es"
27
+ end
28
+
29
+ it "should return es local segment" do
30
+ I18n.locale_segment.should == '/es'
31
+ end
32
+
33
+ it "should return es local segment using alias" do
34
+ I18n.ls.should == '/es'
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,66 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe I18n::Backend::Database do
4
+ before(:each) do
5
+ @backend = I18n::Backend::Database.new
6
+ end
7
+
8
+ after(:each) do
9
+ @backend.cache_store.clear
10
+ end
11
+
12
+ describe "with default locale en" do
13
+ before(:each) do
14
+ I18n.default_locale = "en"
15
+ if File.exists?('vendor/rails/activesupport/lib/active_support/locale/en.yml')
16
+ I18nUtil.load_from_yml 'vendor/rails/activesupport/lib/active_support/locale/en.yml'
17
+ else
18
+ activesupport_gem = Gem.cache.find_name('activesupport').sort_by { |g| g.version.version }.last
19
+ I18nUtil.load_from_yml "#{activesupport_gem.full_gem_path}/lib/active_support/locale/en.yml"
20
+ end
21
+ end
22
+
23
+ describe "and locale en" do
24
+ before(:each) do
25
+ I18n.locale = "en"
26
+ end
27
+
28
+ it "should localize dates in different formats" do
29
+ dates = {
30
+ Date.new(2009,1,1) => {:default => "2009-01-01", :short => "Jan 01", :long => "January 01, 2009"},
31
+ Date.new(2009,1,31) => {:default => "2009-01-31", :short => "Jan 31", :long => "January 31, 2009"},
32
+ Date.new(2009,2,2) => {:default => "2009-02-02", :short => "Feb 02", :long => "February 02, 2009"},
33
+ Date.new(2009,2,28) => {:default => "2009-02-28", :short => "Feb 28", :long => "February 28, 2009"},
34
+ Date.new(2008,2,29) => {:default => "2008-02-29", :short => "Feb 29", :long => "February 29, 2008"},
35
+ Date.new(2009,3,11) => {:default => "2009-03-11", :short => "Mar 11", :long => "March 11, 2009"},
36
+ Date.new(2010,4,30) => {:default => "2010-04-30", :short => "Apr 30", :long => "April 30, 2010"},
37
+ Date.new(2020,12,31) => {:default => "2020-12-31", :short => "Dec 31", :long => "December 31, 2020"}
38
+ }
39
+
40
+ dates.each do |date, formats|
41
+ formats.each do |format, expected_value|
42
+ @backend.localize("en", date, format).should == expected_value
43
+ end
44
+ end
45
+ end
46
+
47
+ it "should localize times in different formats" do
48
+ time = DateTime.new(y=2008,m=3,d=22,h=16,min=30,s=12)
49
+
50
+ @backend.localize("en", time, :default).should == "Sat, 22 Mar 2008 16:30:12 +0000"
51
+ @backend.localize("en", time, :short).should == "22 Mar 16:30"
52
+ @backend.localize("en", time, :long).should == "March 22, 2008 16:30"
53
+ @backend.localize("en", time, '%B %d, %Y %H:%M %p').should == "March 22, 2008 16:30 pm"
54
+ end
55
+
56
+ end
57
+
58
+ describe "and locale es" do
59
+ before(:each) do
60
+ I18n.locale = "es"
61
+ @spanish_locale = Locale.create!(:code => 'es')
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,76 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe I18n::Backend::Database do
4
+ before(:each) do
5
+ @backend = I18n::Backend::Database.new
6
+ end
7
+
8
+ after(:each) do
9
+ @backend.cache_store.clear
10
+ end
11
+
12
+ describe "with default locale en" do
13
+ before(:each) do
14
+ I18n.default_locale = "en"
15
+ @english_locale = Locale.create!(:code => "en")
16
+ end
17
+
18
+ describe "and locale en" do
19
+ before(:each) do
20
+ I18n.locale = "en"
21
+ end
22
+
23
+ it "should localize tagged text" do
24
+ @backend.localize_text("en", "shane ^^is now friends with^^ dylan").should == "shane is now friends with dylan"
25
+ end
26
+
27
+ it "should localize text tagged more than once" do
28
+ @backend.localize_text("en", "dylan ^^is now friends with^^ shane ^^and is happy, ^^dylan ^^claps his hands!^^").should == "dylan is now friends with shane and is happy, dylan claps his hands!"
29
+ end
30
+
31
+ it "should localize tagged text using class method" do
32
+ I18n.localize_text("shane ^^is now friends with^^ dylan").should == "shane is now friends with dylan"
33
+ end
34
+
35
+ it "should localize tagged text using class method alias" do
36
+ I18n.lt("shane ^^is now friends with^^ dylan").should == "shane is now friends with dylan"
37
+ end
38
+
39
+ it "should add localize text tags" do
40
+ I18n.tag_localized_text("is now friends with").should == "^^is now friends with^^"
41
+ end
42
+
43
+ it "should add localize text tags using alias" do
44
+ I18n.tlt("is now friends with").should == "^^is now friends with^^"
45
+ end
46
+
47
+ it "should just return text with no localize text tags" do
48
+ @backend.localize_text("en", "shane is now friends with dylan").should == "shane is now friends with dylan"
49
+ end
50
+
51
+ it "should localize tagged text using custom localize text tag" do
52
+ @backend.localize_text_tag = "##"
53
+ @backend.localize_text("en", "shane ##is now friends with## dylan").should == "shane is now friends with dylan"
54
+ end
55
+
56
+ it "should localize tagged text using another custom localize text tag" do
57
+ @backend.localize_text_tag = "LOCALIZED_TEXT_TAG"
58
+ @backend.localize_text("en", "shane LOCALIZED_TEXT_TAGis now friends withLOCALIZED_TEXT_TAG dylan").should == "shane is now friends with dylan"
59
+ end
60
+
61
+ end
62
+
63
+ describe "and locale es" do
64
+ before(:each) do
65
+ I18n.locale = "es"
66
+ @spanish_locale = Locale.create!(:code => 'es')
67
+ end
68
+
69
+ it "should localize tagged text" do
70
+ @spanish_locale.translations.create!(:key => 'is now friends with', :value => 'ahora es con amigos')
71
+ @backend.localize_text("es", "shane ^^is now friends with^^ dylan").should == "shane ahora es con amigos dylan"
72
+ end
73
+
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,111 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Locale do
4
+ before(:each) do
5
+ @valid_attributes = {
6
+ :code => "en",
7
+ :name => "English"
8
+ }
9
+ end
10
+
11
+ it "should create a new instance given valid attributes" do
12
+ Locale.create!(@valid_attributes)
13
+ end
14
+
15
+ it "should return code as to_param" do
16
+ Locale.new(@valid_attributes).to_param.should == 'en'
17
+ end
18
+
19
+ it "should be invalid with no code" do
20
+ Locale.create!(:code => "en")
21
+ locale = Locale.new
22
+ locale.should_not be_valid
23
+ end
24
+ end
25
+
26
+
27
+ describe "English and Spanish Locales with I18n default locale set to English" do
28
+ before(:each) do
29
+ I18n.default_locale = "en"
30
+ @english_locale = Locale.create!(:code => "en")
31
+ @spanish_locale = Locale.create!(:code => "es")
32
+ end
33
+
34
+ it "should create a translated translation using english locale" do
35
+ translation = @english_locale.create_translation('Hello World', 'Hello World')
36
+ translation.key.should == Translation.hk('Hello World')
37
+ translation.value.should == 'Hello World'
38
+ end
39
+
40
+ it "should create an untranslated translation using spanish locale" do
41
+ translation = @spanish_locale.create_translation('Hello World', 'Hello World')
42
+ translation.key.should == Translation.hk('Hello World')
43
+ translation.value.should be_nil
44
+ end
45
+
46
+ it "should return default locale of English" do
47
+ Locale.default_locale.should == @english_locale
48
+ end
49
+
50
+ it "should return list of non-default locales" do
51
+ @geek_locale = Locale.create!(:code => "gk")
52
+ Locale.non_defaults.should include(@spanish_locale)
53
+ Locale.non_defaults.should include(@geek_locale)
54
+ Locale.non_defaults.should_not include(@english_locale)
55
+ end
56
+
57
+ it "should know that the english_locale is the default" do
58
+ @english_locale.should be_default_locale
59
+ end
60
+
61
+ it "should know if it has a translation record" do
62
+ translation = @english_locale.create_translation('key', 'Hello World')
63
+ @english_locale.should have_translation('key')
64
+ end
65
+
66
+ it "should have 0 translations" do
67
+ @spanish_locale.should have(0).translations
68
+ end
69
+
70
+ it "should have 0 untranslated" do
71
+ @spanish_locale.translations.untranslated.should have(0).records
72
+ end
73
+
74
+ it "should have 0 translated" do
75
+ @spanish_locale.translations.translated.should have(0).records
76
+ end
77
+
78
+ it "should have 100% translated" do
79
+ @spanish_locale.percentage_translated.should == 100
80
+ end
81
+
82
+ end
83
+
84
+ describe "Locale with translations" do
85
+ before(:each) do
86
+ I18n.default_locale = "en"
87
+ @english_locale = Locale.create!(:code => "en")
88
+ @spanish_locale = Locale.create!(:code => "es")
89
+
90
+ @spanish_locale.translations.create!(:key => 'key1', :value => 'translated1')
91
+ @spanish_locale.translations.create!(:key => 'key2', :value => 'translated2')
92
+ @spanish_locale.translations.create!(:key => 'key3', :value => nil) # 1 untranslated record
93
+ end
94
+
95
+ it "should have 3 translations" do
96
+ @spanish_locale.should have(3).translations
97
+ end
98
+
99
+ it "should have 1 untranslated" do
100
+ @spanish_locale.translations.untranslated.should have(1).records
101
+ end
102
+
103
+ it "should have 2 translated" do
104
+ @spanish_locale.translations.translated.should have(2).records
105
+ end
106
+
107
+ it "should have 67% translated" do
108
+ @spanish_locale.percentage_translated.should == 67
109
+ end
110
+
111
+ end