Skip to content

Commit

Permalink
Adding rudimentary I18n support
Browse files Browse the repository at this point in the history
  • Loading branch information
carpodaster committed May 4, 2011
1 parent 969387d commit 9ad1954
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/recaptcha/verify.rb
Expand Up @@ -29,7 +29,9 @@ def verify_recaptcha(options = {})
flash[:recaptcha_error] = error
if model
model.valid?
model.errors.add attribute, options[:message] || "Word verification response is incorrect, please try again."
message = "Word verification response is incorrect, please try again."
message = I18n.translate(:'recaptcha.errors.verification_failed', {:default => message}) if defined?(I18n)
model.errors.add attribute, options[:message] || message
end
return false
else
Expand All @@ -40,8 +42,10 @@ def verify_recaptcha(options = {})
flash[:recaptcha_error] = "recaptcha-not-reachable"
if model
model.valid?
model.errors.add attribute, options[:message] || "Oops, we failed to validate your word verification response. Please try again."
end
message = "Oops, we failed to validate your word verification response. Please try again."
message = I18n.translate(:'recaptcha.errors.recaptcha_unreachable', :default => message) if defined?(I18n)
model.errors.add attribute, options[:message] || message
end
return false
rescue Exception => e
raise RecaptchaError, e.message, e.backtrace
Expand Down
23 changes: 23 additions & 0 deletions test/verify_recaptcha_test.rb
Expand Up @@ -2,6 +2,7 @@
require 'rubygems'
require 'active_support/core_ext/string'
require 'mocha'
require 'i18n'
require 'net/http'
require File.dirname(File.expand_path(__FILE__)) + '/../lib/recaptcha'

Expand Down Expand Up @@ -70,6 +71,28 @@ def test_timeout
assert_equal "recaptcha-not-reachable", @controller.flash[:recaptcha_error]
end

def test_message_should_use_i18n
I18n.locale = :de
verification_failed_translated = "Sicherheitscode konnte nicht verifiziert werden."
verification_failed_default = "Word verification response is incorrect, please try again."
recaptcha_unreachable_translated = "Netzwerkfehler, bitte versuchen Sie es später erneut."
recaptcha_unreachable_default = "Oops, we failed to validate your word verification response. Please try again."
I18n.expects(:translate).with(:'recaptcha.errors.verification_failed', :default => verification_failed_default).returns(verification_failed_translated)
I18n.expects(:translate).with(:'recaptcha.errors.recaptcha_unreachable', :default => recaptcha_unreachable_default).returns(recaptcha_unreachable_translated)

errors = mock
errors.expects(:add).with(:base, verification_failed_translated)
errors.expects(:add).with(:base, recaptcha_unreachable_translated)
model = mock; model.stubs(:valid? => false, :errors => errors)

expect_http_post(response_with_body("false\nbad-news"))
@controller.verify_recaptcha(:model => model)

expect_http_post(Timeout::Error, :exception => true)
@controller.verify_recaptcha(:model => model)

end

private

class TestController
Expand Down

0 comments on commit 9ad1954

Please sign in to comment.