splendeo_translator 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,359 @@
1
+ require 'test_helper'
2
+
3
+ # Include the models/helpers directories on the load path.
4
+ [:models, :helpers, :controllers].each do |path|
5
+ $:.unshift "#{File.dirname(__FILE__)}/fixtures/app/#{path}"
6
+ end
7
+
8
+ # sample AR model
9
+ require 'blog_post'
10
+ # sample ActionMailer
11
+ require 'blog_comment_mailer'
12
+ # sample controller
13
+ require 'blog_posts_controller'
14
+
15
+ # Set up simple routing for testing
16
+ ActionController::Routing::Routes.reload rescue nil
17
+ ActionController::Routing::Routes.draw do |map|
18
+ map.connect ':controller/:action/:id'
19
+ end
20
+
21
+ # For Rails 2.2 compat
22
+ parent_module = ActiveSupport::TestCase
23
+
24
+ if Rails::VERSION::MAJOR == 2 && Rails::VERSION::MINOR > 2
25
+ # Rails 2.3 compat
26
+ parent_module = ActionController::TestCase
27
+ end
28
+
29
+ # Test Translator functionality
30
+ class TranslatorTest < parent_module
31
+
32
+ def setup
33
+ # Create test locale bundle
34
+ I18n.backend = I18n::Backend::Simple.new
35
+
36
+ # tell the I18n library where to find your translations
37
+ I18n.load_path += Dir.glob(File.join(File.dirname(__FILE__), 'locales', '*.{yml,rb}'))
38
+
39
+ # reset the locale
40
+ I18n.default_locale = :en
41
+ I18n.locale = :en
42
+
43
+ # Set up test env
44
+ @controller = BlogPostsController.new
45
+ @request = ActionController::TestRequest.new
46
+ @response = ActionController::TestResponse.new
47
+ super
48
+ end
49
+
50
+ ### ActionController Tests
51
+
52
+ # Test that translate gets typical controller scoping
53
+ def test_controller_simple
54
+ get :index
55
+ assert_response :success
56
+ assert_not_nil assigns
57
+ # Test that controller could translate
58
+ assert_equal I18n.t('blog_posts.index.title'), assigns(:page_title)
59
+ assert_equal I18n.translate('blog_posts.index.intro', :owner => "Ricky Rails"), assigns(:intro)
60
+ end
61
+
62
+ # Test that if something that breaks convention is still processed correctly
63
+ # This case breaks with standard key hierarchy convention
64
+ def test_controller_different_formats
65
+ get :different_formats
66
+ assert_response :success
67
+ assert_not_nil assigns(:taglines)
68
+
69
+ expected = "Hello i18n World" # copied from en.yml
70
+
71
+ assigns(:taglines).each do |str|
72
+ assert_equal expected, str
73
+ end
74
+ end
75
+
76
+ # Test call to translate with default value
77
+ def test_controller_with_defaults
78
+ get :default_value
79
+ assert_response :success
80
+ assert_not_nil assigns(:title)
81
+
82
+ # TODO: Need better way to check that the default was only returned as last resort.
83
+ assert_equal 'the default', assigns(:title)
84
+ end
85
+
86
+ # TODO: Test bulk lookup
87
+ def test_bulk_lookup
88
+ # flunk
89
+ end
90
+
91
+ # Translator should raise an exception on a leading dot key to
92
+ # preserve Rails 2.3 behavior. It is caught & handled
93
+ def test_leading_dot_key
94
+ assert_raise Translator::TranslatorError do
95
+ Translator.translate_with_scope(["blog_posts", "show"], ".category")
96
+ end
97
+ end
98
+
99
+ # Test that first the most specific scope will be tried (controller.action) then
100
+ # back off to just the outer scope (controller)
101
+ def test_controller_shared_messages
102
+ get :admin
103
+ assert_response :redirect
104
+
105
+ # Test that t should have tried the outer scope
106
+ assert_equal I18n.t('blog_posts.flash.invalid_login'), flash[:error]
107
+ end
108
+
109
+ ### ActionView Tests
110
+
111
+ # Test that translate works in Views.
112
+ # Also tests that a dotted key (".foo") can be accepted used, since
113
+ # Rails 2.3 supports it
114
+ def test_view_show
115
+ get :show
116
+ assert_response :success
117
+ post_title = I18n.translate('blog_posts.show.title')
118
+ post_body = I18n.t('blog_posts.show.body', :name => 'hobbes') # matches show.erb
119
+
120
+ assert_match /#{post_title}/, @response.body
121
+ assert_match /#{post_body}/, @response.body
122
+ end
123
+
124
+ # Test that layouts can pull strings
125
+ def test_show_with_layout
126
+ get :show_with_layout
127
+ assert_response :success
128
+
129
+ blog_title = I18n.t('layouts.blog_layout.blog_title')
130
+ assert_match /#{blog_title}/, @response.body
131
+ end
132
+
133
+ # Test that partials pull strings from their own key
134
+ def test_view_partial
135
+ get :footer_partial
136
+ assert_response :success
137
+
138
+ footer = I18n.t('blog_posts.footer.copyright')
139
+ assert_match /#{footer}/, @response.body
140
+ end
141
+
142
+ def test_header_partial
143
+ get :header_partial
144
+ assert_response :success
145
+
146
+ blog_name = I18n.t('shared.header.blog_name')
147
+ assert_match /#{blog_name}/, @response.body
148
+ end
149
+
150
+ # Test that view helpers inherit correct scoping
151
+ def test_view_helpers
152
+ get :archives
153
+ assert_response :success
154
+
155
+ archives_title = I18n.t('blog_posts.archives.title')
156
+ assert_match /#{archives_title}/, @response.body
157
+ end
158
+
159
+ # Test that original behavior of TranslationHelper is not undone.
160
+ # It adds a <span class="translation_missing"> that should still be there
161
+ def test_missing_translation_show_in_span
162
+ Translator.strict_mode(false)
163
+
164
+ get :missing_translation
165
+ assert_response :success
166
+
167
+ # behavior added by TranslationHelper
168
+ assert_match /span class="translation_missing"/, @response.body, "Should be a span tag translation_missing"
169
+ end
170
+
171
+ # Test that strict mode prevents TranslationHelper from adding span.
172
+ def test_strict_mode_in_views
173
+ Translator.strict_mode(true)
174
+
175
+ get :missing_translation
176
+ assert_response :error
177
+ assert_match /18n::MissingTranslationData/, @response.body, "Exception should be for a missing translation"
178
+ end
179
+
180
+ def test_view_without_template
181
+ get :ajax_message
182
+ message = I18n.t('no_template.ajax_message')
183
+ assert_match /#{message}/, @response.body
184
+ end
185
+
186
+ ### ActionMailer Tests
187
+
188
+ def test_mailer
189
+ mail = BlogCommentMailer.create_comment_notification
190
+ # Subject is fetched from the mailer action
191
+ subject = I18n.t('blog_comment_mailer.comment_notification.subject')
192
+
193
+ # Signoff is fetched in the mail template (via addition to ActionView)
194
+ signoff = I18n.t('blog_comment_mailer.comment_notification.signoff')
195
+
196
+ assert_match /#{subject}/, mail.body
197
+ assert_match /#{signoff}/, mail.body
198
+ end
199
+
200
+ ### ActiveRecord tests
201
+
202
+ # Test that a model's method can call translate
203
+ def test_model_calling_translate
204
+ post = nil
205
+ author = "Ricky"
206
+ assert_nothing_raised do
207
+ post = BlogPost.create(:title => "First Post!", :body => "Starting my new blog about RoR", :author => author)
208
+ end
209
+ assert_not_nil post
210
+
211
+ assert_equal I18n.t('blog_post.byline', :author => author), post.written_by
212
+ end
213
+
214
+ # Test that the translate method is added as a class method too so that it can
215
+ # be used in validate calls, etc.
216
+ def test_class_method_translate
217
+
218
+ url = "http://ricky.blog"
219
+ # Call a static method
220
+ assert_equal I18n.t('blog_post.permalink', :url => url), BlogPost.permalink(url)
221
+ end
222
+
223
+ ### TestUnit helpers
224
+
225
+ def test_strict_mode
226
+ Translator.strict_mode(true)
227
+
228
+ # With strict mode on, exception should be thrown
229
+ assert_raise I18n::MissingTranslationData do
230
+ str = "Exception should be raised #{I18n.t('the_missing_key')}"
231
+ end
232
+
233
+ Translator.strict_mode(false)
234
+
235
+ assert_nothing_raised do
236
+ str = "Exception should not be raised #{I18n.t('the_missing_key')}"
237
+ end
238
+ end
239
+
240
+ # Fetch a miss
241
+ def test_assert_translated
242
+ # Within the assert_translated block, any missing keys fail the test
243
+ assert_raise Test::Unit::AssertionFailedError do
244
+ assert_translated do
245
+ str = "Exception should be raised #{I18n.t('the_missing_key')}"
246
+ end
247
+ end
248
+
249
+ assert_nothing_raised do
250
+ str = "Exception should not be raised #{I18n.t('the_missing_key')}"
251
+ end
252
+ end
253
+
254
+ # Test that marker text appears in when using pseudo-translation
255
+ def test_pseudo_translate
256
+ Translator.pseudo_translate(true)
257
+
258
+ # Create a blog post that uses translate to create a byline
259
+ blog_post = BlogPost.create!(:author => "Ricky")
260
+ assert_not_nil blog_post
261
+
262
+ assert_match Translator.pseudo_prepend, blog_post.written_by, "Should start with prepend text"
263
+ assert_match Translator.pseudo_append, blog_post.written_by, "Should end with append text"
264
+ end
265
+
266
+ # Test that markers can be changed
267
+ def test_pseudo_translate_with_diff_markers
268
+ Translator.pseudo_translate(true)
269
+
270
+ start_marker = "!!"
271
+ end_marker = "%%"
272
+
273
+ # Set the new markers
274
+ Translator.pseudo_prepend = start_marker
275
+ Translator.pseudo_append = end_marker
276
+
277
+ get :footer_partial
278
+ assert_response :success
279
+
280
+ # Test that the view has the pseudo-translated strings
281
+ copyright = I18n.t('blog_posts.footer.copyright')
282
+ assert_match /#{start_marker + copyright + end_marker}/, @response.body
283
+ end
284
+
285
+ # Test that if fallback mode is enabled, the default locale is used if
286
+ # the set locale can't be found
287
+ def test_fallback
288
+ # Enable fallback mode
289
+ Translator.fallback(true)
290
+
291
+ # Set the locale to Spanish
292
+ I18n.locale = :es
293
+
294
+ # The index action fetchs 2 keys - 1 has a Spanish translation (intro), 1 does not
295
+ get :index
296
+ assert_response :success
297
+ assert_not_nil assigns
298
+
299
+ # Test that controller could translate the intro from spanish
300
+ assert_equal I18n.t('blog_posts.index.intro', :owner => "Ricky Rails"), assigns(:intro)
301
+
302
+ # Test that global strings are found correctly when they have a prefix
303
+ assert_equal I18n.t('global.sub.key', :locale => :es), @controller.t('global.sub.key')
304
+
305
+ # Should find the English version
306
+ I18n.locale = :en # reset local so call to I18n pulls correct string
307
+ assert_equal I18n.translate('blog_posts.index.title'), assigns(:page_title)
308
+
309
+ # Test that global strings are found correctly when they have a prefix
310
+ assert_equal I18n.t('global.sub.key', :locale => :en), @controller.t('global.sub.key')
311
+ end
312
+
313
+ # Test that fallback
314
+ def test_fallback_with_scoping_backoff
315
+
316
+ # Enable fallback mode
317
+ Translator.fallback(true)
318
+
319
+ # Set the locale to Spanish
320
+ I18n.locale = :es
321
+
322
+ get :about
323
+ assert_response :success
324
+
325
+ # Test that the Spanish version was found
326
+ bio = I18n.t('blog_posts.bio', :locale => :es)
327
+ assert_match /#{bio}/, @response.body
328
+
329
+ # Only English version of this string
330
+ subscribe = I18n.t('blog_posts.subscribe_feed', :locale => :en)
331
+ assert_match /#{subscribe}/, @response.body
332
+ end
333
+
334
+ # Test that we can set up a callback for missing translations
335
+ def test_missing_translation_callback
336
+ test_exception = nil
337
+ test_key = nil
338
+ test_options = nil
339
+
340
+ Translator.set_missing_translation_callback do |ex, key, options|
341
+ test_exception = ex
342
+ test_key = key
343
+ test_options = options
344
+ end
345
+
346
+ get :missing_translation
347
+ assert_response :success
348
+ assert_equal "missing_string", test_key
349
+ assert_not_nil test_options
350
+ assert_not_nil test_exception
351
+ end
352
+
353
+ # Test the generic translate method on Translator that does lookup without a scope, but includes fallback behavior.
354
+ def test_generic_translate_methods
355
+ assert_equal I18n.t('blog_posts.index.intro', :owner => "Ricky Rails"), Translator.translate('blog_posts.index.intro', :owner => "Ricky Rails")
356
+ assert_equal I18n.t('blog_posts.footer.copyright'), Translator.t('blog_posts.footer.copyright')
357
+ end
358
+
359
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: splendeo_translator
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Mike Champion
14
+ - "Enrique Garc\xC3\xADa"
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-08-12 00:00:00 +02:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: Translator makes using Rails internationalization simpler
24
+ email: egarcia@splendeo.es
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files:
30
+ - README.rdoc
31
+ files:
32
+ - README.rdoc
33
+ - Rakefile
34
+ - VERSION.yml
35
+ - lib/splendeo_translator.rb
36
+ - test/fixtures/app/controllers/blog_posts_controller.rb
37
+ - test/fixtures/app/helpers/blog_posts_helper.rb
38
+ - test/fixtures/app/models/blog_comment_mailer.rb
39
+ - test/fixtures/app/models/blog_post.rb
40
+ - test/fixtures/app/views/blog_comment_mailer/comment_notification.rhtml
41
+ - test/fixtures/app/views/blog_posts/_footer.erb
42
+ - test/fixtures/app/views/blog_posts/about.erb
43
+ - test/fixtures/app/views/blog_posts/archives.erb
44
+ - test/fixtures/app/views/blog_posts/missing_translation.erb
45
+ - test/fixtures/app/views/blog_posts/show.erb
46
+ - test/fixtures/app/views/layouts/blog_layout.erb
47
+ - test/fixtures/app/views/shared/_header.erb
48
+ - test/fixtures/schema.rb
49
+ - test/locales/en.yml
50
+ - test/locales/es.yml
51
+ - test/test_helper.rb
52
+ - test/translator_test.rb
53
+ has_rdoc: true
54
+ homepage: http://github.com/splendeo/splendeo_translator
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options:
59
+ - --charset=UTF-8
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.3.7
84
+ signing_key:
85
+ specification_version: 2
86
+ summary: Rails extentions to simplify internationalization
87
+ test_files:
88
+ - test/fixtures/app/controllers/blog_posts_controller.rb
89
+ - test/fixtures/app/helpers/blog_posts_helper.rb
90
+ - test/fixtures/app/models/blog_comment_mailer.rb
91
+ - test/fixtures/app/models/blog_post.rb
92
+ - test/fixtures/schema.rb
93
+ - test/test_helper.rb
94
+ - test/translator_test.rb